Controlling continuous rotation servos using light sensor

Hi guys,

I am new to the Arduino and I hope you guys can help me out a bit.

What I am currently doing is to connect the light sensor and the continuous rotation servo to the Arduino board. When the light intensity is below a certain amount, it will then trigger the servo so it will rotate for certain degree. It will then stay at that position for a while and goes back to the original position. The code works perfectly, and I want to connect more servos and more sensors, so that sensor A will control servo A; sensor B will control servo B. But the questions are:

  1. Is it possible to have several light sensors as different input?
  2. I was using "delay" in my script, so it might not possible to have the servos operate properly in the same time. Is there any way but doing the same job?

I really need your help, thank you for reading my quesitons.=]

Here is the Arduino script I used:

#include <Servo.h>

Servo myServo;

#define myServoservopin 8

#define Sensor A0 // setting up light sensor

int Sensorvalue;

const int Threshold = 10; // the value that the servo will trigger

void setup(){

myServo.attach(myServoservopin);

myServo.writeMicroseconds(1500);

Serial.begin(9600);

Serial.println("Inactive!");

Serial.println(Sensorvalue);
}

void loop(){
Sensorvalue = analogRead(Sensor);

Serial.println(Sensorvalue);

if (Sensorvalue < Threshold) {

Serial.println(Sensorvalue);

Serial.println("Active!");

myServo.writeMicroseconds(1600); // servo rotating in a specific direction

delay(4500); // the time that the servo would keep rotating in that direction

myServo.writeMicroseconds(1500); // the servo would stay stationary in 1500

delay(1000); // the time that the servo would stay in position

myServo.writeMicroseconds(1400); // servo rotating in opposite direction in attempt to go back to the original position

delay(4500);

Serial.println("Inactive!");

myServo.writeMicroseconds(1500);
}

}

it will then trigger the servo so it will rotate for certain degree.

If it is a continuous rotation ex-servo, then you simply cannot do that without a limit switch of some sort or another form of encoder. (unless it is a sail winch servo)

In order to make it stop in a desire position, I was using "delay" to achieve that

According to the manufacturer,
1300 ?s: Turn clockwise
1500 ?s: Stops the motor
1700 ?s: Turn counterclockwise

myServo.writeMicroseconds(1600); // the servo would then turn counterclockwise
delay(4500); // this value is the amount of turns I wanted

However, instead of using delay, is there any way I can use?

Four and a half seconds of delay is going to bang most servos into the end-stops and strip gear teeth.

AWOL:
Four and a half seconds of delay is going to bang most servos into the end-stops and strip gear teeth.

You need a (very) short delay to give the not-really-a-servo time to move, then use writeMicroseconds() to stop it, then you can have the long delay.

In order to make it stop in a desire position, I was using "delay" to achieve that

That's not really turning the (not a)servo to a particular position, it is moving the (not a)servo for a particular length of time. The angle it moves will not be exactly the same each time. Is there any reason why you cannot use a real servo ?

Is there any reason why you cannot use a real servo ?

Since the (not a) servo would be connected to a wire and it will act like a winch, normal servo couldn't do the job as they can't rotate more than 360 degree.

Yes Bob you are right, with this servo I can only control speed and direction, but not angle.

and it will act like a winch

So why not use a sail winch servo?

mkle:

Is there any reason why you cannot use a real servo ?

Since the (not a) servo would be connected to a wire and it will act like a winch, normal servo couldn't do the job as they can't rotate more than 360 degree.

Yes Bob you are right, with this servo I can only control speed and direction, but not angle.

So it now emerges that you want to rotate the (not a) servo more than 360 degrees. That is going to make the timing even more critical I would think. Can you put an optical encoder on the servo output or maybe a single magnet if the amount of rotation will always be the same.

AWOL:

and it will act like a winch

So why not use a sail winch servo?

Oh I didn't think of that beforehand, but since I already bought the servos, it would be great if I could figure out how to make them work

Can you put an optical encoder on the servo output or maybe a single magnet if the amount of rotation will always be the same.

Thanks for the heads-up, but my concern is that , is it possible to have sensor A control servo A; sensor B control servo B at the same time?

Thanks for the heads-up, but my concern is that , is it possible to have sensor A control servo A; sensor B control servo B at the same time?

Assuming your light sensor has an analog voltage output, then the code would be similar to the servo knob example code. Coding for multiple sensors/servos is fairly easy.

mkle:

Can you put an optical encoder on the servo output or maybe a single magnet if the amount of rotation will always be the same.

Thanks for the heads-up, but my concern is that , is it possible to have sensor A control servo A; sensor B control servo B at the same time?

At the same time, no, but sequentially so fast that it appears to happen at the same time. Using interrupts the control could be made to happen when required rather than in any particular order.