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