I have been given a project by my uni to create a photometer prototype using a servo motor.
The initial stages required me to use a potentiometer to control the angle of the servo between 0 - 180 degrees, this was not a problem.
However my lecturer also wants me to use the "Servo.read" function to compare the angle displayed through the serial monitor against the physical angle of the servo measured on my protractor and I keep getting error messages when I try to use said function.
Any advice would be greatly appreciated
Code is below:
#include <Servo.h>
Servo myservo;
int potpin = A0;
int ServoPin = 7 ;
int val;
void setup() {
myservo.attach(ServoPin);
}
void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
Servo.read (val)
delay (15);
}
may be if you were using read() as per the documentation you'd be better off?
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)
Yes at this stage all I am trying to do is get the serial monitor to display the angle of the servo so I can compare it against the physical angle shown against my protractor
Correction: the documentation says to use servo.read(), where servo is "a variable of type Servo" (in the "Parameters" section). Capitalization matters in Arduino (which is really C++ with stuff on top).
Tell your lecturer that read() does not return the real angle reached by the servo but what it should be. That is if you tell the servo to go to 70° (with a write(70)) then if you ask (with a read()) what’s the position then you’ll get 70° even if something was blocking the servo say at 60°.
So if you want to measure the angle in real world you need a different device (an encoder on the shaft or a potentiometer) that would give you feedback.
It’s possible to hack some servos (by tapping into the servo's own internal potentiometer) to get that feedback but you won’t read the position with a call to read() on the servo.