controling two servos with light sensor

Hi!

I am just starting with Arduino and I don't succeed in controlling two servos with a light sensor.

I build one circuit with one servo controled by one light sensor through arduino that is working fine. The coding is the following:

#include <Servo.h>
Servo myservo; // create servo object to control a servo

int ldr = 2;
int ledPin = 13;
int val = 0;

void setup(){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin,OUTPUT);
//open the serial port at 9600 bps;
Serial.begin(9600);

}

void loop(){
//myservo.write(val);
val = analogRead(ldr);

if (val < 100){ //making minimum value 100
val = 100;
}
if (val > 1000){ //making max value 1000
val = 1000;
}

val = val - 100; // so that numbers range from 0 - 900

val = val/5; // so that val is now between 0-180

//Serial.println(val);

myservo.write(val);

}

I have tried to adapt the scripting in order to do the same with two servos but I didn't succeed and don't understand why. I am new to the game so I would really appreciate if someone could give me a hand to find out where the problem is coming from.

Here is my second attempt of scripting for controlling two servos with light sensor:

#include <Servo.h>
Servo myservo1; // create a servo object to control servo1
Servo myservo2; // create a servo object to control servo2

int ldr =2;
int ledPin = 13;
int val = 0;

void setup(){
myservo1.attach (8);
pinMode(ledPin,OUTPUT);
myservo2.attach (9);
pinMode(ledPin, OUTPUT);
Serial.begin (9600);

}

void loop(){
val = analogRead(ldr);

if (val < 100){
val = 100;
}
if (val > 1000){
val = 1000;
}

val = val - 100;

val = val/5;

myservo1.write(val);
myservo2.write(val);

}

Anyone interested in helping me? thanks by advance

ps: can send pics by email of the circuit at current stage too...

Hi, what the question ?

here it is !

In what way does your sketch not work?

well only servo 1 is working, the second one doesn't move at all, but it might rather be a problem of electronic circuit than scripting but I didn't manage to post the pictures of the circuit online and I wanted at least to check is the code was fine...

does it make sense?

The servos only work on pins 9 and 10 with that library, see:

Try changing pin 8 to pin 10

Hey indeed, it is working now. Thanks a lot!

May I ask you if there is any chance to build a circuit with more than two servos in that case?

The ServoTimer2 library drives up to 8 servos: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230479947

Its use is similar but the values given to servo write are microseconds not degrees. So to move the servo in response to values from analogWrite you would do something like:
val = analogRead(ldr);
myservo1.write(val + 1000); // this drives the servo from 1000 to just over 2000 us

The thread has more info including a tip on how to use the arduino map function if you want to adjust the servo range.

cool! I will try that. Many thanks for your help.

Hello,
i plan to build a robot (light sensor movement solution) and found this project here and as you asked for servos - this board is designed for this in particular.

The Roboduino is a Freeduino (Arduino software compatible) microcontroller board designed for robotics. All of its connections have neighboring power buses into which servos and sensors can easily be plugged. Additional headers for power and serial communication are also provided. The kits come with the surface mount parts pre-soldered. Skill level: Beginner to intermediate.
http://curiousinventor.com/kits/roboduino

hello roboter, it looks like the only advantage of that board over a standard arduino board are the pin headers for connecting the servos. $30 dollars seems a lot of money to save the trouble of wiring up some pin headers for the servos. Am I missing something?

Hi mem,
well i dont know, but i thought this is the best way to start off for a newbie as im and the reason is that www.ArduinoFun.com uses this model aswell.

But im open for ideas, i will propably wait 2 more weeks till i decide which board i use.