How to enable and disable function?

hi, i,m new here and this is my first project. i just managed to blinking three leds separately and used potentiometer to control their blinking speed. now i want to add enable/disable this blink function by pressing push button (button2) once. i,m totally new to this. what is the next code will be added to this project that i could enable/disable blink function by pressing once push button ? help will be appreciated and thanks in advance. (my english is weak hope you understand me)
and here is my first project code.

// Assign output variables to GPIO pins
int potPin= A0;  //Declare potPin to be analog pin A0
int button = 14;
int button2 = 12;
int RED = 2;
int GREEN = 4;
int BLUE = 5;
int pwmMin = 0;  // delay time
int pwmMax = 1023;
int analogState;         // variable for reading blinking speed
int mode = 0;
int blink;

void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
 //setup buttons
pinMode(potPin, INPUT);  
pinMode (button, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
}
void loop() {
  analogState = analogRead(potPin);
  blink = map(analogState, pwmMin, pwmMax, 50, 1023); //allows for pot to control bliking speed

if (digitalRead(button) == LOW) {
  mode = mode + 1;
  delay(300);

  }

//Green
 if (mode == 0) {
  digitalWrite(BLUE, 255);
  digitalWrite(GREEN, 255);
  digitalWrite(RED, 255);
  delay(blink);
  digitalWrite(BLUE, 255);
  digitalWrite(GREEN, 0);
  digitalWrite(RED, 255);
  delay(blink);
  
}
//Blue
 if (mode == 1) {
  digitalWrite(BLUE, 255);
  digitalWrite(GREEN, 255);
  digitalWrite(RED, 255);
  delay(blink);
  digitalWrite(BLUE, 0);
  digitalWrite(GREEN, 255);
  digitalWrite(RED, 255);
  delay(blink);
  
}
//Red
 if (mode == 2) {
  digitalWrite(BLUE, 255);
  digitalWrite(GREEN, 255);
  digitalWrite(RED, 255);
  delay(blink);
  digitalWrite(BLUE, 255);
  digitalWrite(GREEN, 255);
  digitalWrite(RED, 0);
  delay(blink);
  
}

//back
 if (mode == 3) {
  mode = 0;

 }
}

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

You can do this by using a boolean flag to enable / disable the blinking by pushing the button
First declare a
bool enable = true;
Then in the loop:

bool buttonState = digitalRead(button2);
if (!buttonState) { # Pullup is LOW when pushed
  enable = !enable;
  delay(30);
}

And change your ifs as follows:
if (mode == 0 && enable)

1 Like

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

Once you can detect when the button has become pressed you can use that action to set the value of a boolean variable. Set it to true when you want the blinking to occur and false when you want it to stop. Test the value of the boolean and only execute the blinking code when it is true

Now let's acknowledge the elephant in the room. The delay() function stops all other code in your sketch from doing anything while the delay() is in progress. This can make code unresponsive because it takes time for inputs to be acted upon

The solution is to use non blocking code for timing events but it is not a simple drop in replacement for the delay() function

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

1 Like

thanks for reply buddy. as i mention above i,m new to this, please if you dont mind me asking, could you please correct my full code ?

that,s the idea. thanks for your reply mate.

Not easily on a smartphone, but it's easy: just put this line before the setup:
bool enable = true;

Then add the block

bool buttonState = digitalRead(button2);
if (!buttonState) { # Pullup is LOW when pushed
  enable = !enable;
  delay(30);
}

at the very beginning of the loop, and change all your if as advised.

if (mode == 0 && enable)
if (mode == 1 && enable)
if (mode == 2 && enable)

and test if it works...

1 Like

how about

    if (enable)  {
        if (mode == 0)
            ...
        if (mode == 1)
            ...
        if (mode == 2)
            ...
    }

I guess it's the same. Maybe the compiler will provide the same code for both

what's more important, the code generated by the compiler or readability?

plus it's redundant

it works, thanks for your cooperate.

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