How to stop code being currently executed

Hi, So I am trying to create a project where three buttons are pressed to change states of LEDs and one is the 'off' button but I want to make it so while the LEDs are going on with the state I can push the button to change it to another state but the LEDs don't change states when I press another state button but instead, it doesn't even happen when i press it though.

Here's the script.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 7;// the number of the pushbutton pin
const int buttonPin1 = 8;
const int buttonPin2 = 9;

// variables will change:
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;

bool isRunning = true;

void setup() {
  //led pins here.
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

 //button pins here.
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

//functions here.
void ledControl_(int Times, int delayTime){
  if(isRunning == true){
    for (int x = 0 ; x < Times ;x++ )
  { 
  digitalWrite (1, HIGH);
  digitalWrite (2, HIGH);
  digitalWrite (3, HIGH);
  digitalWrite (4, HIGH);
  digitalWrite (5, HIGH);
  digitalWrite (6, HIGH);
  delay(delayTime);
  digitalWrite (1, LOW);
  digitalWrite (2, LOW);
  digitalWrite (3, LOW);
  digitalWrite (4, LOW);
  digitalWrite (5, LOW);
  digitalWrite (6, LOW);
  delay(delayTime);
  }}}

void ledControl_1(int Null, int delayTime){
  if(isRunning == true){
    for (int x = 0 ; x < 20 ;x++ )
  { 
  if(isRunning == false){
  digitalWrite (1, LOW);
  digitalWrite (2, LOW);
  digitalWrite (3, LOW);
  digitalWrite (4, LOW);
  digitalWrite (5, LOW);
  digitalWrite (6, LOW);
  break;
  }
  digitalWrite (1, HIGH);
  digitalWrite (2, LOW);
  digitalWrite (3, HIGH);
  digitalWrite (4, LOW);
  digitalWrite (5, HIGH);
  digitalWrite (6, LOW);
  delay(delayTime);
  digitalWrite (1, LOW);
  digitalWrite (2, HIGH);
  digitalWrite (3, LOW);
  digitalWrite (4, HIGH);
  digitalWrite (5, LOW);
  digitalWrite (6, HIGH);
  delay(delayTime);
  digitalWrite (1, LOW);
  digitalWrite (2, LOW);
  digitalWrite (3, LOW);
  digitalWrite (4, LOW);
  digitalWrite (5, LOW);
  digitalWrite (6, LOW);
  
  }}}

void loop() {
  

    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if(buttonState == HIGH){
    if(isRunning == true){
      ledControl_(20, 100);
    }
    delay(50);
  }else if(buttonState1 == HIGH){
    if(isRunning == true){
      ledControl_1(0,100);
    }else if(buttonState2 == HIGH){
   isRunning = false;
    delay(50);
    }  }  }

(Code tags added by Moderator)

Remove the for loops. Remove the delay() calls in loop().

Mark your code as code, otherwise the forum system mangles it!

If above doesn't work, post a wiring diagram!

Hello
Well I would like to give the advise to study the IOP-Model to achieve a proper solution for your task.
I=Input --> read buttons
P=Process --> generation of related bit-pattern
O=Out --> controlling leds with given bit-pattern

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