Same button to enter and exit an infinite loop and control an input perpetually

Hello,
I was delighted when it occurred to me to create a code so that the same button could be used to enter and exit an infinite loop.

My goal is to use the button as both "on" and "off". I tried to search but only came up with ways that only work by holding the button, and I want to press it ** once ** instead (first to enter and then to exit).

Also, I found some problems when testing because sometimes it works, but only in certain time frames. The code in the loop (to turn the LEDs on and off) is long enough because it makes a complicated play of lights, and therefore the action of exiting the loop ** only worked at the end ** of the led game. I would like it to work ** ALWAYS **, if I press at the beginning, in the middle or at the end.

now, my question is, is there a way to have an entrance (that of the button) perpetually controlled, even while the Arduino is doing other operations?

What code?

Post your attempts, there are as many ways to do this as there are ppl paying any attention to this thread.

a7

If you want the code works ALWAYS, you have to rewrite your code without delay() first

You need the sketch to be in one of 2 states. One started and the other not started. This can be done conveniently using a boolean variable

boolean started = true;

If the variable is false then you are waiting to start

Then you need to detect when the button becomes pressed. See the StateChangeDetection example in the IDE

If started is true and the button becomes pressed then set its state to false and vice versa

Make which sections of code run depend on the state of started

okay, sorry but I'm a newbie

this is the last code i did

# define LED_VERDE 13  // Ho associato al PIN 13 il LED_VERDE
# define LED_BIANCO 12 // Ho associato al PIN 12 il LED_BIANCO
# define LED_ROSSO 8   // Ho associato al PIN 8 il LED_ROSSO
# define BUTTON 7      // Ho associato al PIN 7 il tasto
int val=0;        // Dichiaro la variabile val pari a zero


// Funzione di SETUP si avvia al RESET
void setup() {
  pinMode (LED_VERDE, OUTPUT);   //Dichiaro che il PIN 13 sia un uscita
  pinMode (LED_BIANCO, OUTPUT);  //Dichiaro che il PIN 12 sia un uscita
  pinMode (LED_ROSSO, OUTPUT);   //Dichiaro che il PIN 8 sia un uscita
  pinMode (BUTTON, INPUT);       //Dichiaro che il PIN 7 sia un ingresso
}

void loop(){

    if (digitalRead(BUTTON) == HIGH)
    {
      val = 1;
    } 
    else 
    {
      val = 0;
    }

  while(val == 1){

    
    if(val == 1){
    digitalWrite(LED_VERDE, HIGH);    // Accendo il LED_VERDE (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_BIANCO, HIGH);   // Accendo il LED_BIANCO (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_ROSSO, HIGH);    // Accendo il LED_ROSSO (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_VERDE, LOW);     // Spengo il LED_VERDE (livello BASSO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_BIANCO, LOW);    // Spengo il LED_BIANCO (livello BASSO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_ROSSO, LOW);     // Spengo il LED_ROSSO (livello BASSO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_ROSSO, HIGH);    // Accendo il LED_ROSSO (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_BIANCO, HIGH);   // Accendo il LED_BIANCO (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_VERDE, HIGH);    // Accendo il LED_VERDE (livello ALTO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_ROSSO, LOW);     // Spengo il LED_ROSSO (livello BASSO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_BIANCO, LOW);    // Spengo il LED_BIANCO (livello BASSO)
    delay(100);                       // Attendo 100ms
    digitalWrite(LED_VERDE, LOW);     // Spengo il LED_VERDE (livello BASSO)
    delay(100);                       // Attendo 100ms
    } else {
      break;
    }
        if(digitalRead(BUTTON) == HIGH)
    {
      val = 0;
    } else {
      val = 1;
    }
    delay (200);
  }
}

the comments are in Italian,
and the code in the second if is to turn the LEDs on and off.
thx

ok so.. where can I find StateChangeDetection tutorial in the IDE?

Post #4 gave you your answer. Study that code and use it instead of you just looking at the current state of your digital input.

Also all that copy and paste of the same four instructions should be in a for loop to make your code look a bit more like you know what you are doing.

In the IDE under file -> examples

1 Like

how can I put a "Wait" command to the LEDs without stopping the code?

Did you look in the example sketches.

@Grumpy_Mike
yeah, I found it, thank you

See "BlinkWithoutDelay" IDE example

1 Like

You can use a State Machine.


Look at how a TIMER can be made using the millis( ) function.

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

https://www.baldengineer.com/blink-without-delay-explained.html

This demo-code demonstrates how to use a momentary push-button as a toggle-switch
press once => ON
press again => OFF
press again => ON
press again => OFF
...

best regards Stefan

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