Unable to understand what is happening in this code
Posted: Thu Mar 17, 2022 12:23 pm
Hello everyone, i have a question that might be trivial for some of you but i really can't understand what is going on.
Do you understand why this work well:
but this, not at all :
(Difference is the while becoming an if)
Thanks!
Do you understand why this work well:
Code: Select all
#include <Arduino.h>
#define LED 12
#define R1 27
#define NO 14
bool RUNNING = true;
bool TRIGGER = false;
void IRAM_ATTR NO_trigger()
{
if(!TRIGGER && RUNNING)
{
TRIGGER = true;
}
}
void setup()
{
pinMode(LED, OUTPUT);
pinMode(NO, INPUT_PULLUP);
attachInterrupt(NO, NO_trigger, RISING);
}
void loop()
{
while(RUNNING)
{
while(TRIGGER)
{
pinMode(R1, OUTPUT);
digitalWrite(LED, HIGH);
delay(200);
pinMode(R1, INPUT);
digitalWrite(LED, LOW);
TRIGGER = false;
}
}
}
Code: Select all
#include <Arduino.h>
#define LED 12
#define R1 27
#define NO 14
bool RUNNING = true;
bool TRIGGER = false;
void IRAM_ATTR NO_trigger()
{
if(!TRIGGER && RUNNING)
{
TRIGGER = true;
}
}
void setup()
{
pinMode(LED, OUTPUT);
pinMode(NO, INPUT_PULLUP);
attachInterrupt(NO, NO_trigger, RISING);
}
void loop()
{
while(RUNNING)
{
if(TRIGGER)
{
pinMode(R1, OUTPUT);
digitalWrite(LED, HIGH);
delay(200);
pinMode(R1, INPUT);
digitalWrite(LED, LOW);
TRIGGER = false;
}
}
}
Thanks!