How to move a servo with an analog input

I want to receive a voltage as an input and compare it to a value, and if it is >= to that value, make the servo move.
The servo will always be connected to 5V in the arduino and if the comparison is true the servo will start moving.
This is the code I have, but it doesn't work.
Can someone help me fix this problem?
Thanks!!

Servo servo1; // creates an instance of the servo object to control a servo

int posServo1 = 0;
int analogPin = A0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int analogValue1 = 0;

int servoPin = 4; // Control pin for servo motor
void setup() {
servo1.attach(servoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}

void loop()
{
pinMode(A0,INPUT);
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
analogValue1 = map(analogValue, 0, 1023, 0, 179); // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
Serial.println(analogValue1);
while(analogValue1>=2.5)
{
servo1.write(posServo1+=1); // write the new mapped analog value to set the position of the servo
delay(50);
} // waits for the servo to get there
}

First of all what pin are you wiring to your servo? The code says pin 4 but your comment says pin 9.

Also why are you using a floating point number in while(analogValue1>=2.5) ?

Logic seems not correct:

while(analogValue1>=2.5)
   { 
   servo1.write(posServo1+=1);                       // write the new mapped analog value to set the position of the servo
   delay(50);     
   }   // waits for the servo to get there

Why wouldn't you just use servo1.write(analogValue1); ?

Lefty

This is my new code, but it still doesn't work:

#include "Servo.h" // include the servo library

Servo servo1; // creates an instance of the servo object to control a servo

int posServo1 = 0;
int analogPin = A0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int analogValue1 = 0;

int servoPin = 4; // Control pin for servo motor
void setup() {
servo1.attach(servoPin); // attaches the servo on pin 4 to the servo object
Serial.begin(9600);
}

void loop()
{
pinMode(A0,INPUT);
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
//analogValue1 = map(analogValue, 0, 1023, 0, 179); // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
float val = map(analogValue, 0, 1023, 0.0, 5.0); // converts analogRead into a voltage between 0.0-5.0
Serial.print(val,DEC);
while(val>=2.5)
{
servo1.write(posServo1+=1); // write the new mapped analog value to set the position of the servo
delay(50);
} // waits for the servo to get there
}
The pin I'm using is pin 4 and I want the servo to move, when the voltage is >= 2.5V.
I also want to print out the voltage values when I connect the battery to the arduino.
Please help!

Your code seems really messed up in lots of places, but I'm not good a explaining it all.

Why don't you load the example program called Knob from the arduino IDE file/Examples/Servo/Knob because it sounds like it does exactly what you want. If not, at least it would be a starting point to build on.

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

Lefty

I looked at the knob example and it does the same thing that I'm doing, with the exception that the map is map(val, 0, 1023, 0, 179), so it returns an angle and mine is map(val, 0, 1023, 0.0,5.0 ), so it returns a voltage between 0 and 5V.
Please tell me what do I have to fix in order for this to work!
What I want to do is make my servo move when the input >= 2.5V.

Why are you concerned about voltage and mapping it to a 0-5 reading? A analog input value can span 0-1023, a servo command accepts a 0-179 angle value, so that is what the Knob program uses. It covers full range of the pot and full range of the servo. So what in addition to that are you trying to accomplish? Just use 90 degrees as your >= decision point.

Lefty

I'm using this comparison, because I want the servo to move when it reaches a voltage >= 2.5V. I still want it to move the whole 180 degrees, but I want it to give me a voltage instead of a degree. That's the reason I'm using 0,5V.
If what I'm doing is incorrect, how can I get a voltage between 0 and 5V instead of a degree between 0 and 179 degrees?
Thanks for your help!

nando88:
I'm using this comparison, because I want the servo to move when it reaches a voltage >= 2.5V. I still want it to move the whole 180 degrees, but I want it to give me a voltage instead of a degree. That's the reason I'm using 0,5V.
If what I'm doing is incorrect, how can I get a voltage between 0 and 5V instead of a degree between 0 and 179 degrees?
Thanks for your help!

I still don't understand what you mean by 'get a voltage' or 'give me a voltage'? Is this in regard to the analog input being driven by the pot? It isn't about the servo output as that is a PPM signal output. So your reference to voltage inside your sketch just doesn't make much sense to me. Inside your sketch the pots reading can range from 0-1023, can't you just use a number inside that range that represents the value you want to do something with?

Lefty

why not map in just another way:

if pot is giving a value over 511 (2.5 volts at input) then map the values from 511 to 1023 to coordinates 0-180 of the servo

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  if (val >=511) {  // if analogRead is equal or over 2.5V
    val = map(val, 511, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
    myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

This is untested code, so .....

D.

I really need the map to return a voltage to compare it to 2.5V, if its >= the servo should move from starting position (0). The thing is the servo will always be connected to 5V and GND so the only way its going to move is if the voltage >=2.5V, so how can I map or convert the map values to a voltage?

That's the reason the title says move servo with analog input, because what I'm trying to do is to move the servo when the voltage reaches a certain value, in this case 2.5V.
Please tell me how to map or convert the map to a voltage because the value given by the map is useless to me, because I want to move the servo from 0 and 1 by 1, and not through the map!

I don't understand your problem. If you are doing an analogRead you will get a number from 0 to 1023. Assuming that your reference value is 5V, when you have 2.5V on the input, analogRead will return~512. You wish to move the servo therefore, if you get a number >=512. There is no need whatsoever to map this to a voltage and by the way, map is implemented with long integers, so any fractional part you pass it will be dropped. If you really really really want to work in volts, you will need to roll your own mapping function.

Edit: veracity error.

I made the changes to my code, but when I'm in >=511, the servo doesn't move, what should I do?
Here's my code:
#include "Servo.h" // include the servo library

Servo servo1; // creates an instance of the servo object to control a servo

int posServo1 = 0;
int analogPin = A0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int analogValue1 = 0;

int servoPin = 4; // Control pin for servo motor
void setup() {
servo1.attach(servoPin); // attaches the servo on pin 4 to the servo object
Serial.begin(9600);
}

void loop()
{
pinMode(A0,INPUT);
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
//analogValue1 = map(analogValue, 0, 1023, 0, 179); // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
float val = map(analogValue, 0, 1023, 0, 179);
Serial.print(val,DEC);
while(analogValue>=511)
{
servo1.write(posServo1+=1); // write the new mapped analog value to set the position of the servo
delay(50);
} // waits for the servo to get there

while(analogValue<=511)
{
servo1.write(posServo1-=1); // write the new mapped analog value to set the position of the servo
delay(50);
} // waits for the servo to get there

while(analogValue==0)
{
servo1.write(0); // write the new mapped analog value to set the position of the servo
delay(50);
} // waits for the servo to get there
}

You have a number of issues - those while loops will never terminate - you'll need to read the analog pin again inside the loop.

First things first though - how do you have the servo wired up - does it have it's own power supply? Or rather - have you been able to make the servo move at all, with a simpler program?

Lastly, if you're posting code, please put code tags around it - use the # key in the toolbar above.

I've made the servo move with a simpler code, but the thing is it didn't have the analog input it was always moving, so how can I make the servo move whe the analogRead >=512?
Please help me out!
Thanks in advance!!

Here is a simpler version of your code, with the looping issue removed:

#include "Servo.h"  // include the servo library

 Servo servo1;       // creates an instance of the servo object to control a servo
 int posServo1 = 0;
 int analogPin = A0;      // the analog pin that the sensor is on
 int analogValue = 0;    // the value returned from the analog sensor
 int servoPin = 4;       // Control pin for servo motor

 void setup() 
 {
 servo1.attach(servoPin);  // attaches the servo on pin 4 to the servo object
 Serial.begin(9600);
 servo1.write(45);
 delay(2000);
 }

 void loop()
 {
 analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
 Serial.println(analogValue);
 if(analogValue==0)
   {
   posServo1=0;
   }
 else if(analogValue>=511)
   {
   posServo1+=1;
   }
 else 
   {
   posServo1-=1;
   }
 posServo1=constrain(posServo1,0,180);  
 servo1.write(posServo1);     // write the new mapped analog value to set the position of the servo
 delay(50);                   // waits for the servo to get there
 }

I'm still not entirely clear what the requirement is, But this version should move the Servo to 45 and delay a little, just to prove that the servo does move. Then it continuously reads the analog pin. If it's zero, servo goes to position 0. If it's >= 511 servo position is incremented, otherwise decremented. The constrain statement keeps the position between 0 and 180.