I'm trying to activate different LED flashing modes via a push button

const int LED1 = 13; // the number of the LED pin
const int LED2 = 12; // the number of the LED pin
const int LED3 = 11; // the number of the LED pin
int ms = 400; // The duration of the flashing in milliseconds
int state = 0;
int count = 0;
const int buttonPin = 2; // the pin that the pushbutton is attached to
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() {
pinMode(LED1, OUTPUT); //Set the output pin
pinMode(LED2, OUTPUT); //Set the output pin
pinMode(LED3, OUTPUT); //Set the output pin
pinMode(buttonPin, INPUT); // initialize the button pin as a input:
Serial.begin(9600); //Set the output pin
}
void together() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
}
void traffic() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
}
void flashing() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //LED1 is turned on
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
}
void loop() { //State and count identifier
buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {
  buttonPushCounter++;
  if (count == 4) {
    count = 5;
  }
  Serial.println("on");
  Serial.print("number of button pushes:  ");
  Serial.println(buttonPushCounter);
} else {
  Serial.println("off");
}
delay(50);

}

lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
together();
}
if (buttonPushCounter % 3 == 0) {
traffic();
}
if (buttonPushCounter % 4 == 0) {
flashing();
}
}

Welcome to the Forum!
You will get faster and better help if you post the code as requested by the forum guidelines. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

text (c) @groundFungus

const int LED1 = 13; // the number of the LED pin
const int LED2 = 12; // the number of the LED pin
const int LED3 = 11; // the number of the LED pin
int ms = 400; // The duration of the flashing in milliseconds
int state = 0;
int count = 0;
const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 1; // previous state of the button

void setup() {
pinMode(LED1, OUTPUT); //Set the output pin
pinMode(LED2, OUTPUT); //Set the output pin
pinMode(LED3, OUTPUT); //Set the output pin
pinMode(buttonPin, INPUT_PULLUP); // initialize the button pin as a input:
Serial.begin(9600); //Set the output pin
}
void together() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
}
void traffic() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
}
void flashing() {
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, HIGH); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //Each LED current state
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(ms); //Flash delay in milliseconds
digitalWrite(LED1, LOW); //LED1 is turned on
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(ms); //Flash delay in milliseconds
}
void checkButton()
{
 switch (buttonPushCounter) {
    case 0:
        together();
      break;
    case 1:
        traffic();
      break;
    case 2:
        flashing();
      break;
    case 3:
        //off
      break;
  }  
}
void loop() { //State and count identifier
buttonState = digitalRead(buttonPin);

if (lastButtonState == LOW && buttonState == HIGH)
{ 
    buttonPushCounter++;
    if(buttonPushCounter > 3)
      {
        buttonPushCounter =0;
      }
  Serial.println("on");
  Serial.print("number of button pushes:  ");
  Serial.println(buttonPushCounter);
}
else 
{
  Serial.println("off");
}

 lastButtonState = buttonState;
 checkButton();
}

Thanks for posting your code in code tags, but it is obvious you did not read the forum guide as requested. Perhaps that is because it is written in English?

Your first post has not been corrected. It is breaking forum rules. Please fix that.

What help would you like from the forum?

It might help us to help you if you explain what your code is supposed to do and what it actually does.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

I am the forum's unofficial acerbic curmudgeon, so please don't judge the forum by my hostile responses. You will get lots of help here if you ask the questions correctly.

Before you copy code for the forum, press CTRL-T to properly format your code. This makes the code easier to read and sometimes highlights mismatched braces.

I can guess that the button is not responding to button presses. But rather than guess, please do as @sterretje recommends.

Sorry It's my first time posting on this forum and first time coding. I have 3 LEDs on a breadboard. There are 3 different ways in which they must flash. I am wanting to push a button and it must change to a different mode, similar to how Christmas lights work.

OK, that was what it must do. Now describe what it actually does and where your problem is.

And please read the link in post #2, edit your opening post and apply code tags to the code (using the </> button).

If you press the button 2 times in rapid succession do you want the previous pattern stopped and the new pattern started?

The problem with all of the delay() calls you have is that the processor stops and button presses will be missed. For example, if the flashing() function is called then the button will not be read for almost 5 seconds!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.