Variable not changing on a constructor - Arduino Nano

Helo, I'm trying to change 3 variables on a constructor named

Flasher led1(6,tiempoPrendida,tiempoApagada,intensidad);

I managed to change intensidad with a global variable, but when I try the same with both tiempoPrendida and tiempoApagada ,I can't. The only difference I can see is that intensidad is of type int and the other two are of type float.

the three variables are parsed from a HTTPREAD method, the recognition is working fine because I printed the value of the variables and they are exactly what they are supposed to be. THe problem is that when using the constructor, I successfully made the led attached to pin 6 of the Arduino change its intensity, but I cant change the blinking time.

If i manually enter a number, in the constructor or at the variable definition it works great, but really don't know why the other two aren't changing.

I'm thinking there's something about classes,or constructors in C that I may be not considering.

If you know something that I can do to make this thing work It would be great, I've been tryin to solve this issue with no success for hours really.

Here's the code and a short part of the log.

Thanks.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <AltSoftSerial.h>


#define rxPin 10  
#define txPin 11
SoftwareSerial sim800L(rxPin,txPin); 

//GPS Module RX pin to Arduino 9
//GPS Module TX pin to Arduino 8
AltSoftSerial neogps;

TinyGPSPlus gps;

int readPin=A2; // lectura de voltaje de la bateria

int BLUELed = 6;  //pin our blue LED is connected to
int ledState = LOW; //used to control blue LED state, we're starting with it OFF

unsigned long currentMillis = 0; //stores the current time
unsigned long previousMillis = 0; //stores last time blue LED was updated

int    intensidad=50;        //cantidad de candelas
long   tiempoPrendida=10000;             //cuanto tiempo esta encendida la baliza
long   tiempoApagada=10000;           // cuanto tiempo esta apagada la baliza

//--------------------------ISR-------------------------------------------------------

class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int  ledPin;      // the number of the LED pin
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time

  // These maintain the current state
  int ledState;                 // ledState used to set the LED
  unsigned long previousMillis;   // will store last time LED was updated

  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off,int intensidad)
  {
  ledPin = pin;
  pinMode(ledPin, OUTPUT);     
    
  OnTime = on;
  OffTime = off;
  
  ledState = LOW; 
  previousMillis = 0;
  }

  void Update(unsigned long currentMillis)
  {
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      analogWrite(ledPin, 0);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      analogWrite(ledPin, intensidad);   // Update the actual LED
    }
  }
};


Flasher led1(6,tiempoPrendida,tiempoApagada,intensidad); //(6,123,400)

//--------------------------FIN ISR-------------------------------------------------------

void setup(){

  pinMode(readPin,INPUT);
  pinMode(BLUELed, OUTPUT);

//-----------------ISR------------------------------------------------------------------

 // Timer0 is already used for millis() - we'll just interrupt somewhere
  // in the middle and call the "Compare A" function below
  OCR0A = 0xAF;
  TIMSK0 |= _BV(OCIE0A);

//--------FIN ISR ----------------------------------------------------------------------
  
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  //Begin serial communication with Arduino and SIM800L
  sim800L.begin(9600);
  //Begin serial communication with Arduino and SIM800L
  neogps.begin(9600);

  Serial.println("Initializing...");
 
}

void loop(){
                
              while(sim800L.available()){
                Serial.write(sim800L.read());
                          }
              while(Serial.available())  {
                sim800L.write(Serial.read());
                          }

unsigned long currentMillis = millis();
                     controlLedsyGPS();
                     
                     Serial.println("el valor de la variable tiempoPrendida es");
                     Serial.println(tiempoPrendida);
                     Serial.println("el valor de la variable tiempoApagada es");
                     Serial.println(tiempoApagada);
                     Serial.println("el valor de la variable intensidad es");
                     Serial.println(intensidad);
                     delay(1000);
                            }

void controlLedsyGPS(){
    
      //Can take up to 60 seconds
    boolean newData = false;
    for (unsigned long start = millis(); millis() - start < 10000;){
      while (neogps.available()){
        if (gps.encode(neogps.read())){
          newData = true;
          break;
        }
      }
    }
  
    //If newData is true
    if(true){
      newData = false;

//Control de carga--------------------------------------------------------------------------------------------
      
      int v1=0;                 //Lectura de voltaje en el pin A2
      float voltaje1_temp=0;
      v1=analogRead(readPin);
      voltaje1_temp=(5./1023.)*v1 ;  //el punto es porque preciso que el valor sean un float con decimales

      char buffer[6];  //convertimos el float a string para ser enviado por el metodo GET
      dtostrf(voltaje1_temp,4,2,buffer);  //paso intermedio conviertiendo a un array de char[]
      String voltaje1=String(buffer); //lo convierto a string 

//Control de carga----------------------------------------------------------------------------------------------      
      
      String latitude, longitude ;
        
      latitude = String(gps.location.lat(), 6); // Latitude in degrees (double)
      longitude = String(gps.location.lng(), 6); // Longitude in degrees (double)
            
      Serial.print("Latitude= "); 
      Serial.print(latitude);
      Serial.print(" Longitude= "); 
      Serial.print(longitude);
      Serial.print(" Voltaje1= "); 
      Serial.println(voltaje1);
      //if (latitude == 0) {return 0;}
       
      String url,tmp;
      url = "http://balizas2.000webhostapp.com/gpsdata.php?lat=";
      url += latitude;
      url += "&lng=";
      url += longitude;
      url += "&voltaje1=";
      url+=  voltaje1;
      url += "&estado=1"; 
           
      tmp = "http://balizas2.000webhostapp.com/getstate.php?color=All";
      
      Serial.println(tmp);    
      delay(300);

      Serial.println(url);    
      delay(300);

//-----------------Lectura de valores de la interfaz de usuario----------------------------------------------

    sim800L.println("AT+CSQ"); // Signal quality check
    delay(100);
    sim800L.println("AT+CBC"); // Signal quality check
    delay(100);
    ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
    sim800L.println("AT+CGATT?"); //Attach or Detach from GPRS Support
    delay(100);
    ShowSerialData();
    sim800L.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
    delay(1000);
    ShowSerialData();
    sim800L.println("AT+SAPBR=3,1,\"APN\",\"antel.lte\"");//setting the APN, Access point name string
    delay(2000);
    ShowSerialData();
    sim800L.println("AT+SAPBR=0,1");//setting the SAPBR
    delay(1000);
    ShowSerialData();
    sim800L.println("AT+SAPBR=1,1");//setting the SAPBR
    delay(1000);
    ShowSerialData();
    sim800L.println("AT+HTTPINIT"); //init the HTTP request
    delay(1000); 
    ShowSerialData();
    sim800L.println("AT+HTTPPARA=\"URL\",\"balizas2.000webhostapp.com/getstate.php?color=All\"");// setting the httppara, the second parameter is the website you want to access
    delay(1000);
    ShowSerialData();
    sim800L.println("AT+HTTPACTION=0");//submit the request 
    delay(2000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
    //while(!sim800L.available());
    ShowSerialData();
    sim800L.println("AT+HTTPREAD");// read the data from the website you access
    delay(300);
    dim();
    sim800L.println("");
    delay(100);
    sim800L.println("AT+HTTPTERM");//setting the SAPBR
    delay(1000);
    ShowSerialData();
    
//--------------------------- GPS - Con multitarea simulada para no perder oraciones ---------------------
    
    sendATcommand("AT+CGATT?", "OK", 2000);
    //Connection type: GPRS - bearer profile 1
    sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
    //sets the APN settings for your network provider.
    sendATcommand("AT+SAPBR=3,1,\"APN\",\"antel.lte\"", "OK", 2000);
    sendATcommand("AT+SAPBR=0,1", "OK", 2000);
    //enable the GPRS - enable bearer 1
    sendATcommand("AT+SAPBR=1,1", "OK", 2000);
    //sendATcommand("AT+HTTPTERM", "OK", 1000);
    sendATcommand("AT+HTTPINIT", "OK", 2000); 
    sim800L.print("AT+HTTPPARA=\"URL\",\"");
    sim800L.print(url);
    sendATcommand("\"", "OK", 1000);
    //Set up the HTTP action
    sendATcommand("AT+HTTPACTION=0", "0,200", 1000);
    sendATcommand("AT+HTTPTERM", "OK", 1000);
    
  }
  return 1;
}

// -------------------funcion para enviar comandos AT simulando multitarea (con millis())----------------

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    //Initialice the string
    memset(response, '\0', 100);
    delay(100);
    
    //Clean the input buffer
    while( sim800L.available() > 0) sim800L.read();
    
    if (ATcommand[0] != '\0'){
      //Send the AT command 
      sim800L.println(ATcommand);
    }

    x = 0;
    previous = millis();

    //this loop waits for the answer with time out
    do{
       //if there are data in the UART input buffer, reads it and checks for the asnwer
        if(sim800L.available() != 0){
            response[x] = sim800L.read();
            //Serial.print(response[x]);
            x++;
            // check if the desired answer (OK) is in the response of the module
            if(strstr(response, expected_answer) != NULL){
                answer = 1;
            }
        }
    }while((answer == 0) && ((millis() - previous) < timeout));

                      Serial.println(response);
                      return answer;
            }


