Servo.read function not working

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);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

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)

3 Likes

Assuming you are using garden variety inexpensive servos, they are running open loop.

The read method only returns what you wrote, it cannot form the basis of any kind of closed loop control system.

a7

1 Like

Did you mean to write this?
`val = myservo.read();'

1 Like
  Servo.read (val)

The Servo read() function does not take a parameter but it does return a value

  int val = myservo.read();

Note the use of the name you gave the servo when using the read() function

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

I don't think so, I tried changing it to this but it doesn't seem to work. I just want it to display the angle of the servo on the serial monitor

The documentation provided says to use Servo.read () but doesn't elaborate on how to use it. Also I have formatted the post to make it easier to read

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

Are you running the code or still stuck with compiler errors?

Doesn't seem to work.

You could say what it does that it should not, or,does not do that it should.

a7

Take a close look at reply #6

So I tried using
int val = myservo.read();

But the serial monitor is still not displaying angle

À Serial.begin(115200); in the setup and a print in the loop would help with that

So this is what the code looks like now, unfortunately the angle is still not being displayed in the serial monitor.

#include <Servo.h>
Servo myservo;
int potpin = A0;
int ServoPin = 7;
int val;
void setup() {
  myservo.attach(ServoPin);
Serial.begin(115200);
}
void loop() {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);
  myservo.write(val);
  int val = myservo.read();
Serial.println();
  delay(15);
}

What do you think this is printing ?

Also as you’ve been told the read will just return the value you passed with write.

That is because you are not printing it

Serial.println(myservo.read());

  myservo.write(val);
  Serial.println(val);

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.

See (first Google hit) https://www.instructables.com/Servo-Feedback-Hack-free/ or similar (tons of tutorials on line about this)