Static variable shared with all objects

I’m trying to make my first code using class (never used it before), a static variable that a declared inside the class is being shared with the objects… Is there a way to make each object have its own static variable?

Since the variable is shared (nextToggle) the LEDs don’t toggle in the specified time.

class led
{
public:
  uint8_t ledPin;
  unsigned long delayMilliseconds;

  /* 
  Funcao para mudar o estado do led ele pode ser chamada constantemente pois
  o led somente sera atualizado quando o perido indicado tiver passado.

  parametros: 
  ledPin - pino em que o LED esta conectado
  delayMilliseconds - Tempo para que a proxima atualizacao seja aceita
  */
  void toggleLed(uint16_t delayMilliseconds)
  {
    static unsigned long nextToggle = 0;

    if ((digitalRead(ledPin)) && (millis() >= nextToggle))
    {
      digitalWrite(ledPin, LOW);
      nextToggle = millis() + delayMilliseconds;
    }
    else if ((!digitalRead(ledPin)) && (millis() >= nextToggle))
    {
      digitalWrite(ledPin, HIGH);
      nextToggle = millis() + delayMilliseconds;
    }
  }

  /*
  Desligar o Led 
  */
  void turnOffLed ()
  {
    digitalWrite(ledPin, LOW);
  }

  /*
  Ligar o Led
  */
  void turnOnLed ()
  {
    digitalWrite(ledPin, HIGH);
  }

  /*
  Confifura o pino como saida
  */
  void setAsOutput()
  {
    pinMode(ledPin, OUTPUT);
  }

  /*
  Configura o pino como entrada
  */
  void setAsInput()
  {
    pinMode(ledPin, INPUT);
  }
};
led ledStatus, ledMQTTActivity;

void setup()
{
  // put your setup code here, to run once:
  ledStatus.ledPin = ledStatusPin;
  ledStatus.setAsOutput();
  ledMQTTActivity.ledPin = ledMQTTActivityPin;
  ledMQTTActivity.setAsOutput();
}

void loop()
{
  // put your main code here, to run repeatedly:
  ledStatus.toggleLed(1000);
  ledMQTTActivity.toggleLed(250);
}

If you want to share it outside of the class it needs to be a public variable in the class. To share between functions inside the class it needs to be a private variable. Buried inside the function it cannot be shared.

No, I don`t want it to be shared... I want each (ledStatus and ledMQTTActivity) to haver its own static variable nextToggle. Otherwise the LEDs don't blink properly.

Whoa, slow down..

Here's myClass.h

extern int aClassVar;

class myClass {

   public:
              myClass(void);
   virtual ~myCLass(void);

              void aMethod(void);
              
              int aVariable;
};

What exactly are you asking for?

-jim lee

Kumogakure:
No, I don`t want it to be shared...

Then why are you making it static? The whole point of a static class variable is to be able to share it among all instances of the class. If you want each instance to have its own copy, then make it an instance variable.

You don't need member variables to be 'static' for them to retain their value for the life of the object.

gfvalvo:
Then why are you making it static? The whole point of a static class variable is to be able to share it among all instances of the class. If you want each instance to have its own copy, then make it an instance variable.

:slight_smile: :slight_smile: :slight_smile: Ok, now it works... Thanks!