jump fuction after the if statement

Hi all,
please help me with my current prob. I'm using Arduino Duemilanove and 1 IR analog Distance sensor to control 1 servo and 1 LED light. I wanna use "goto" function to jump to the command (case #) that i desired. However, the the jump function didn't work very well. It still works in sequence which is not what i want. i want it to be able to jump to a certain place after the comparison statement.
Below is apart of my source to discrible the situation. Anyone can point out the problem, i'd be much appreciated.

LL.guy


#include <Servo.h>

void setup() {

void loop()
{
while(analogRead(analogPin_flush))
{
if(analogRead(analogPin_flush)<=300) // if IR sensor is touched
{ goto case1;}
else // if IR sensor is not touched
{goto case2;}
}
case1:
//***turn servo 90 degrees
while(analogRead(analogPin_servo)<=100)
{delay(1500);
}
for(pos = 180; pos>70; pos -= 1)
{
myservo.write(pos);
delay(5);
}
for(pos = 70; pos<=180; pos+=1)
{
myservo.write(pos);
delay(10);
if(pos==180){break;}
}

//*turn on LED
case2:
while(analogRead(analogPin_light)<=300)
{
}
digitalWrite(digitalPin_light, LOW);
delay(1500);
}

I wanna use "goto"

Don't.
Just...don't.

Use of "goto" leads to premature hair-loss.[edit]BTW, sp. "want to"[/edit]

Use a plain function call.

Structured programming does not require the use of goto. As a new programmer, you should forget that you ever heard that C supports the goto command.

Your program can be structured without the use of the goto, quite easily, by using functions.

I'd show you how, except that I started to convert the case 1 code to a function, and I went Huh?

while(analogRead(analogPin_servo)<=100)
{delay(1500);
}
for(pos = 180; pos>70; pos -= 1)
{
myservo.write(pos);
delay(5);
}
for(pos = 70; pos<=180; pos+=1)
{
myservo.write(pos);
delay(10);
if(pos==180){break;}
}

analogPin_servo is not defined. Even if it was, it's a really poor name. The name implies that there is a servo attached to the pin, and that you are trying to read the value of the servo. Clearly, that is not what you are trying to do. Is it?

When the sensor value is less than 101, for whatever kind of sensor is being read, you want to do nothing except twiddle your thumbs for 1 second and a half. This will make yor whatever pretty unresponsive. Much better to do something only when the sensor is above some value.

The first for loop is going to turn the servo from 180 to 70. The second loop is going to turn the servo back to 180. The loop will end when pos gets to 180, so the if/break statement is not going to do anything.

Please explain what case 1 and case 2 are supposed to do, and under what circumstances they should be executed, and we can help you write understandable code to accomplish those goals.

Without using goto.

while(analogRead(analogPin_flush))

You can skip that one too, loop is called over and over and you probably do not want the program to do something else if analogRead() happened to return 0.

//*******************************************************
#include <Servo.h> // Or whatever library you are going to use

//
// Declare an int or #define analogPin_whatever to be whatever you want to connect
//

//
// The following is executed exactly once at the beginning
// of the main() function supplied by the Arduino IDE.
//
void setup()
{
    // Initialize whatever you need to.
}

//
// The following is executed repeatedly by the
// main() function supplied by the Arduino IDE.
//
void loop()
{
    if (analogRead(analogPin_whatever) <= 300)
    {

        // Do whatever you need to do for a low value on the analog input pin.

        // Maybe some delay here (or not)

    }
    else
    {

        // Do whatever you need to do for a high value on the analog input pin.

        // Maybe some delay here (or not)

    }

    // Maybe some delay here (or not)

}

Regards,

Dave

I did write the if statements before. It was suposed to "do sumthing" when the condition is meet, else will just skip the excecuting line. However, in practice, the line didn't excecuted just keep waiting to be excecuted (you know wat i meant - it didn't skip when the condition is not meet. It just keep waiting to be meet). Therefore, the program i didn't go through. Hence, i was looking for the jump function which is "goto". :slight_smile:

Be4, my if statement lines were:
********Turn servo condition
if(analogRead(analogPin_flush)<=300)
{
for(pos = 180; pos>70; pos -= 1)
{
myservo.write(pos);
delay(5);
}
for(pos = 70; pos<=180; pos+=1)
{
myservo.write(pos);
delay(10);
if(pos==180){break;}
}
}


what i wanted to do is if the IR sensor is not sensing anything, just skip the line to the next line. Howver, it just keep waiting for the condition to be meet instead of passing. I don't know why.

Thanks for your time and patient !

Have a gander at http://arduino.cc/en/Tutorial/IfStatement

Then put something like the following in your loop() function

    if (someLogicCondition)
    {
       // do stuff if the condition is true
    }
    [glow]else[/glow]  //<=== This is important!
    {
       // do stuff if the condition is false
    }

Regards,

Dave