While loop question

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!!!

What actually happens if you don't comment out the second while statement?

 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.

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

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.

D'oh - PaulS beat me to it!

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

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
}

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

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

}

Fritzing schematic.