was wondering if anyone can help me with a little problem im having
i have created a circuit and code which does the following:
i have 4 LEDs lined in a row, attached are also a push button and a tilt sensor. everytime the button is pressed the LEDs light up one by one starting with the left most LED. once all 4 LEDs have been lit up, use the tilt sensor to extinguish the LEDs.
i have managed to get this working, however once the LEDs have been extinguished, it ends the loop rather than starting from the begining, meaning that once tilt sensor has been used, pushing the button does not do anything and so the arduino has to be reset
void loop()
{
val = digitalRead(inPin); // read input value of push button
val2 = digitalRead(inPin2); // read input value of tilt switch
digitalWrite(pins*, LOW); // initialises state of the led at the i array position.*
if(val == LOW) // following code is executed if button is pressed*
I can see a couple of problems, but the reason the loop never gets repeated after the LEDs are turned off by the tilt sensor, is the value of i is now equal to num_pins or 4. When you repress the push button, it tries to turn on pins[4] then pin[5], etc.. The variable i needs to get reset. Also, the initialization of the pins to LOW, should go in setup since it only needs to happen once. Here's some code that should work. Be aware I haven't actually tried it yet, so I could have easily made a mistake.
void setup()
{
// add this to the setup
for(int i = 0; i < num_pins; i++)
digitalWrite(pins[i], LOW); // initialises state of the led at the i array position.
}
void loop()
{
val = digitalRead(inPin); // read input value of push button
val2 = digitalRead(inPin2); // read input value of tilt switch
int i;
if(val == LOW) // following code is executed if button is pressed
{
for(i = 0; i < num_pins; i++) // turn on LEDs one by one
{
digitalWrite(pins[i], HIGH);
delay(200); // wait
}
}
if(val2 == 1) // following code is executed if tilt switch is moved
{
for(int i = 0; i< num_pins; i++)
digitalWrite(pins[i], LOW); // turns LED off
}
}