tombe_s
November 14, 2022, 4:22pm
1
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?
alto777
November 14, 2022, 4:34pm
2
tombe_s:
The code in the loop
What code?
Post your attempts, there are as many ways to do this as there are ppl paying any attention to this thread.
a7
b707
November 14, 2022, 4:36pm
3
tombe_s:
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 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
tombe_s
November 14, 2022, 4:39pm
5
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
tombe_s
November 14, 2022, 4:44pm
6
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
tombe_s
November 14, 2022, 4:46pm
8
how can I put a "Wait" command to the LEDs without stopping the code?
LarryD
November 14, 2022, 4:48pm
9
Did you look in the example sketches.
tombe_s
November 14, 2022, 4:49pm
10
@Grumpy_Mike
yeah, I found it, thank you
b707
November 14, 2022, 4:50pm
11
See "BlinkWithoutDelay" IDE example
1 Like
LarryD
November 14, 2022, 4:56pm
12
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
...
Hi everybody,
from time to time I enjoy writing demo-codes for a certain functionality.
EDIT 04.06.2022
user @dlloyd has written a library with toggle-functionality
This means all details how to debounce the button and detecting state-change etc. are hided away from the user inside his library. This makes it easier to apply this functionality but harder to understand.
So in post #3 there is a version that makes use of the toogle.h-library.
The toogle.h-library can be installed from the li…
best regards Stefan
system
Closed
May 13, 2023, 7:29pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.