Starter Problems...

/*

  • Bike light, final version
    */

int switchPin = 2; // switch is connected to pin 2
int redleft = 12;
int redright = 11;
int redtop = 10;
int greentop = 9;
int greenleft = 8;
int greenright = 7;

int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state

int lightMode = 0; // What mode is the light in?

void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input

pinMode(redleft, OUTPUT);
pinMode(redright, OUTPUT);
pinMode(redtop, OUTPUT);
pinMode(greentop, OUTPUT);
pinMode(greenleft, OUTPUT);
pinMode(greenright, OUTPUT);

Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}

void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // if its off
lightMode = 1; // turn lights on!
} else {
if (lightMode == 1) { // if its all-on
lightMode = 2; // make it blink!
} else {
if (lightMode == 2) { // if its blinking
lightMode = 3; // make it wave!
} else {
if (lightMode == 3) { // if its waving,
lightMode = 0; // turn light off!
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}

// Now do whatever the lightMode indicates
if (lightMode == 0) { // all-off
digitalWrite(redleft, LOW);
digitalWrite(redright, LOW);
digitalWrite(redtop, LOW);
digitalWrite(greentop, LOW);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);

}

if (lightMode == 1) { // all-on
digitalWrite(redleft, LOW);
digitalWrite(redright, LOW);
digitalWrite(redtop, LOW);
digitalWrite(greentop, HIGH);
digitalWrite(greenleft, HIGH);
digitalWrite(greenright, HIGH);
}
if (lightMode == 2) {
digitalWrite(redleft, HIGH);
digitalWrite(redright, HIGH);
digitalWrite(redtop, HIGH);
digitalWrite(greentop, HIGH);
digitalWrite(greenleft, HIGH);
digitalWrite(greenright, HIGH);
delay(3000);
digitalWrite(redleft, HIGH);
digitalWrite(redright, HIGH);
digitalWrite(redtop, HIGH);
digitalWrite(greentop, LOW);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);
delay(6000);
digitalWrite(redleft, HIGH);
digitalWrite(redright, HIGH);
digitalWrite(redtop, LOW);
digitalWrite(greentop, LOW);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);
delay(5000);
digitalWrite(redleft, LOW);
digitalWrite(redright, LOW);
digitalWrite(redtop, LOW);
digitalWrite(greentop, LOW);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);
delay(8000);
}
if (lightMode == 3) { // "wave"
digitalWrite(redleft, HIGH);
digitalWrite(redright, HIGH);
digitalWrite(redtop, HIGH);
digitalWrite(greentop, LOW);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);
delay(6000);
digitalWrite(redleft, HIGH);
digitalWrite(redright, HIGH);
digitalWrite(redtop, LOW);
digitalWrite(greentop, HIGH);
digitalWrite(greenleft, LOW);
digitalWrite(greenright, LOW);
delay(5000);
digitalWrite(redleft, LOW);
digitalWrite(redright, LOW);
digitalWrite(redtop, LOW);
digitalWrite(greentop, HIGH);
digitalWrite(greenleft, HIGH);
digitalWrite(greenright, HIGH);

}
}

Why does the 3rd lightmode not run after pressing the switchpin? And how do i only run one lightmode once and not keep it in the loop? THANKS IN ADVANCE:)

If you re-edit the post and put the code in code tags (the # button), we'll find it easier to read...

I chose "i dunnu", so I hope that helps. :slight_smile:

Thanks for the help...