Make running led on off with push botton

hi all, i need help.
i wanna code for a project, when push the botton the led will start running, and willbe stop when i push the button again.
but my result code is the led start running first and cant stop when i pust the botton

here my code, thanks for helping
const byte LED1 = 10; // Pin for LED 1
const byte LED2 = 11; // Pin for LED 2
const byte LED3 = 12; // Pin for LED 3
const byte SW = 8; // Pin for the switch

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(SW, INPUT_PULLUP); // Use internal pull-up resistor
}

void loop() {
bool val = digitalRead(SW); // Read the switch state

// If the switch is pressed (LOW)
if (val == LOW) {
for (int i = 0; i < 3; i++) {
digitalWrite(LED1, HIGH);
delay(100);
digitalWrite(LED1, LOW);

  digitalWrite(LED2, HIGH);
  delay(100);
  digitalWrite(LED2, LOW);
  
  digitalWrite(LED3, HIGH);
  delay(100);
  digitalWrite(LED3, LOW);
}

} else {
// If the switch is not pressed, turn off all LEDs
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
}

Have you done any code for this yet?
Maybe you have already done the running LED code and now you want to know how to start / stop the running LEDs with a button press.
If you haven't done the running LEDs yet, there's a tutorial here
When you have the running LEDs working, you can ask how to add the code for the button to turn the running off and on

  • Your switch is not wired correctly.

  • Wire it as S3 is wire below, a pressed switch will give a LOW when it is read.
    Turn on the pull-up.

  • Avoid using Pins 0 and 1 as they are used for serial communications.

1 Like

Arduino wrote an exercise for lighting an LED.
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/

Arduino wrote an exercise for a pushbutton.
https://docs.arduino.cc/built-in-examples/digital/Button/

... and many more example exercises... https://docs.arduino.cc/built-in-examples/

1 Like

What exactly is a "Running LED"? Possibly you mean a LED chaser where the LEDs sequentially turn On/Off in a chasing effect? Should you want a LED Chaser a simple Google of LED Chaser Arduino will get you plenty of examples and code.

Next have you read the forum guidelines? The way this works is you make your code and post it appropriately using code tags. Then explain where you want help with your code. Next, when I see a post like yours using all upper case letters I generally just ignore it. Again, read the forum guidelines.

Ron

1 Like

i have do some code but the led cant run.

If you provide your code it will be easier to help you.

I already put my code, thanks for helping me

You need to post your code in code tags:

sorry, i new here. i will change it . thanks

I already put my code, thanks for helping me

Oh, I didn't saw it.

What is that resistor for? The one connected to the button

for the led and push botton, i dont know too, my teacher ask me to make a running led on off with push botton. and i see the example for 1 led they make like this skema. so i follow them for 3 leds

If you're trying to make a running led circuit, the button should not be directly connected to the LED's.

Also, your code says INPUT_PULLUP, so the button should be connected to GND and not 5v.

  • Don’t make major changes to your posts as it make subsequent posts lost.
1 Like

Here the text book, i make the led 3

Ok thanks, im newbe in this forum

  • Have you tried the Blink example ?

https://docs.arduino.cc/built-in-examples/basics/Blink/

  • Have you tried the Button example ?

https://docs.arduino.cc/built-in-examples/digital/Button/

Your code from post #1 works like this:

  • All LEDs start OFF
  • Press and hold the button and the LEDs "cycle"
  • Release the button and the LEDs will run for 300 ms, then turn OFF

The LEDs will not stop if the button is stuck in a LOW state, maybe due to wiring, because the button pin is held HIGH by the INPUT_PULLUP resistor.

const byte LED1 = 10; // Pin for LED 1
const byte LED2 = 11; // Pin for LED 2
const byte LED3 = 12; // Pin for LED 3
const byte SW = 8; // Pin for the switch

int buttonState = 0;  // variable for reading the pushbutton status
int lastButtonState = LOW; // previous state of the button
unsigned long lastDebounceTime = 0; // last time the output was toggled
unsigned long debounceDelay = 50; // debounce time

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(SW, INPUT_PULLUP); // Use internal pull-up resistor
}

void loop() {
  int reading = digitalRead(SW);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading == LOW) {
      buttonState = HIGH; // Button is pressed
    } else {
      buttonState = LOW; // Button is not pressed
    }
  }

  lastButtonState = reading;

  if (buttonState == HIGH) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(LED1, HIGH);
      delay(100);
      digitalWrite(LED1, LOW);
      delay(100);

      digitalWrite(LED2, HIGH);
      delay(100);
      digitalWrite(LED2, LOW);
      delay(100);

      digitalWrite(LED3, HIGH);
      delay(100);
      digitalWrite(LED3, LOW);
      delay(100);
    }
  } else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
  }
}