First off, I'm new here, so I apologize in advance if this is the wrong place to post this.
Secondly, I'm also almost completely new to arduino programming, so in your explanations, please try and talk as if I am a beginner.
I'm trying to get a servo to turn in to a certain spot depending on when a light source points to one of three photocells. (First photocell to the left, Second photocell to the center, Third photocell to the right.)
I used a schematic and code from this one site
to get it to move the servo, but only with one photocell, and it's when the photocell is lacking light that the servo will move, I'd like the opposite.
First thing I wanted to try was see if I could get it to work with two photocells, so I plugged in another photocell+resistor combo and put one of the photocell legs to Analog Pin 2. I added a section into the void loop code to let the board pick up analog pin 2 as well.
Here is the new code:
#include <Servo.h>
Servo myservo;
int val;
void setup()
{
myservo.attach(12);
}
void loop()
{
val = analogRead(0);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
val = analogRead(2);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
While it does work, the servo starts shaking really badly, and I'm not sure if it's a power problem, a cheap servo problem, or if I did something incorrectly. The servo is one that came with my kit, and it says "VILROS MicroServe 9g SG90" on it. I know absolutely nothing about servos, but I think the 90 means it can only turn 90 degrees?
Anyways, my questions are:
- How can I get it to stop shaking?
- Am I doing the right thing by adding another photocell, or is there another way to do it?
- How can I get it to move the servo to a certain spot if there's a bright light source detected on it?
I would greatly appreciate any help at all. ![]()