// -------------------------Mostrar datos por el puerto serie-----------------
 
 void ShowSerialData(){
  
    while(sim800L.available()!=0)
    Serial.write(char (sim800L.read()));
                      }

// -----------------DIM--------------------------------------------------------------

void dim(){
                       String content = "";
                      // String RedState = content.substring();
                       while(sim800L.available()!=0)
                       {  
                          //Serial.write(sim800L.read());
                          content = content + String(char (sim800L.read())); //almaceno el contenido de los caracteres del array 
                       }
                           Serial.println(content);      //verificacion de que se recibio la trama
                           Serial.println(content[1]);
                           for (int i = 0; i < 32; i++){
                            Serial.println(content[i]);
                           }
//----------Decodificacion de intensidad-----------------------------------------                   
                           if(content.substring(28,29)== "5"){
                                      intensidad=255;
                                            }
                       else if (content.substring(28,29)== "4"){
                                      intensidad=200;      
                                            }
                       else if (content.substring(28,29)== "3"){
                                      intensidad=150;
                                            }
                       else if (content.substring(28,29)== "2"){
                                      intensidad=100;
                                             }
                       
                       else if (content.substring(28,29)== "1"){
                                      intensidad=50;
                                             }
                       
                       else if (content.substring(28,29)== "0"){
                                      intensidad=0;
                                             } 
                                               
//--------------Decodificacion de tiempoPrendida---------------------------------------             
                       
                       if(content.substring(29,30)== "5"){
                                     tiempoPrendida=2500; 
                                          }
                       else if (content.substring(29,30)== "4"){
                                     tiempoPrendida=2000; 
                                          }
                       else if (content.substring(29,30)== "3"){
                                     tiempoPrendida=1500; 
                                          }
                        else if (content.substring(29,30)== "2"){
                                     tiempoPrendida=1000; 
                                          }
                       else if (content.substring(29,30)== "1"){
                                     tiempoPrendida=500; 
                                          }   
                       else if (content.substring(29,30)== "0"){
                                     tiempoPrendida=0; 
                                          } 
                                          
//----------------------Decodificacion de tiempoApagada---------------------------------
                                                                                                                                                           
                       if(content.substring(30,31)== "5"){
                                     tiempoApagada=2500; 
                                          }
                       else if (content.substring(30,31)== "4"){
                                     tiempoApagada=2000; 
                                          }
                       else if (content.substring(30,31)== "3"){
                                     tiempoApagada=1500; 
                                          }
                        else if (content.substring(30,31)== "2"){
                                     tiempoApagada=1000; 
                                          }
                       else if (content.substring(30,31)== "1"){
                                     tiempoApagada=500; 
                                          }   
                       else if (content.substring(30,31)== "0"){
                                     tiempoApagada=0; 
                                          } 
                        content = "";
                              }


//-------------------ISR-------------------------------------------------------

// Interrupt is called once a millisecond, to update the LEDs

SIGNAL(TIMER0_COMPA_vect) {
      
        unsigned long currentMillis = millis();
  
        led1.Update(currentMillis);
                  }
                  
//------------------FIN ISR-----------------------------------------------------

The log..., notice how the variables change correctly apparently but nothing happens...

05:04:30.091 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.79
05:04:30.185 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:04:30.466 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.79&estado=1
05:04:31.016 -> AT+CBC

05:04:31.063 -> +CBC: 0,100,4219
05:04:31.063 -> 
05:04:31.063 -> OK
05:04:31.110 -> AT+CGATT?

05:04:31.110 -> +CGATT: 1
05:04:31.157 -> 
05:04:31.157 -> OK
05:04:32.141 -> AT+SAPBR=3,1,"CONTYPE","GPRS"

05:04:32.188 -> OK
05:04:34.186 -> AT+SAPBR=3,1,"APN","antel.lte"

