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.
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?
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.
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.