servos controlled with infrared sensor

I am working on a piece where I need to hook up 7 servo motors to the arduino (each rotating at a different speed, some in one direction, others in the other) but basically an on/off with a motion sensor. I am using a full rotation servo.

I amusing an infrared sensor but the servo is still moving (awkwardly and jerky) when not sensing motion, but then runs as normal.
Here is the code I am using (right now, I am only at 1 servo – trying to get the sensor right first) :

#include <Servo.h>

Servo myservo;
int potPin = 0; //analog pin used to connect the sensor
int ledPin = 9; //select pin for the servo
int val = 0; //variable to read the value from the analog pin
void setup()
{
myservo.attach(9); //set up the servo as usual
pinMode(ledPin, OUTPUT); //variable to store the value coming from the sensor
Serial.begin(9600); //for watching the speeds in the serial monitor
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
val = map(val,0,600,0,255);
if (val>80) digitalWrite(ledPin, HIGH); //turn the ledPin on

else if (val<80) digitalWrite(ledPin, LOW); //turn the LedPin off

Serial.println(val);
delay(10);

}

– any suggestions?

int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
void setup()
{
 myservo.attach(9); //set up the servo as usual
 pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor

Setting up the servo as usual generally means giving it it's own pin.

You never actually tell the servo to move.

That makes sense, I wondered what the set up as usual meant, but wouldn't I select it to show where the sensed info goes? and I am guessing I need to indicated what the movement would be under HIGH rather than just on? Thanks.

The servo is not an LED. You use the servo.write() command to direct the servo to a specific position.

Is the infrared sensor an analog sensor (distance) or a digital sensor (on/off)?

val = analogRead(potPin);    // read the value from the sensor
 val = map(val,0,600,0,255);

This implies that the sensor is indeed an analog sensor, but the range of values from an analog sensor is 0 to 1023. Of course, you may not be interested in the whole range, but you should ensure that the value IS in the from range (constrain() might prove useful) before calling the map() function.

I am using a full rotation servo.

Why is the sensor value mapped to less than a full rotation, then? Can you actually use servo.write() to move the servo from 0 to 360 and back to 0?

Hi - thanks for the response - I finally got it to work with one servo - now I need to add the other 6.
Here is what worked:
#include <Servo.h>

Servo myservo;
int potPin = 0; //analog pin used to connect the sensor
int ledPin = 9; //select pin for the servo
int val = 0; //variable to read the value from the analog pin
void setup()
{
myservo.attach(9); //set up the servo as usual

pinMode(ledPin, OUTPUT); //variable to store the value coming from the sensor
Serial.begin(9600); //for watching the speeds in the serial monitor
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
val = map(val,0,1023,0,179);
if (val>79) myservo.writeMicroseconds(0); //set servo to mid-point

else if (val<80) myservo.writeMicroseconds(1500); //set servo to mid-point

Serial.println(val);
delay(10);

}

I’m surprised that works.

 val = map(val,0,1023,0,179);
  if (val>79) myservo.writeMicroseconds(0);  //set servo to mid-point
 
  else if (val<80) myservo.writeMicroseconds(1500);  //set servo to mid-point

The 0 to 1023 range is the range of values that the analog pin can report. That is the correct from range to use.

What does the 0 to 179 range represent?

The possible output from the map function is any number in the range 0 to 179. The if(val > 79) test takes care of 80 through 179. If the value is not greater than 79, it must be less than 80, right> So, why the second if text?

The writeMicroseconds() method is an alternative to the write() method that is typically only used for continuous rotation “servos”. You’ve said that this is not what you have.

The writeMicroseconds method typically takes a value between about 1500 and 2200. The low end of the range causes the servo to rotate one way, at maximum speed. The high end of the range causes the servo to rotate the other way, and maximum speed. The actually limits of the range need to be determined experimentally.

Values between the extremes cause the servo to rotate at less then full speed, with some value causing the servo to not move at all.

It isn’t clear why you are moving the servo using this method, and why the position to move the servo to is not a function of val.

Hey - thanks for your post - I have been busy installing another art installation and am just getting back to this now. I had taken a class where we were instructed to use the if else formula and I am using a continuous rotation servo. I want the servo to rotate when someone is close and triggers the sensor but I also want it to stop immediately when they move away. I am attempting to attach the other 6 servos today. I'll let you know if it works - thanks again - W

From your initial post:

I am using a full rotation servo.

From your last post:

I am using a continuous rotation servo.

A full rotation servo rotates from 0 to 360 degrees, and can be commanded to any angle in that range.

A continuous rotation servo isn't a servo at all. It can not be sent to a specific position and told to remain there.

you are correct - I am using a full rotation servo and not a continuous rotation servo. Sorry - I'm still a little new but working hard at getting this - do you know if I am connecting the different servos to different pins, can I just connect them directly to the arduino if there are no other components being attached rather than routing them to the breadboard? thanks - W

You can connect the control wires directly to the Arduino pins. The servo power can not come from the Arduino. Typically, one servo draws more current than the Arduino can provide. 6 certainly will.