good evening
can anyone help a newbie
when I press the button I can get the program to increase the state but only as far as 3
what am I missing?
also how would I program it to start back at 0 state?
many thanks
int L1 = 2;
int L2 = 3;
int L3 = 4;
int L4 = 5;
int L5 = 6; //sets the leds to pin number
int buttonPin = 13; //the number of the pushbutton pin
int de=100; // delay time
// variables to hold the new and old switch states
int buttonState = 0;
byte state = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 500; // the debounce time; increase if the output flickers
void setup()
{
pinMode(L1,OUTPUT); // Sets the leds to output
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(L4,OUTPUT);
pinMode(L5,OUTPUT);
pinMode(buttonPin, INPUT); // sets button as an input
}
void loop()
{
{
buttonState = digitalRead(buttonPin);
// has the button switch been closed?
if ( buttonState == HIGH )
{
// increase the value of state
state++;
}
}
// Turn on the next LED
// Because the value of state does not change while we are testing it we don't need to use else if
if (state == 1) {
digitalWrite(L1,1);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0);
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,1);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //2
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,1);
digitalWrite(L4,0);
digitalWrite(L5,0); //3
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,1);
digitalWrite(L5,0); //4
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,1); //5
delay(de);
}
if (state == 2) {
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,1);
delay(de);
digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //6
delay(de);
}
if (state == 3) {
digitalWrite(L5,0); // turn the pin on:
digitalWrite(L4,0); // turn the pin on:
digitalWrite(L3,0); // turn the pin on:
digitalWrite(L2,0); // turn the pin on:
digitalWrite(L1,0); // turn the pin on:
delay(200);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L3,0); // turn the pin off
digitalWrite(L4,1); // turn the pin on:
delay(100);
digitalWrite(L4,0); // turn the pin off
digitalWrite(L5,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L3,0); // turn the pin off
digitalWrite(L4,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(500);
}
if (state == 4) {
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,1);
}
}
new.ino (4.66 KB)