Blink n times

I have an Arduino Uno. I want to write a program that makes the on-board LED connected to pin 13 blink n times and then stop. Here's my code

int blinkSpeed = 1000;
int pin = 13;
int n = 3;
int counter = 0;

void setup() {
  pinMode(pin, OUTPUT);
}

void loop() {
  if (counter < n) {
      digitalWrite(pin, HIGH);
      delay(blinkSpeed);
      digitalWrite(pin, LOW)
      delay(blinkSpeed);
  }
  counter++;
}

The above code causes the LED to blink indefinitely.

Interestingly, when I moved counter++; to the last line of the if block, it got the expected behavior. Why would that make a difference?

Put the statement back where it didn't work and add a few Serial.println()'s to the code and see why it wouldn't work.

I lied slightly when I said I had an Arduino. We are using them in class, and I am no longer in class. But I didn't know it was possible to print things out. That command will definitely come in handy next time.

No glaring issues, then?

look at data type

for your counter

else [that is, if counter is >= n] reset counter to 0

--Michael

I should have been clearer. You also have to move counter++ inside your "if" brackets:

  if (counter < n) {
      digitalWrite(pin, HIGH);
      delay(blinkSpeed);
      digitalWrite(pin, LOW)
      delay(blinkSpeed);
      counter++
  }
  else { counter = 0; }

--Michael

siutoejai:
look at data type

for your counter

Shouldn't the count of blinks be represented as an integer?

mjward:
I should have been clearer. You also have to move counter++ inside your "if" brackets:

  if (counter < n) {

digitalWrite(pin, HIGH);
      delay(blinkSpeed);
      digitalWrite(pin, LOW)
      delay(blinkSpeed);
      counter++
  }
  else { counter = 0; }




--Michael

I don't want to ever reset my counter. I want the LED to blink thrice and stop blinking. However, the code I posted was not achieving that for some reason; it was instead causing the LED to blink indefinitely.

I see I misunderstood.

You still have a problem with "counter" though. Once the "if" condition is exceeded, there's nothing to stop the processor from whipping through your loop at full speed 32,767 times (limit of an int) until the variable rolls over and you start blinking again.

There's a quick and dirty solution if you don't want your Arduino to do anything once the 3 blinks are over. Is that the case?

--Michael

mjward:
You still have a problem with "counter" though. Once the "if" condition is exceeded, there's nothing to stop the processor from whipping through your loop at full speed 32,767 times (limit of an int) until the variable rolls over and you start blinking again.
--Michael

Ah, that makes a lot of sense. I can just use a flag, which gets set to false when n is reached the first time. I could also just stick the counter++; inside the loop. Thanks!

A "one shot blinking" or "one shot flashing" that shall occur only a certain number of times when a momentary switch button is pressed, or some other action occurs, can also be done without delay() so it is non-blocking, and also with different LED on and LED off times. Maybe the code below is useful for some other beginners.

const byte pinMomentarySwitch = A0;
bool momentarySwitchTriggered = false;
bool lastMomentarySwitchState = HIGH;

const byte pinLED = 13;
bool stateLED = LOW;
unsigned long timeNowLED = 0;
unsigned long timeIntervalLED = 0;

void setup()
{
  pinMode(pinLED, OUTPUT);
  pinMode (pinMomentarySwitch, INPUT_PULLUP);
}

void loop()
{
  readMomentarySwitch();

  if (momentarySwitchTriggered == true)
  {
    flash(300, 150, 5);
  }
}

void flash(int timeon, int timeoff, byte flashes)
{
  static byte counter = 0;

  if (millis() - timeNowLED > timeIntervalLED)
  {
    timeNowLED = millis();

    if (stateLED == LOW)
    {
      timeIntervalLED = timeon;
      stateLED = HIGH;
      digitalWrite(pinLED, stateLED);
    }

    else
    {
      timeIntervalLED = timeoff;
      stateLED = LOW;
      counter++;
      digitalWrite(pinLED, stateLED);
    }
  }

  if (counter >= flashes)
  {
    counter = 0;
    momentarySwitchTriggered = false;

    return;
  }
}

void readMomentarySwitch()
{
  byte momentarySwitchState = digitalRead (pinMomentarySwitch);

  if (momentarySwitchState != lastMomentarySwitchState)
  {
    lastMomentarySwitchState = momentarySwitchState;

    if (momentarySwitchState == LOW)
    {
      momentarySwitchTriggered = true;
    }
  }

  return;
}