system
January 31, 2010, 12:58pm
1
Hello,
I am interfacing a servo motor with two photo sensors to create a solar tracker. If I comment out the second while statement it works great, but only in one direction. Basically the servo is setup to start at position 1 and incriment every 200m while pin0 > pin1. What I need however is the servo motor to then incriment -1 while pin0 < pin1. Here is the code.
#include <Servo.h>
Servo myservo;
const int pin0 = 0;
const int pin1 = 1;
int angle = 1;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
}
void loop()
{
Serial.print(analogRead(pin0));
Serial.print(",");
Serial.print(analogRead(pin1));
Serial.print(",");
Serial.println(angle);
while ((analogRead(pin0)) > (analogRead(pin1)));
{
myservo.write(angle);
angle = (angle + 1);
delay(200);
}
while ((analogRead(pin0)) < (analogRead(pin1)));
{
myservo.write(angle);
angle = (angle - 1);
delay(200);
}
}
And the setup.
The photo sensors are inside the copper tubing for isolation.
Thanks!!!
system
January 31, 2010, 1:22pm
2
What actually happens if you don't comment out the second while statement?
system
January 31, 2010, 1:46pm
3
What actually happens if you don't comment out the second while statement?
The servo locks up at position 1
You should move the myservo.write() statements after the update to angle, so you're not doing a "ready-fire-aim" loop.
system
January 31, 2010, 2:02pm
5
while ((analogRead(pin0)) > (analogRead(pin1)));
{
myservo.write(angle);
angle = (angle + 1);
delay(200);
}
The while loop does nothing, because of the ; on the end.
This is then followed by some code enclosed in unnecessary curly braces.
Remove the ; at the end of the while statements.
Just out of curiosity: what happens if you increase the delays inside the loops?
If I would be you, I would put a wrapper around the analogRead statements and track the results with Print statements. This should shed light onto the strange behaviour.
Cheers, Udo
system
January 31, 2010, 2:17pm
7
I think I can guess what's happening the while conditions are failing everytime. The only reason it seems to work if you comment out the second while statement is because it's being looped as part of the main loop. With both while statements - they cancel each other out.
system
January 31, 2010, 2:19pm
8
D'oh - PaulS beat me to it!
system
January 31, 2010, 2:46pm
9
Ok I made the suggested edits.
#include <Servo.h>
Servo myservo;
const int pin0 = 0;
const int pin1 = 1;
int angle = 1;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
}
void loop()
{
Serial.print(analogRead(pin0));
Serial.print(",");
Serial.print(analogRead(pin1));
Serial.print(",");
Serial.println(angle);
while ((analogRead(pin0)) > (analogRead(pin1)))
angle = (angle + 1);
myservo.write(angle);
delay(200);
while ((analogRead(pin0)) < (analogRead(pin1)))
angle = (angle -1);
myservo.write(angle);
delay(200);
}
It is still locks up and spits out a couple lines of crazy serial data
815,599,1
655,780,20323
911,784,16042
system
January 31, 2010, 3:34pm
10
The curly braces are not optional after the while statement, if you want more than one line of code executed.
The way you had it before was like this:
while(value1 < value2)
; // do nothing
{
// do this
// and this
// and this
}
Now, what you have is this:
while(value1 < value2)
// Do this
// When the while loop is done, do this
// and this
// and this
What you want is this:
while(value1 < value2)
{
// Do this
// and this
// and this
}
system
January 31, 2010, 5:09pm
11
Ok, I tried this
#include <Servo.h>
Servo myservo;
const int pin0 = 0;
const int pin1 = 1;
int angle = 1;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
}
void loop()
{
Serial.print(analogRead(pin0));
Serial.print(",");
Serial.print(analogRead(pin1));
Serial.print(",");
Serial.println(angle);
while ((analogRead(pin0)) > (analogRead(pin1)))
{
angle = (angle + 1)
myservo.write(angle)
delay(200)
}
while ((analogRead(pin0)) < (analogRead(pin1)))
{
angle = (angle -1)
myservo.write(angle)
delay(200)
}
}
And got this
I thought maybe it had somthing to do with the servo library so I deleted that line of code and ran it again. It gave the same error in the same spot.
Any thoughts?
you forgot a ton of semicolons
while ((analogRead(pin0)) > (analogRead(pin1)))
{
angle = (angle + 1);
myservo.write(angle);
delay(200);
}
while ((analogRead(pin0)) < (analogRead(pin1)))
{
angle = (angle -1);
myservo.write(angle);
delay(200);
}
system
January 31, 2010, 5:35pm
13
I got it !!!
Thanks for all the help Dave, Paul, Udo, Ran and Weirdo! I'ts working great, I'll post the schematic and a link to the youtube video soon.
#include <Servo.h>
Servo myservo;
const int pin0 = 0;
const int pin1 = 1;
int angle = 1;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
}
void loop()
{
Serial.print(analogRead(pin0));
Serial.print(",");
Serial.print(analogRead(pin1));
Serial.print(",");
Serial.println(angle);
while ((analogRead(pin0)) > (analogRead(pin1))){
angle = (angle + 1);
myservo.write(angle);
delay(20);
}
while ((analogRead(pin0)) < (analogRead(pin1)))
{
angle = (angle -1);
myservo.write(angle);
delay(20);
}
}