counter++ question

Hi all,

I am using this sketch of the forum to vary the flash interval of an LED. I added it twice in the sketch for two two different intervals depending on a switch input. The switch is for testing. I am actually going to EEPROM read, and if the value is zero it will do the short flash and anything else I want to flash that number of times, then pause and keep doing that until the main switch in the loop becomes active.

I tried to add a counter and implement a different offTime in another if statement at the end but it didn’t work. I don’t think I’m far off, but we’ll see.lol Also, I’m a rookie, so if I have more code then I need, feel free to let me know.
Thanks, Randy

const int led=  8;      // the number of the LED pin
const int button = 9;

// Variables will change:
int counter = 0;
int ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0;        // will store last time LED was updated

const unsigned long onTime1 = 150UL;
const unsigned long offTime1 = 1500UL;
const unsigned long onTime2 = 250UL;
const unsigned long offTime2 = 250UL;
const unsigned long offTime3 = 1000UL;

long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  if (digitalRead(button) == HIGH) {
    // blink the LED.
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        interval = onTime1;
        ledState = HIGH;
      }

      else {

        interval = offTime1;
        ledState = LOW;
      }
      // set the LED with the ledState of the variable:
      digitalWrite(led, ledState);
    }
  }
  
  if (digitalRead(button) == LOW && counter < 3) {
    // blink the LED.
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        interval = onTime2;
        ledState = HIGH;
        counter++;
      }

      else {

        interval = offTime2;
        ledState = LOW;
      }
      // set the LED with the ledState of the variable:
      digitalWrite(led, ledState);
    }
    
    if (digitalRead(button) == LOW && counter == 3) {
      interval = offTime3;
      ledState = LOW;
    }
    digitalWrite(led, ledState);
    counter = 0;
  }
}

Define "it did not work" ;)

Your current code (by the looks of it) 1) if the button reading is HIGH, flashes a led with onTime1/offTime1 indefinitely 2) if the button reading is LOW, flashes a led 3 times with onTime2/offTime2 and next will no longer do anything till you release the button

After releasing the button it will do (1) again but when you press it, it will not change to (2) because counter is still 3.

You probably want to reset your counter to 0 in the last if and you might also want to reset your counter when the button is HIGH.

Hi sterretje, thanks for looking and replying. I didn't realize I took the reset out before I copied it for the post. I edited the original post to show where and how I had it. You are correct in your description of the sketch. I want it to do three flashes, pause, three flashes, pause and keep going unless the button is released

With the counter reset in, it just keeps going. I tried putting it in the controlling if statement and other places, but no matter where I put it, it just flashing with out a longer pause and the counter is resetting. Additionally the last interval3 offTime doesn't seem to do anything. Thats where I was hoping to put a separating pause between flashes. I can't seem to get my three flashes, pause and repeat.

Thanks, Randy

Place counter = 0; inside the last if block.

With your current code, it will never reach three as you increment and next reset to zero.

PS
When posting updated code, it’s in general appreciated to post it in a new post; don’t modify code that already was posted. In this case I think it does not matter but as a general ‘rule’ it stands.

sterretje, thanks a bunch!

sterretje: Place counter = 0; inside the last if block.

Thats all it took!

I do have one more question though. If my offTime and onTime are controlling the flash rate. What is interval doing?

I sure appreciate the help, Randy

Not quite sure what you’re asking but the interval holds the onTime or offTime that is currently in use for a specific ‘delay’

I see.... I'm redefining for the different intervals, but it is initiated as a long interval = 1000. I wonder if I really need to assign it a number or just initiate it like "long interval;". Thanks Randy

If you don't give it an initial value, the initial value will be 0 because global variables are initialised with 0.

You can test what happens if you make it 0 or leave the initialisation out ;) The only difference will be in your first 'timing' if; instead of waiting 1 second, you now wait 0 seconds.