Here is my code so far. ive gotten the servo to turn on but the servo doesn't turn. Im using Arduino uno. Any advice???
#include <Servo.h>
int servoPin=9;
int lightVal;
int servoPos = lightVal;
Servo myServo;
int lightPin = A0;
int DT=250;
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
pinMode(lightPin,INPUT);
}
void loop() {
lightVal=analogRead(lightPin);
delay(DT);
Serial.print(" the servo is at angle ");
Serial.println(lightVal);
while (Serial.available() ==0) {
servoPos=analogRead(lightVal);
}
myServo.write(servoPos);
}
servoPos=analogRead(lightVal); makes no sense. lightVal isn't a pin number.
Steve
blh64
May 10, 2020, 6:12pm
3
nhorn12:
Here is my code so far. ive gotten the servo to turn on but the servo doesn't turn. Im using Arduino uno. Any advice???
Yes, please read the sticky post at the top of the forum about how to properly post your code using code tags. It helps people help you
#include <Servo.h>
int servoPin=9;
int lightVal;
int servoPos = lightVal;
Servo myServo;
int lightPin = A0;
int DT=250;
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
pinMode(lightPin,INPUT);
}
void loop() {
lightVal=analogRead(lightPin);
delay(DT);
Serial.print(" the servo is at angle ");
Serial.println(lightVal);
while (Serial.available() ==0) {
servoPos=analogRead(lightVal);
}
myServo.write(servoPos);
}
You probably want
servoPos=lightVal;
instead of
servoPos=analogRead(lightVal);
The latter one doesn't make any sense, have a look at the API for analogRead.
Also, think about the ranges of values for lightVal and servoPos. Probably you want to perform some mapping between them.