Could someone please help me? I had gone over the basics and about 4 hours of trial and error got me here
// constant integer variables, will not change
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int greenLedPin = 12; // the pin that the LED is attached to
const int redLedPin = 13;
// integer variables, will be altered
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the RED LED as an output:
pinMode(redLedPin, OUTPUT);
// initialize the GREEN LED as an output:
pinMode(greenLedPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// call displaySequence method with buttonPushCounter as parameter
displaySequence(buttonPushCounter);
}
void displaySequence(int buttonPushCounter)
{
// check the number of button pressed and follow sequence according to it
// If buttonPushCounter is pressed the first time, blink Red LED in the sequence
// If buttonPushCounter is pressed the second time, blink Red and Green as part of th sequence
// If buttonPushCounter is pressed the third time, reset the counter back to 0 so that the sqeuence can be played again
if (buttonPushCounter == 1) {
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(100);
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(100);
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(200);
digitalWrite(redLedPin, HIGH);
delay(400);
digitalWrite(redLedPin, LOW);
delay(200);
digitalWrite(redLedPin, HIGH);
delay(400);
digitalWrite(redLedPin, LOW);
delay(200);
digitalWrite(redLedPin, HIGH);
delay(400);
digitalWrite(redLedPin, LOW);
delay(200);
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(100);
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(100);
digitalWrite(redLedPin, HIGH);
delay(100);
digitalWrite(redLedPin, LOW);
delay(500);
} else if(buttonPushCounter == 2) {
digitalWrite(redLedPin, HIGH);
delay(200);
digitalWrite(redLedPin, LOW);
delay(400);
digitalWrite(greenLedPin, HIGH);
delay(200);
digitalWrite(greenLedPin, LOW);
delay(400);
digitalWrite(redLedPin, HIGH);
delay(200);
digitalWrite(redLedPin, LOW);
delay(400);
digitalWrite(greenLedPin, HIGH);
delay(200);
digitalWrite(greenLedPin, LOW);
delay(100);
digitalWrite(redLedPin, HIGH);
delay(200);
digitalWrite(redLedPin, LOW);
delay(400);
digitalWrite(greenLedPin, HIGH);
delay(200);
digitalWrite(greenLedPin, LOW);
delay(400);
} else if(buttonPushCounter == 3) {
buttonPushCounter = 0;
}
}
This code basically starts the first sequence when the button is pressed, and when the button is pressed and held down it changes the sequence once the first one is over and then when its pressed and held again, it stops.
My goal is to press button once sequence 1 starts, press button again to start sequence 2, press again to start sequence 1, press again to start seq 2 again, etc.
Anyways does anyone see anything i could do to make it work?