0

For a school assignment I need to make a LED blink on/of 3 times then stop. Now I have come up with the following code:

Repeat!

I think it's ''clean'' enough for what it has to do, but I have a feeling that here must be a easier way to make the LED blink 3 times.

Question is: Is there a way to reapeat the part of code 3 times without copy&pasting it?

6
  • 3
    Other than with a loop? Commented Sep 5, 2016 at 17:04
  • Google "for loop C++".
    – Majenko
    Commented Sep 5, 2016 at 17:05
  • @IgnacioVazquez-Abrams The thing is that it has to stop after 'blinking' 3 times. And there is no way to stop the loop, correct me if i'm wrong.
    – Jordakoes
    Commented Sep 5, 2016 at 17:06
  • 1
    And? There are loops that can do that. Or have you not looked at any C++ tutorials yet? Commented Sep 5, 2016 at 17:07
  • @IgnacioVazquez-Abrams Only thing I found so far is a Exit(0); which makes the program stop. But then I still have to copy paste the code 3x becuase the loop otherwise stops after 1 on 1 off loop.
    – Jordakoes
    Commented Sep 5, 2016 at 17:10

3 Answers 3

2

Here you can use a loop. a classic for loop in C++ looks like this

for (int i = 0; i < n; i++)
{
    // content in between these brackets
}

where n is the amount of times you want to loop

you can also instantiate the i before the loop.

int i;
for (i = 0; i < n; i++)
{
    //content here
}

The code below should work. Toy around with it and see what you get.

int ledPin = 13;
int delayPeriod = 500;


void setup(){
    pinMode(ledPin, OUTPUT);

    for (int i = 0; i < 3; i++){
        digitalWrite(ledPin, HIGH);
        delay(delayPeriod);
        digitalWrite(ledPin, LOW);
        delay(delayPeriod);
    }

another approach you can do is using a while loop. These kinds of loops will loop until a given parameter has been met. In your case you want an LED to stop blinking after 3 times.

int start = 0;
int stop = 3;

while (start <= stop){
    //content here
}

While loops will continue to go as long as the parameter is true. This means something like while(1) will continue to run forever.

3
  • Thanks! I tried the Int i; function and it works perfectly!
    – Jordakoes
    Commented Sep 5, 2016 at 17:31
  • @tisaconundrum have you missed an increment or decrement in your while loop? Commented Sep 7, 2016 at 15:34
  • How can I change to code to work with if and else statements, so it wil work in the loop.
    – Jordakoes
    Commented Sep 9, 2016 at 8:45
0

You could also look at optimising the 4 lines of code inside you loop:

const int ledPin = 13;
const int delayPeriod = 500;
const size_t count = 3;

void setup()
{
    pinMode(ledPin, OUTPUT);
    for (size_t loop = 0; loop < (count*2); ++loop) // size_t because variable is never negative.
    {  // Two time count, because you only have half the code
        digitalWrite(ledPin, (loop % 2)?HIGH:LOW);// Use Modulus
        delay(delayPeriod);
    }
}
1
  • Sorry for the late respons! Thanks you for helping out, I will try this methode later tonight! I already made my code work with the previouse answer. This methode may be easier to read for others. Def going to take a look at it. Thanks again, Really appreciate the time and effort you took for responding!
    – Jordakoes
    Commented Sep 8, 2016 at 17:34
0

Another way is to declare a function. This method will give you more chance of using it in various ways.

    int LED_PIN = 13;

    void flashing(size_t times=1) { // By default it will flash once, you can chance this into your desire
        int delayPeriod = 500;
        for (size_t loop = 0; loop < (times*2); ++loop) {
          digitalWrite(LED_PIN, (loop % 2)?HIGH:LOW);// Use Modulus
          delay(delayPeriod);
        } // Thanks to [Matt][1] and [Edgar Bonet][2] for their comments
    }

    void setup() {
        pinMode(ledPin, OUTPUT);
        flashing(3);
    }

    void loop() {
        // Do something here
    }
2
  • I also came up with the idea of using a function, but you have to consider the overhead of calling it, In this case I don't think its worth the cost. Normal coding standards frown upon placing two statements on a single line. Off the top of my head I couldn't say if a while or for was more efficient, but personally for a fixed number of iterations I would go for a for loop, if I had to do a while loop then I would set I to zero so I could do (i<times) which would be quicker than what you have. All that said, its still a good answer. Commented Sep 6, 2016 at 12:02
  • A for loop is definitely better in terms of legibility. Commented Sep 6, 2016 at 12:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.