How to use "if" statements and counters

Hey.

I am brand new to Arduino and writing code. I am currently in week two and I cannot figure out how to use "if" statements and counters.

These are the instructions for my current assignment.

  1. Construct the circuit on one half of the breadboard.
  2. Using the Arduino IDE, create a program that uses digitalWrite command to turn the LED on and off in the following manner
    a. 1 second on 1 second off for 3 blinks
    b. .5 seconds on .5 seconds off for 3 blinks
    c. .25 seconds on .25 seconds off for 3 blinks
    d. .1 seconds on .1 secons off for 6 blinks
    e. Repeat cycle

I can make this cycle happen if I write out the code like this.

int led = 13;
void setup() {
// put your setup code here, to run once:
pinMode (led, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite (led, HIGH);
delay (1000);
digitalWrite (led, LOW);
delay (1000);
Then repeat what is above as many times as needed with changes in delay.

The teacher, however, wants us to incorporate "if" statements and counters. He didn't go very in depth about it so I was hoping someone here could walk me through how you would write it. I don't care so much for the answer as I do to actually learn how to write this myself.

Thank you in advance.

The teacher, however, wants us to incorporate “if” statements and counters.

OK. Lets start with what you are counting. You can’t create a counter, and increment it at the right point, without knowing what you are counting.

Now, presumably you are counting how many times the LED has changed state. When the Arduino starts up, that count would be 0.

So, you have a global variable:

byte ledBlinkCount = 0;

In loop(), you’d have:

void loop()
{
   if(ledBlinkCount == 0)
   {
      for(byte b=0; b<3; b++) 
      {
         digitalWrite(somePin, HIGH);
         delay(1000);
         digitalWrite(somePin, LOW);
         delay(1000);
         ledBlinkCount++;
      }
   }
   else ...

Now, you try writing the rest of the code. By the time that code ends, ledBlinkCount will be 3, because the LED has blinked three times. So, an else if(ledBlinkCount == 3) seems appropriate.

In the body of that statement, you make the LED blink 3 more times, at 500 millisecond intervals.

The rest of the code is pretty easy, then.