05:04:34.233 -> OK
05:04:35.217 -> AT+SAPBR=0,1

05:04:35.217 -> OK
05:04:36.248 -> AT+SAPBR=1,1

05:04:36.248 -> OK
05:04:37.233 -> AT+HTTPINIT

05:04:37.280 -> OK
05:04:38.311 -> AT+HTTPPARA="URL","balizas2.000webhostapp.com/getstate.php?coloAT+HTTPACTION=0

05:04:40.373 -> OK
05:04:40.654 -> AT+HTTPREAD

05:04:40.701 -> OK

05:04:40.701 -> 
05:04:40.701 -> T
05:04:40.701 -> A
05:04:40.701 -> T
05:04:40.701 -> +
05:04:40.701 -> H
05:04:40.701 -> T
05:04:40.701 -> T
05:04:40.701 -> P
05:04:40.701 -> R
05:04:40.701 -> E
05:04:40.701 -> A
05:04:40.701 -> D
05:04:40.701 -> 

05:04:40.748 -> 

05:04:40.748 -> 
05:04:40.748 -> 
05:04:40.748 -> O
05:04:40.748 -> K
05:04:40.748 -> 

05:04:40.748 -> 
05:04:40.748 -> 
05:04:40.748 -> 

+HTTPACTION: 0,200,135
05:04:47.591 -> 
05:04:47.591 -> el valor de la variable tiempoPrendida es
05:04:47.638 -> 10000
05:04:47.638 -> el valor de la variable tiempoApagada es
05:04:47.685 -> 10000
05:04:47.685 -> el valor de la variable intensidad es
05:04:47.732 -> 50
05:04:58.699 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.78
05:04:58.746 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:04:59.074 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.78&estado=1
05:04:59.590 -> AT+CSQ

05:04:59.590 -> +CSQ: 18,0
05:04:59.637 -> 
05:04:59.637 -> OK
05:04:59.637 -> AT+CBC

05:04:59.637 -> +CBC: 0,100,4224
05:04:59.683 -> 
05:04:59.683 -> OK
05:04:59.730 -> AT+CGATT?

05:04:59.730 -> +CGATT: 1
05:04:59.730 -> 
05:04:59.730 -> OK
05:05:00.761 -> AT+SAPBR=3,1,"CONTYPE","GPRS"

05:05:00.808 -> OK
05:05:02.777 -> AT+SAPBR=3,1,"APN","antel.lte"

05:05:02.824 -> OK
05:05:03.800 -> AT+SAPBR=0,1

05:05:03.847 -> OK
05:05:04.834 -> AT+SAPBR=1,1

05:05:04.834 -> OK
05:05:05.866 -> AT+HTTPINIT

05:05:05.866 -> +CME ERROR: 3
05:05:06.897 -> AT+HTTPPARA="URL","balizas2.000webhostapp.com/getstate.php?coloAT+HTTPACTION=0

05:05:08.948 -> OK

05:05:08.948 -> 
05:05:08.948 -> +HTTPACTION: 0,200,4
05:05:09.276 -> AT+HTTPREAD

05:05:09.276 -> +HTTPREAD: 4
05:05:09.276 -> 555	
05:05:09.276 -> OK
05:05:09.276 -> 
05:05:09.323 -> T
05:05:09.323 -> A
05:05:09.323 -> T
05:05:09.323 -> +
05:05:09.323 -> H
05:05:09.323 -> T
05:05:09.323 -> T
05:05:09.323 -> P
05:05:09.323 -> R
05:05:09.323 -> E
05:05:09.323 -> A
05:05:09.323 -> D
05:05:09.323 -> 

05:05:09.323 -> 

05:05:09.323 -> 
05:05:09.323 -> 
05:05:09.323 -> +
05:05:09.370 -> H
05:05:09.370 -> T
05:05:09.370 -> T
05:05:09.370 -> P
05:05:09.370 -> R
05:05:09.370 -> E
05:05:09.370 -> A
05:05:09.370 -> D
05:05:09.370 -> :
05:05:09.370 ->  
05:05:09.370 -> 4
05:05:09.370 -> 

05:05:09.370 -> 
05:05:09.370 -> 
05:05:09.370 -> 5
05:05:09.370 -> 5
05:05:09.417 -> 5
05:05:09.417 -> 	
05:05:10.448 -> 
AT+HTTPTERM

