Light It Up

By pressing and holding the button down, you can “charge” up the board. The longer you hold down the button, the more power the board will get, which causes the LEDs to blink for a longer time period.

This demo can be framed in different ways, the simplest being a battery charge indicator. It could also be used to visualize and experience the amount of energy required to power different household appliances for a young audience.

 


//LIGHT IT UP


//Variables
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int onoff = 11;

long startCount = 0;
long finalCount;

//Variable Parameters; adjust the timing of counting (counterMultiplier) and number of blinks (timeDecay)
int counterMultiplier = 190;
float timeDecay = 0.3;

//=================
void setup() {
 Serial.begin(9600);
 
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(led5, OUTPUT);
 pinMode(onoff, INPUT);
}

void loop() {
 while (digitalRead(onoff) == HIGH) {
 startCount = startCount + 1;
 Serial.println(startCount);
 }
 finalCount = startCount;
 startCount = 0;
 if (finalCount > 0 && finalCount <= (2*counterMultiplier)) { Serial.println("LED: 1"); blinkOne(); } else if (finalCount > (2*counterMultiplier) && finalCount <= (4*counterMultiplier)) { Serial.println("LED: 2"); blinkTwo(); blinkOne(); } else if (finalCount > (4*counterMultiplier) && finalCount <= (6*counterMultiplier)) { Serial.println("LED: 3"); blinkThree(); blinkTwo(); blinkOne(); } else if (finalCount > (6*counterMultiplier) && finalCount <= (8*counterMultiplier)) { Serial.println("LED: 4"); blinkFour(); blinkThree(); blinkTwo(); blinkOne(); } else if (finalCount > (8*counterMultiplier)) {
 Serial.println("LED: 5");
 blinkFive();
 blinkFour();
 blinkThree();
 blinkTwo();
 blinkOne();
 }
}


//==================
void blinkOne() {
 do {
 digitalWrite(led1, HIGH);
 delay(500);
 digitalWrite(led1, LOW);
 delay(500);
 finalCount = finalCount - (timeDecay*counterMultiplier);
 Serial.print("1 Final Count ");
 Serial.println(finalCount);
 }
 while (finalCount >= 0);
}

void blinkTwo() {
 do {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 delay(500);
 digitalWrite(led2, LOW);
 delay(500);
 finalCount = finalCount - (timeDecay*counterMultiplier);
 Serial.print("2 Final Count ");
 Serial.println(finalCount);
 }
 while (finalCount >= (2*counterMultiplier));
}

void blinkThree() {
 do {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 delay(500);
 digitalWrite(led3, LOW);
 delay(500);
 finalCount = finalCount - (timeDecay*counterMultiplier);
 Serial.print("3 Final Count ");
 Serial.println(finalCount);
 }
 while (finalCount >= (4*counterMultiplier));
}

void blinkFour() {
 do {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, HIGH);
 delay(500);
 digitalWrite(led4, LOW);
 delay(500);
 finalCount = finalCount - (timeDecay*counterMultiplier);
 Serial.print("4 Final Count ");
 Serial.println(finalCount);
 }
 while (finalCount >= (6*counterMultiplier));
}

void blinkFive() {
 do {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, HIGH);
 digitalWrite(led5, HIGH);
 delay(500);
 digitalWrite(led5, LOW);
 delay(500);
 finalCount = finalCount - (timeDecay*counterMultiplier);
 Serial.print("5 Final Count ");
 Serial.println(finalCount);
 }
 while (finalCount >= (8*counterMultiplier));
}