Pages: [1]   Go Down
Author Topic: function to restart looping?  (Read 778 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hay.. there is function to restart looping

example..

Code:
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?
Logged

Sydney, Australia
Online Online
Edison Member
*
Karma: 27
Posts: 1185
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
return;
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Couldn't you just say

if(val !=5){

// all the other stuff

}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
}
}
}

« Last Edit: May 18, 2012, 11:38:35 pm by abrookfield » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Arrch

yeah the function return; is work..

thanks for all
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 inner loop can be shortened by using bitmasks.
Code:
    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:
while (analogRead(1) != 5)
{
  if (loopVal & 0x01) digitalWrite(led, HIGH);
  else digitalWrite(led, LOW);
  loopVal++;
  if (loopVal == 6 ) loopVal=1;
  delay(1000);
}
Logged

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
(analogRead(1) != 5)
Am I the only one who thinks this is a really bad idea?
Logged

"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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


yeah 5 is such a magic number, one should use a #define for that smiley-wink

Code:
#define MAGICNUMBER 5
...

(analogRead(1) != MAGICNUMBER )
but then, it might be just what the OP intended ...
Logged

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, not the magicness, just the precision, from an analogue input.
Logged

"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.

Pages: [1]   Go Up
Jump to: