Society of Robots - Robot Forum
Software => Software => Topic started by: Treetpflyer on December 14, 2008, 03:06:19 PM
-
Hi all,
I've been working with the Arduino code. It seems different than just normal C. I have a normal hobby servo setup and it's working well. I would like to insert a switch in the loop code that will allow me to start and stop the loop(on and off). In the past I've used a goto end statment, but that was years ago and I'm still learning to write code. When I try to compile it, it gives me an error of "error: 'end' was not declared in this scope" This is just a copy paste of the servo code from Ardiuno website with some changes for the switch.
Is there something better I can use? Thanks for your help.
The code is as follows:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int switchPin = 2; // start stop switch
int val2 = 0; // variable to read the value from the start/stop pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(switchPin, INPUT); // declare switch as input
}
void loop(){
val2 = digitalRead(switchPin); // read input value of switch
if (val2 == HIGH) { // check the switch position
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(0 ); } // waits for the servo to get there
if (val2 == LOW); { // check the switch position
goto end; // skips over the servo code and goes to end, then repeats
}
end;
}
-
Well, you need to declare a label called "end". You have a semicolon ";" after the word end" where it looks like you want the label to be and it needs a colon ":".
Change ->
end;
to
end:
where you want the label, but NOT on the goto. Can you see the difference? Sometimes it's hard for us to see, but not to the compiler.
Doug
-
Greetings,
In general using goto in C is a bad idea.
Rather than stopping the loop, keep looping but just let it do nothing.
If I understand your goal as being do not execute the servo code when the switch value is LOW. Then all you need to do is eliminate the second if statement.
Sincerely
Kirk