Troubleshooting given code for dual servo operation

For an assignment we must make two servos sweep while a photocell attached to one senses a light source and stops the loop when it’s pointed at a light. Right now i’m just trying to get the base code we were given to work right. For some reason only one of the servos will move (sero 2, connected to pin 11). I’m trying to figure out why the other servo won’t move. I do not have the code for stopping the loop with the sensor yet.

// Light Detector (2 D)
//
// This program continuously sweeps a servo back and forth while making
// readings from a connected photocell.
// 

#include <Servo.h>
const int SENSOR = 3;
int val = 0;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int pos1 = 0; // variable to store the servo1 position
int pos2 = 0; // variable to store the servo2 position

void setup() {
 myservo1.attach(10);
 myservo2.attach(11);
 Serial.begin(9600);
}
void loop() {

  for(pos1 = 0; pos1 < 180; pos1 += 1) {
    myservo1.write(pos1); //position servo
    for(pos2 = 0; pos2 < 180; pos2 += 1) {
      myservo2.write(pos2); //position servo
      delay(10);
      val = analogRead(SENSOR); //read current sensor value
      Serial.println(val); //print it via serial port
   } // for pos2
    for(pos2 = 180; pos2 >=0; pos2 -= 1) {
     myservo2.write(pos2); //position servo
     delay(10);
     val = analogRead(SENSOR); //read current sensor value
     Serial.println(val); //print it via serial port
   } // for pos2
  } // for pos1
 
  for(pos1 = 180; pos1 >=0; pos1 -= 1) {
    myservo1.write(pos1); //position servo
    for(pos2 = 0; pos2 < 180; pos2 += 1) { 
      myservo2.write(pos2); //position servo
      delay(10);
      val = analogRead(SENSOR); //read current sensor value
      Serial.println(val); //print it via serial port
    } // for pos2
  for(pos2 = 180; pos2 >=0; pos2 -= 1) {
    myservo2.write(pos2); //position servo
    delay(10);
    val = analogRead(SENSOR); //read current sensor value
    Serial.println(val); //print it via serial port
  } // for pos2
  } // for pos1 
}

I suspect both servos are moving but servo1 very slowly. Because you have nested the for loops servo1 moves 1 degree, then servo2 moves the full 180 and then back then servo1 moves another 1 degree etc. Was that really what you meant it to do?

Steve

That makes sense. I just increased the increment amount of servo 1 and noticed the movement. Thank you very much

Hi,
So you have;

  • One LDR connected to an input, Pin3.
  • One servo connected to an output Pin10.
  • One servo connected to an output Pin11.

When the LDR is de activated do you want both servos to sweep.
When the LDR is activated do you want both servos to stop sweeping.

When the servos sweep , do you want them to sweep together, or at different rates or in opposing directions.

If sweeping together then;

  • You only need one position variable.
  • You only need one for... loop to sweep both one way,
  • You only need another single for.. loop to sweep both back the other way.

There is no need to nest your for.. loops...

Tom... :slight_smile:

The goal is to have the LDR move around by the servos and stop when its pointing at a light source strong enough to trigger it. My professor’s instructions were a bit unclear, but by the way his provided base code works, the base servo moves a bit, waits for the top servo (with the LDR) to do a full sweep (0-180-0) and then increment another small degree. In which case I think the nested for loops are required.

Slipstick was right and the base servo was only moving 1 degree after every sweep and I just didn’t see that, and after increasing the increment amount, I could see the movements working properly. I then added a while loop to stop the loop when the LDR sensor value was high enough, per the assignment’s instructions.

Hi,

The two servos are mechanically separate from each other;

The LDR is mounted on Servo1 arm.

So Servo1 mechanically sweeps the LDR.
You monitor the LDR output and log the highest light output and at what servo position it is at.

Then you sweep Servo2 to that position.

All the time Servo1 keeps sweeping and updating the Servo2 position.

Basically a Target and Aim system.

Have you looked at the Servo sweep example in the IDE?

Tom... :slight_smile: