Two servos on one Arduino Uno - telling time

HI

I am trying to get two servo to ‘tell time’. One os the hour hand the other minutes I have some code but I can only get one servo to work until the end of the for statement then the other kicks in … any tips in the coding?

Thanks in advance …


#include <Servo.h>

Servo minute; // create servo object to control a servo
Servo hour; // a maximum of eight servo objects can be created

int minutepos = 0; // variable to store the servo position
int hourpos = 0; // variable to store the servo position

void setup()
{
minute.attach(9); // attaches the servo on pin 9 to the servo object
hour.attach( 8 );
}

void loop()
{
for(minutepos = 0; minutepos < 180; minutepos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
minute.write(minutepos); // tell servo to go to position in variable ‘pos’
delay(1000); // waits 1000ms for the servo to reach the position
}
for(minutepos = 180; minutepos>=1; minutepos-=1) // goes from 180 degrees to 0 degrees
{
minute.write(minutepos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}

for(hourpos = 0; hourpos < 180; hourpos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
hour.write(hourpos); // tell servo to go to position in variable ‘pos’
delay(600); // waits 600ms for the servo to reach the position
}
for(hourpos = 180; hourpos>=1; hourpos-=1) // goes from 180 degrees to 0 degrees
{
hour.write(hourpos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}

}


DesTech:
I can only get one servo to work until the end of the for statement then the other kicks in

That's exactly how FOR loops work.

If one servo is supposed to advance once every minute (perhaps 180 deg = 3 x 60 minutes) and the other is supposed to advance every hour then you don't use a FOR loop with the servos at all.

You just need

minute.write(minutePos);
hour.write(hourPos)

;

and some other code to make the numbers in minutePos and hourPos increment appropriately.

...R

Thanks R

I was getting myself all tied up and the simple thing was there!

M

I have another question ...

Now I want to connect a couple of small hobby dc motors. I have the code (below) and I can get them to work but at the end of the code it delays and the dc motor sops as well.

How can I have the dc motor continue while the hands of the servo clock reset when they are suppose to? Is it an if-else statement or Case?

Thanks in advance

M

I have the code (below)

Didn't seem to make it into your post...

oops …

#include <Servo.h>

Servo minute; // create servo object to control a servo
Servo hour; // a maximum of eight servo objects can be created

int potPin = A0;
int motorPin = 3;
int potValue = 0;
int motorValue = 0;

void setup()
{
minute.attach(9); // attaches the servo on pin 9 to the servo object
hour.attach(8);
Serial.begin(9600);
}

void loop()
{
potValue = analogRead(potPin);
motorValue = map(potValue, 0, 1023, 0, 255);
analogWrite(motorPin, motorValue);
Serial.print(“potentiometer = " );
Serial.print(potValue);
Serial.print(”\t motor = ");
Serial.println(motorValue);
delay(2);

minute.write(0);
hour.write(0);
delay(1000);
minute.write(45);
hour.write(25);
delay(1000);
minute.write(90);
hour.write(50);
delay(1000);
minute.write(135);
hour.write(75);
delay(1000);
minute.write(180);
hour.write(100);
delay(1000);
minute.write(0);
hour.write(100);
delay(1000);
}

You need to lose ALL the delay()s and manage the time with the technique in the Blink Without Delay example sketch. This demo several things at a time is an extended example of the technique.

...R

Thanks Robin

I am a newbie so the coding is a bit daunting. I will try and I will try something else like run two arduinos

M

DesTech:
I will try something else like run two arduinos

I may have the wrong idea entirely, but if one Arduino is enough, using two will be a great deal more complex.

The learning curve may seem steep, but if you work through several of the examples that come with the Arduino IDE you will master it. Treat your program as a series of small separate parts that are joined together and get each piece working on its own.

...R

have the code (below)

You need to read #7 below.

http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks R

I can do small stuff but the bigger more complex motor controlling is alluding me and I have an project deadline for uni.

I will try

M