Go Down

Topic: function to restart looping? (Read 1 time) previous topic - next topic

ase123

hay.. there is function to restart looping

example..

Code: [Select]

int val = analogRead(1);

void loop()
{
digitalWrite(led, HIGH);
delay(1000)
digitalWrite(led, LOW);
delay(1000)
digitalWrite(led2, HIGH);
delay(1000)
digitalWrite(led2, LOW);
if (val == 5)               <============= if val = 5 restart looping from begining
{   restart looping from begining      }
delay(1000)
digitalWrite(led3, HIGH);
delay(1000)
digitalWrite(led3, LOW);
delay(1000)
}


there is function to do it?

marco_c

It is ugly and not recommended but you could use a goto statement. Always better to restructure your code so that the condition that causes the looping is in the loop statement, if that makes sense.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com


accel

Couldn't you just say

if(val !=5){

// all the other stuff

}

Coding Badly

int val = analogRead(1);


Is a bad idea.  The hardware has not been initialized at the point analogRead is called.  Move the initialization to setup where it belongs.

abrookfield

#5
May 19, 2012, 06:36 am Last Edit: May 19, 2012, 06:38 am by abrookfield Reason: 1
Code: [Select]

void loop() {
int loopVal;

while (1){
loopVal=1;

while (analogRead(1)!= 5) {
switch(loopVal) {
case 1:
digitalWrite(led, HIGH);
loopVal++;
case 2:
digitalWrite(led, LOW);
loopVal++;
case 3:
digitalWrite(led2, HIGH);
loopVal++;
case 4:
digitalWrite(led2, LOW);
loopVal++;
case 5:
digitalWrite(led3, HIGH);
loopVal++;
case 6:
digitalWrite(led3, LOW);
loopVal=1;
}
delay(1000);
}
}
}

www.reeftopper.com

ase123

@Arrch

yeah the function return; is work..

thanks for all

robtillaart

inner loop can be shortened by using bitmasks.
Code: [Select]

    while (analogRead(1) != 5) {

switch(loopVal) {
case 1:
digitalWrite(led, HIGH);
loopVal++;
case 2:
digitalWrite(led, LOW);
loopVal++;
case 3:
digitalWrite(led2, HIGH);
loopVal++;
case 4:
digitalWrite(led2, LOW);
loopVal++;
case 5:
digitalWrite(led3, HIGH);
loopVal++;
case 6:
digitalWrite(led3, LOW);
loopVal=1;
}
delay(1000);
}


Code: [Select]

while (analogRead(1) != 5)
{
  if (loopVal & 0x01) digitalWrite(led, HIGH);
  else digitalWrite(led, LOW);
  loopVal++;
  if (loopVal == 6 ) loopVal=1;
  delay(1000);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Code: [Select]
(analogRead(1) != 5)
Am I the only one who thinks this is a really bad idea?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart


yeah 5 is such a magic number, one should use a #define for that ;)

Code: [Select]

#define MAGICNUMBER 5
...

(analogRead(1) != MAGICNUMBER )

but then, it might be just what the OP intended ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

No, not the magicness, just the precision, from an analogue input.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up