Servo + Photoresistor + Multitasking + More

Hello,

I am currently working on a university project using Arduino Uno.

The project includes 9 servos (servo1,servo2,etc) whose movement is controlled by 9 photoresistos (1,2,etc) respectively .The servos are attached to 9 straws which move vertically depending on the values read from the photocells which are translated to degrees for the servos.

1.I want the servos to be able to move simultaneously and independantly from one another.
2.When the straws reach their lowest position, AT THAT MOMENT I want the servos to pause for a few second and then
3.start moving to their initial position “at a lower speed” (meaning that i don’t want their return to their initial position to be instant/sudden)

My team and I have managed to form the code for the servos to be controlled by the photoresistors.We thought that by combining elements from codes (that can describe/control each of the above 1,2,3 respectively) would work, but nothing seems to be efficient.

This is the code we have formed thus far, to control the servos via the photocells:

#include <Servo.h>
Servo myservo1;
Servo myservo2;
int val1;
int val2;
void setup()
{
Serial.begin(9600);
myservo1.attach(12);
myservo2.attach(11);
}
void loop()
{
val1 = analogRead(0);
val1 = map(val1, 40, 170, 0, 180);
myservo1.write(val1);
Serial.println(val1);
delay(0);
val2 = analogRead(1);
val2 = map(val2, 40, 170, 0, 180);
myservo2.write(val2);
delay(0);
}

In this forum, we have found the following codes:

1-For pausing the servos before returning to their initial position (sorry i could not find the original post):

#include <Servo.h>
Servo myServo;
int myServoPin=12;
void setup()
{
** Serial.begin(9600);**
** myServo.attach(myServoPin);**
}

void loop()
** {**
** // If it’s time to open the valve**
** myServo.attach(myServoPin);**
** myServo.write(0);**
** delay(2000); // Give the servo time to move**
** myServo.detach(); // Stop sending pulses so the servo doesn’t try to move**
** delay(2000); // Delay the rest of the 5 minutes**
** myServo.attach(myServoPin);**
** myServo.write(180);**
** delay(2000); // Give the servo time to move**
** myServo.detach(); // Stop sending pulses so the servo doesn’t try to move**
** }**

2-For controlling the speed when returning to the initial position (for which we have downloaded the corresponding library):

#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
void setup() {
** myservo.attach(12); // attaches the servo on pin 9 to the servo object**
}
void loop() {
** myservo.write(180, 5, true); // move to 180 degrees, use a speed of 30, wait until move is complete**
** myservo.write(0, 30, true); // move to 0 degrees, use a speed of 30, wait until move is complete**
}

We need to note that we are THE ULTIMATE ROOKIES! We started working with Arduino only recently and the teachers in our class taught us the very basics!! :wink: So we might be thinking all this in the wrong way and overcomplicating things…We are open to all ideas!

Thanks in advance,guys!

We need to note that we are THE ULTIMATE ROOKIES!

So the first thing you would expect a rookie to do would be to read the "how to use this forum" sticky post.
Do that, then edit that post to include code tags.

The only way to slow down a servo is to break the required movement into a number of small steps, command those steps and put a small delay in between. If you want to do that with many servos then you will need a state machine.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once

As to the number of servos, most Arduino boards (UNO...) support only 6 PWM outputs, at pins dedicated in hardware. If you want to control more servos, you can use multiple Arduinos (good for more multi-tasking functionality), or you can use e.g. an Arduino Mega for up to 15 servos.

No a standard UNO supports 12 servos. Are you mixing that up with the number of PWM pins?

Isn't one PWM pin sufficient for controlling one servo motor?

I've not used an Mega yet, any enlightenment is appreciated :slight_smile:

DrDiettrich:
Isn't one PWM pin sufficient for controlling one servo motor?

No a PWM pin is most unsuitable for controlling a servo. You use the servo library and that uses one timer in 16 bit mode. It then controls the servos using an interrupt driven timer task to generate the modulation required to drive the servo. A servo requires a pulse between 1mS and 2mS depending on the position you want it to be. This is repeated every 50mS or so. While the frame rate can be faster than this you can't get it too fast.

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega.
See:-

For the theory behind the control see:-

(deleted)

the value of the new one is about ~700 but the old one is about ~500, both at the same light exposure.

So what makes you thing that one is more accurate than the other. Different yes, sensitive yes, but accurate no. There is no measurement to be accurate about.

MUST PLUG THE NEGATIVE POLE TO THE ARDUINO BOARD OR ELSE THE SERVO WONT WORK!!!

No too simplistic. The External power supply ground and the arduino ground must be connected together. Then the negative connection ( not pole ) must be connected to the common point.

I recommend you to use an external battery for the servos or your board will turn off every time the servo moves as it consume too much power from your board.

That depends on the power supply you are using and the amount of decoupling you have going into the servio supply.

Belive it or not, I've just done a school project

Comments like that, oh yes I believe you have just done a school project. Maybe you can get a bit more perspective before shooting off.

Thanks, I didn't realize yet how servos are used with Arduinos.

Is it hard or impossible to use the hardware PWM with servos, or is the servo library only an enhancement of the hardware support?

Is it hard or impossible to use the hardware PWM with servos

Not impossible but the results are poor. That is there is not much resolution with the control. And you will also be giving a faster frame rate than some servos can cope with.