05:05:10.495 -> OK
05:05:10.589 -> AT+CGATT?

05:05:10.636 -> +CGATT: 1
05:05:10.636 -> 
05:05:10.636 -> OK

05:05:10.776 -> AT+SAPBR=3,1,"Contype","GPRS"

05:05:10.823 -> OK
05:05:10.964 -> AT+SAPBR=3,1,"APN","antel.lte"

05:05:11.011 -> OK
05:05:11.387 -> AT+SAPBR=0,1

05:05:11.387 -> OK
05:05:11.715 -> AT+SAPBR=1,1

05:05:11.762 -> OK
05:05:11.855 -> AT+HTTPINIT

05:05:11.855 -> OK
05:05:12.091 -> =.p&estado=1"

05:05:12.137 -> OK
05:05:13.170 -> AT+HTTPACTION=0

05:05:13.216 -> OK
05:05:13.216 -> 
05:05:13.216 -> +HTTPACTION: 0,200
05:05:13.310 -> AT+HTTPTERM

05:05:13.357 -> OK
05:05:13.357 -> el valor de la variable tiempoPrendida es
05:05:13.404 -> 2500
05:05:13.404 -> el valor de la variable tiempoApagada es
05:05:13.451 -> 2500
05:05:13.451 -> el valor de la variable intensidad es
05:05:13.498 -> 255
05:05:14.435 -> 
05:05:24.425 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.61
05:05:24.472 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:05:24.800 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.61&estado=1
05:05:25.363 -> AT+CSQ

05:05:25.363 -> +CSQ: 18,0
05:05:25.363 -> 
05:05:25.363 -> OK
05:05:25.363 -> AT+CBC

05:05:25.363 -> +CBC: 0,90,4125
05:05:25.409 -> 
05:05:25.409 -> OK
05:05:25.456 -> AT+CGATT?

05:05:25.456 -> +CGATT: 1
05:05:25.456 -> 
05:05:25.503 -> OK
05:05:26.501 -> AT+SAPBR=3,1,"CONTYPE","GPRS"

05:05:26.501 -> OK

when you do this, it's a one off. You instantiate the Flasher class to create the led1 variable with whatever values are currently in the variables or constant you pass (6,tiempoPrendida,tiempoApagada,intensidad).

If in the future tiempoPrendida,tiempoApagada,intensidad are changing, the led1 instance won't know.

if you want this to happen, then your instance should keep a pointer or reference to those variables, so that everytime they are being read inside the class, you get the real current value.

