Go Down

Topic: Language tips  (Read 143 times) previous topic - next topic

kamronscruggs

Quick question -
Which language allows me to stop a loop from continuing?
I modified the beginner tutorial code for blinking the onboard LED to look like this (a heartbeat):

Code: [Select]
void setup() {

pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){
 
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(400);
digitalWrite(LED_BUILTIN, LOW);
delay(450);
}



I'd like to have the loop run some number of times and then stop.

TKall

New college....i used to live close to new college.

Consider a conditional and get rid of the delay.


vinceherman

Here is the reference for a for loop.

Read through it and see if you can make it work.
Ask questions if you cannot make it work.

But give it a try first.

DVDdoug

#3
Feb 08, 2018, 11:07 pm Last Edit: Feb 08, 2018, 11:08 pm by DVDdoug
A For-Loop is probably what you want.

There are also while-loops and do-while loops.  Most loops are conditional...  They loop until some condition...   The Arduino main loop is an exception.

break (usually along with an if-statement) can break you out of a loop at any time, but usually that's used for "unusual" conditions.

You'll have to consider what happens when your for-loop ends...   You probably don't want to start your main loop over immediately.    Do you want it to freeze-up 'till you reset?   Do you want to wait for a button-push before it starts over?  Just a longer delay?  etc.?

UKHeliBob

Wrap the code in a for loop and put the whole thing in setup()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Jimmus

The loop() function, by definition, loops forever.  If you want it to stop, you can create an infinite delay.  This can be done like this:

Code: [Select]
int count = 0;

void setup() {

pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){
 
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(400);
digitalWrite(LED_BUILTIN, LOW);
delay(450);
++count;
if (count >= 10)  // Or however many you want
  for(;;);
}

SteevyT

Could also do something like this to make it restart with a button press.  Button would be wired between digital pin 2 and ground.

Code: [Select]
int count = 0;
byte buttonIn = 2; //byte because an Arduino has less than 255 pins

void setup() {
   pinMode(buttonIn, INPUT_PULLUP);
   pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){
   digitalWrite(LED_BUILTIN, HIGH);
   delay(100);
   digitalWrite(LED_BUILTIN, LOW);
   delay(50);
   digitalWrite(LED_BUILTIN, HIGH);
   delay(400);
   digitalWrite(LED_BUILTIN, LOW);
   delay(450);
   ++count;


   while (count > 10){
      if (digitalRead(buttonIn) == LOW){ //looking for low because pin is high when button not pressed
         count = 0;
      }

   }

}
I might not know what I'm doing.

kamronscruggs

Quote
Do you want it to freeze-up 'till you reset?   Do you want to wait for a button-push before it starts over?  Just a longer delay?  etc.?
I figure a reset initialization would be fine in my case. I also figure that this project is about getting good about working with a coding language, and my goal is to understand how to tell Arduino what to do about processes and variables.


When I consider a conditional, then the number of times the loop goes is the variable, and the condition has to be false when that variable exceeds 1. That's when the loop ends. Then I do not know how to tell Arduino that the loop needs to be counted and that number to be considered the variable. Is there a return integer for the number of times a loop cycles?

Setting up the code in the void setup() works for a single run. It's cool, but I think I want to adjust the number of times before the loops stops.

I didn't see what count was until I tried what SteevyT suggested and added a button. I get that count is exactly what (it sounds like) I was looking for! And the experiment worked. Thank you all.

UKHeliBob

Quote
When I consider a conditional, then the number of times the loop goes is the variable, and the condition has to be false when that variable exceeds 1
It's not going to be much of a loop if it stops when the variable exceeds 1

What is wrong with using a for loop ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up