Imperfect light entertainment

SEE THE UPDATES BELOW

In order to upgrade the drum that I made last time, I tried to make the light part of the entertainment more appealing, using the knowledge gained from the last class. From the video, you could see there are 5 lamps implemented and light one by one. Imagine the buttons are the sticks. It is supposed to look like a streak of light, like a thunder. (It just doesn’t look like it is, but with better equipments, it can look better). When two switches are closed at the same time, all the lights light up at the same time.

 

Here is the coding of this light performance.

 

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT); // configure pin 13 as output
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(13, INPUT);
  pinMode(14, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(13) == HIGH) {
    digitalWrite(2, HIGH);
    delay(100);
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(3, HIGH);
    delay(100);
    digitalWrite(3, LOW);
    delay(100);
    digitalWrite(4, HIGH);
    delay(100);
    digitalWrite(4, LOW);
    delay(100);
    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    delay(100);
    digitalWrite(6, HIGH);
    delay(100);
    digitalWrite(6, LOW);
    delay(100);
  }
  else {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }
  if (digitalRead(12) == HIGH) {
    digitalWrite(2, HIGH);
    delay(100);
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(3, HIGH);
    delay(100);
    digitalWrite(3, LOW);
    delay(100);
    digitalWrite(4, HIGH);
    delay(100);
    digitalWrite(4, LOW);
    delay(100);
    digitalWrite(5, HIGH);
    delay(100);
    digitalWrite(5, LOW);
    delay(100);
    digitalWrite(6, HIGH);
    delay(100);
    digitalWrite(6, LOW);
    delay(100);
  }
  else {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }
  if (digitalRead(12) == HIGH && digitalRead(13) == HIGH) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    delay(1000);
  }
  else {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }
}

 

The problem is, somehow the streak of lights do not stop even when I take my hands off the switches (sticks), and repeat itself for multiple times, sometimes a few times and sometimes 6 or even 9 times. Looking at the codes, I do not see any problems. I could not solve why this happened, and thought it was because of connection or switch.

Also, it was not easy to press the switches at the same time, and if there was any difference in the moment of the one switch closed and another, there was a streak of light first and then all-light-up after that, which is not supposed to work like. The solution to this may be to shorten the duration of the streak (setting each delay to be shorter), so that there is no lag for each beating of the sticks (switches).

UPDATES

The problems have been solved thanks to Scott and Mateo. About the continuing streak of lights although the switches are left open; the thing was, I did not connect the switches to the ground by strong resistors, thus providing the opportunities for current to stay in the switches for a while, making the streak of lights continue to occur. So I connected the switches to the ground, and it worked!

The next problem is how the streak of light occurs first before the everything-light-up when the two switches are closed at the same time. This was solved using the coding below at the beginning of void loop.

  if (digitalRead(12) == HIGH || digitalRead(13) == HIGH) {
    delay(50);

By putting this code, there will be a short time (50milliseconds) of lag before anything happens, allowing the two switches to be closed when the next step comes. The problem was that it was virtually impossible to press both buttons at the same time.

 

 

Leave a Reply