Unable to understand what is happening in this code

d3Ex22
Posts: 2
Joined: Thu Mar 17, 2022 12:05 pm

Unable to understand what is happening in this code

Postby d3Ex22 » 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:

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;
    }
  }
}
but this, not at all :

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;
    }
  }
}
(Difference is the while becoming an if)
Thanks!

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Unable to understand what is happening in this code

Postby ESP_Sprite » Fri Mar 18, 2022 12:42 am

I can't explain why one does work, but I can tell you why the other doesn't: the C compiler assumes that variables can only change after some specific events, e.g. a function call. In your while loop, if trigger is false, there is no function call in the 'path' the code takes, so the compiler assumes that whatever trigger was initially will be what trigger always will be.

There's three solutions to this. The first is to make the 'trigger' value volatile. That's kinda dirty, also because you still have a tight loop (as in: the 'while' will run continuously without giving the OS time to do other things). The second one is to add a delay of 10ms or so inside of the loop. That both gives the OS time to do other things, as well as makes sure there's always a function call, so the C compiler will assume that variables may ave changed. The third, galaxy-brain solution is to make use of the multitasking tools the OS gives you and use a semaphore; if you wait on a semaphore your task won't even run until the semaphore is set by your interrupt handler.

d3Ex22
Posts: 2
Joined: Thu Mar 17, 2022 12:05 pm

Re: Unable to understand what is happening in this code

Postby d3Ex22 » Fri Mar 18, 2022 8:04 am

ESP_Sprite wrote:
Fri Mar 18, 2022 12:42 am
I can't explain why one does work, but I can tell you why the other doesn't: the C compiler assumes that variables can only change after some specific events, e.g. a function call. In your while loop, if trigger is false, there is no function call in the 'path' the code takes, so the compiler assumes that whatever trigger was initially will be what trigger always will be.

There's three solutions to this. The first is to make the 'trigger' value volatile. That's kinda dirty, also because you still have a tight loop (as in: the 'while' will run continuously without giving the OS time to do other things). The second one is to add a delay of 10ms or so inside of the loop. That both gives the OS time to do other things, as well as makes sure there's always a function call, so the C compiler will assume that variables may ave changed. The third, galaxy-brain solution is to make use of the multitasking tools the OS gives you and use a semaphore; if you wait on a semaphore your task won't even run until the semaphore is set by your interrupt handler.
Thanks a lot, that's exactly what i needed to know. Very quick and usefull, thank you !

Who is online

Users browsing this forum: No registered users and 69 guests