Press push button differnt times to get differnt led output lengths

This may seem easy I am new to programming and assume there is just some code I just dont know. But here is my problem.

I have a push button turning on an led and it lights in a particular sequence. But then I want it to light differntly the next time the push button is pressed.

Here is the code I have that works for the first press fine:

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 12; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); delay(5000); digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin, HIGH); delay(3000); } else { // turn LED off: digitalWrite(ledPin, LOW); }

I then added:

if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(300); digitalWrite(ledPin, HIGH); delay(100); } else { // turn LED off: digitalWrite(ledPin, LOW); } }

The idea was it would then use that the next time the push button was hit instead it looks like it just cycles through the whole thing. Right now the whole thing is

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 12; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); delay(5000); digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin, HIGH); delay(3000); } else { // turn LED off: digitalWrite(ledPin, LOW); } if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(300); digitalWrite(ledPin, HIGH); delay(100); } else { // turn LED off: digitalWrite(ledPin, LOW); } }

Any help would be greatly appreciated. If you can fix the code or even better if you can explain the problem/what is going on in the arduinos head so I can learn that would be even better. Thanks

Hi and welcome.

Read the "How to use this forum" you'll find in any section of the forum. You'll learn how to ask questions and how to put code in code tags.

Also, you're using very long delays (seconds are ages to a controller). A better way to do this is shown in the "Blink without delay" example sketch that was very conveniently delivered when you installed the IDE.

The trick is that you keep track of what you are and have been doing. And that trick is an universal one. So, to answer your question: You are looking for some keypress at the moment and then do something. At the moment you have decided you have to do something, you forget the key was pressed. Don't throw that valuable information away, but store it. Count the number of keypresses and react accordingly. Make a variable before your loop, let's call it Pressed:

 byte Pressed = 0; // A byte has a range from 0 to 255, so 256 in total it saves some space over an integer.

After you have registered a keypress, you can make the value of Pressed higher. You can limit it to some maximum value if you like so you are within the maximum number of presses you like to register:

if (buttonState == HIGH) {
Pressed ++;
if (Pressed == 5) Pressed = 4;
}

Now check the value of pressed instead of buttonState to decide what to do.

This will however not do exactly do what you want it to do. Because buttonState might be checked thousands of times per second (if you do not use delays). So you also have to look for changes in buttonState instead of the actual value of it, or else you will get incorrect counts. Again, keep track of things and you'll be able to handle that too.

Let's see what your next sketch will do.

Thanks for the info. I did check the how to sue but it doesnt show the whole thing but found if i click on it then I can see all the info and found stuff you were talking about like how to put in code.

Appreciate the help.