Led blinks when off but not when already on.

class Led {
  private:
    int m_pin;
    int m_state;
  
  public:
    Led (int pin = 0, int state = LOW) {
      m_pin = pin;
      m_state = state;

      pinMode(m_pin, OUTPUT);
      digitalWrite(m_pin, m_state);
    };

    void blink () {
      toggle();
      toggle();
    };

    void toggle () {
      m_state = !m_state;

      digitalWrite(m_pin, m_state);

      delay(10);
    };
};

I have a step sequencer and I want the led of each step to quickly blink (on - off - on) or (off - on - off) whenever the sequence is playing that step.

When the step is not active and the led is off, its blink (off - on - off) just fine.

But if the step is active then the led just goes off instead of going (on - off - on) it goes (on - off)

Not sure if being clear!

I can paste all of my code if that helps.

#include <Arduino.h>

class Step {
  public:
    boolean m_state;

    Step (boolean state = false) {
      m_state = state;
    };
};

class Led {
  private:
    int m_pin;
    int m_state;
  
  public:
    Led (int pin = 0, int state = LOW) {
      m_pin = pin;
      m_state = state;

      pinMode(m_pin, OUTPUT);
      digitalWrite(m_pin, m_state);
    };

    void blink () {
      toggle();
      toggle();
    };

    void toggle () {
      m_state = !m_state;

      digitalWrite(m_pin, m_state);

      delay(10);
    };
};

class Sequence {
  private:
    boolean m_state;
    int m_currentStep;
    Led* m_leds;
    Step m_steps[4];
  
  public:
    Sequence (Led *leds, boolean state = false) {
      m_leds = leds;
      m_state = state;
      m_currentStep = 0;
    };

    void toggle () {
      m_state = !m_state;
    };

    void toggleStep (int id) {
      m_steps[id].m_state = !m_steps[id].m_state;
    };

    void check () {
      if (m_state) {
        m_leds[m_currentStep].blink();

        if (m_steps[m_currentStep].m_state) {
          // Serial.print(m_currentStep);
          // Serial.println(" step should play");
        }

        m_currentStep++;

        if (m_currentStep > 3) {
          m_currentStep = 0;
        }
      }
    };
};

class Button {
  private:
    int m_id;
    int m_pin;
    int m_state;
    int m_lastState;
    int m_reading;
    boolean m_startStop;
    unsigned long m_lastDebounceTime;
    Led &m_led;
    Sequence &m_sequence;

  public:
    Button ();
    Button (
      int id,
      int pin, 
      Led& led,
      Sequence& sequence,
      boolean startStop = false
    ): m_led(led), m_sequence(sequence) {
      m_id = id;
      m_pin = pin;
      m_startStop = startStop;

      pinMode(pin, INPUT);
    };

    void check (void) {
      m_reading = digitalRead(m_pin);

      if (m_reading != m_lastState) {  
        m_lastDebounceTime = millis();
      }

      if ((millis() - m_lastDebounceTime) > 50) {
        if (m_reading != m_state) {
          m_state = m_reading;
          
          if (m_state == HIGH && m_startStop) {
            // Serial.println("Stat/Stop pressed:");
            m_led.toggle();
            m_sequence.toggle();
          } else if (m_state == HIGH) {
            m_led.toggle();
            m_sequence.toggleStep(m_id - 1);
          }
          
          // Serial.println(m_pin);
        }
      }

      m_lastState = m_reading;
    };
};

Led led1(3, LOW);
Led led2(5, LOW);
Led led3(7, LOW);
Led led4(9, LOW);
Led led5(13, LOW);

Led leds[4] = {led1, led2, led3, led4};

Sequence sequence1(leds, false);

Button button1(1, 2, led1, sequence1);
Button button2(2, 4, led2, sequence1);
Button button3(3, 6, led3, sequence1);
Button button4(4, 8, led4, sequence1);
Button button5(5, 12, led5, sequence1, true);

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  button1.check();
  button2.check();
  button3.check();
  button4.check();
  button5.check();

  if (millis() - lastMillis > 1000) {
    sequence1.check();
    lastMillis = millis();
  }
}

imagine that m_state is HIGH or LOW

    void blink () {
      digitalWrite(3, !m_state);
      delay(100);
      digitalWrite(3, m_state);
    };

When this blink() runs and m_state was initially LOW, then it works, it blinks the led really nice and quickly.

BUT

When this blink() runs and m_state was initially HIGH, then it does not work, it turn the led off and not back on.

I am going insane!!!!

I fixed it by doing this… Still dont understand wtf is going on.

void blink () {
      digitalWrite(m_pin, !digitalRead(m_pin));
      delay(25);
      digitalWrite(m_pin, !digitalRead(m_pin));
    };