Note that it's not a good practice to do anything hardware related in the constructor

    Flasher(int pin, long on, long off, int intensidad)
    {
      ledPin = pin;
      pinMode(ledPin, OUTPUT); // <==== THIS IS NOT GOOD PRACTICE

as you don't know when the constructor will be called for a global variable. it might be very early, clearly before main() (and thus setup()) is called and so whatever is in the main() call might undo what you did in your constructor or mess with it.

the best practice is to separate things out and create a begin method you call in the setup()

...
    Flasher(int pin, long on, long off, int intensidad)
    {
      ledPin = pin;
      OnTime = on;
      OffTime = off;
      ledState = LOW;
      previousMillis = 0;
    }

  begin() {
    pinMode(ledPin, OUTPUT);
  }
...

and in setup()

Flasher led1(6, tiempoPrendida, tiempoApagada, intensidad); //(6,123,400)

void setup() {
  led1.begin(); // configure our flasher instance
  pinMode(readPin, INPUT);
  pinMode(BLUELed, OUTPUT);
...

I've not read all the code but if there is an ISR involved using volatile for the variables and having critical sections management is necessary

Great, obviously you know what you are talking about....in less than 5 minutes you found flaws. I'll try to change them in order to make it more realiable,..so at first sight, the reason why intensidad is changing but the other two not, is because , its like some kind of random thing, it may work or not, in this case, the variable intensidad was considered but the other two were ignored, right?

I'll have to study this thing you told me...
"... using volatile for the variables and having critical sections management is necessary" .

Thank you for your help, if you have a few minutes take a glimpse at my code, I'm positive you will find something that may solve my issue.

If I can solve this last part of the blinking (i'm almost there), I'll be really close to succeed on my project.

I'm reading on my iPhone at the moment so not easy to see everything in one go or type code

a few ideas:

  • the main code has no business maintaining the intensidad variable, it should be within the flasher class. I would add a method updateIntenisty(int newValue)
  • I would not pass currentMillis() to Update. Just let update read millis on its own.
  • for readability I would use HIGH and LOW for any digitalWrite and not 1 and 0

for example:

class Flasher
{
    // Class Member Variables
    int intensidad = 50;      //cantidad de candelas
    int  ledPin;      // the number of the LED pin
    long OnTime;     // milliseconds of on-time
    long OffTime;    // milliseconds of off-time

    // These maintain the current state
    int ledState;                 // ledState used to set the LED
    unsigned long previousMillis;   // will store last time LED was updated


  public:
    // Constructor - creates a Flasher
    // and initializes the member variables and state
    Flasher(int pin, long on, long off, int intensidad)
    {
      ledPin = pin;
      OnTime = on;
      OffTime = off;
      ledState = LOW;
      previousMillis = 0;
    }

    void begin() {
      pinMode(ledPin, OUTPUT);
    }

    void updateIntenisty(int newValue) {
      intensidad = newValue;
    }

    void Update()
    {
      unsigned long currentMillis = millis();

      if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
      {
        ledState = LOW;  // Turn it off
        previousMillis = currentMillis;  // Remember the time
        analogWrite(ledPin, 0);  // Update the actual LED
      }
      else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
      {
        ledState = HIGH;  // turn it on
        previousMillis = currentMillis;   // Remember the time
        analogWrite(ledPin, intensidad);   // Update the actual LED
      }
    }
};

and change

  //----------Decodificacion de intensidad-----------------------------------------
  if (content.substring(28, 29) == "5") {
    intensidad = 255;
  }
  else if (content.substring(28, 29) == "4") {
    intensidad = 200;
  }
  else if (content.substring(28, 29) == "3") {
    intensidad = 150;
  }
  else if (content.substring(28, 29) == "2") {
    intensidad = 100;
  }

  else if (content.substring(28, 29) == "1") {
    intensidad = 50;
  }

  else if (content.substring(28, 29) == "0") {
    intensidad = 0;
  }

into

  //----------Decodificacion de intensidad-----------------------------------------
  if (content.substring(28, 29) == "5") {
    led1.updateIntenisty(255);
  }
  else if (content.substring(28, 29) == "4") {
    led1.updateIntenisty(200);
  }
  else if (content.substring(28, 29) == "3") {
   led1.updateIntenisty(150);
  }
  else if (content.substring(28, 29) == "2") {
    led1.updateIntenisty(100);
  }
  else if (content.substring(28, 29) == "1") {
    ed1.updateIntenisty(50);
  }
  else if (content.substring(28, 29) == "0") {
    ed1.updateIntenisty(0);
  }

and remove millis from the Update() call


SIGNAL(TIMER0_COMPA_vect) {
  led1.Update();
}

this way when the next ISR strikes, you'll have the updated variable inside the class

If you need the other variables like tiempoPrendida,tiempoApagada to change, bundle them into the class in the same way.

the updateIntenisty() would need a critical section when you change the value (ie the ISR should not trigger during that time) as when you change this value which is over multiple bytes, the ISR might strike and then your code will have possibly a half updated value in memory.

what Arduino are you using ?

If that's what you got from what he typed, I can't help you either.

I'm using a Nano.
Thank you for your help,I'll try to implement those changes and tell you if its working.

Thank you so much for your help, your explanation was superb. Now I understand what was really going on.
I'll make the changes and tell you about it.

I would like to thank J-M-L and Delta_G for the help they provided, thanks to them I was able to make my code work as I wanted.

They both helped me understand and solve the problems I had with my code.

1 Like

Great, it worked. Thank you.

1 Like

Great, it worked. I would like to thank you for helping me these last few days.

Good.

Did you understand the need for the critical section?

I believe so.
Not sure how can I implement it because , as the ISR is the responsible of the led blinking dimming pattern, I think that if I halt the process or the ISR, maybe there's some kind of visual perception that the led in not working as it should be.
Ive to make some calculations to know, how many milliseconds does the updateIntensity() take, in order to be sure that if I pause the triggering of the ISR, it won't be noticeable.

The visual perception won’t be impacted if you have a couple micro seconds critical section delaying a bit the ISR

ISR has 85 cycles overhead to support the critical need to not learn timing.

How often the interrupt is called is where it can matter, otherwise pfftttt!

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