Hi guys,
I have some code and it should in theory allow a servo to rotate 90 degrees and stop, this needs to be repeated several times, the clock uses ball bearings and the servo decide which ball bearing falls in which tube, however I can get it to work for only 2 servos, the thing is i need it to run for 5, I have included the code so if anyone can help me out that would be great, currently the remaining servos don't move or just stutter for about a minute and return to their original position.
I generally am looking for this kind of layout
s2 s1
llll llll llll
l0l llll llll
l0l llll llll
l0l l0l l0l
s3 s4 s5
ok, so its not the greatest drawing but that is generally how it will work, now the hard part
Tube 1 - hours - 1-12
Tube 2 - 5mins - 5,10,15...
Tube 3 - 1mins - 1-4
Ok so now that should make more sense as to what I'm trying to do here is the code.
Cheers
#include <Servo.h>
int second =55, minute=4, hour=0; //start the time on 00:00:00
int munit,hunit,valm=0,valh=0,ledstats,i;
Servo myservo1; // create servo objects to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5; // a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int pos2 = 180;
void setup()
{
myservo1.attach(3); // attaches the servo on pin 2 to the servo object
myservo2.attach(5); // attaches the servo on pin 4 to the servo object
myservo3.attach(9); // attaches the servo on pin 6 to the servo object
myservo4.attach(12); // attaches the servo on pin 8 to the servo object
myservo5.attach(7); // attaches the servo on pin 9 to the servo object
myservo1.write(0);
myservo2.write(0);
myservo3.write(179);
myservo4.write(0);
myservo5.write(0);
Serial.begin(9600);
}
void loop()
{
//Serial.println(minute);
//Serial.println(second);
static unsigned long lastTick = 0;
//if (millis() - lastTick >= 1000)
//{
//lastTick = millis();
//second++;
//}
//move forward one minute every 60 seconds
//if (second >= 60)
//{
//minute++;
//second = 0; // reset seconds to zero
//}
// move forward one hour every 60 minutes
if (minute >=60)
{
hour++;
minute = 0; // reset minutes to zero
}
if (hour >=12)
{
hour = 0;
minute = 0; // reset minutes to zero
}
munit = minute; //sets the variable munit and hunit for the unit digits
hunit = hour;
Serial.println(munit);
//minutes units
if(munit == 1 ){ myservo1.write(0); //starts loop
myservo3.write(179);
delay(1000);
}
if(munit == 5 ){myservo1.write(180);
myservo2.write(180);
myservo3.write(0);
myservo4.write(0);
//delay(5);
}
if(munit == 6) { myservo1.write(0);
myservo3.write(180);}
if(munit == 10) { myservo1.write(180);
myservo3.write(0);}
if(munit == 11) { myservo1.write(0);
myservo3.write(180);}
if(munit == 15) { myservo1.write(180);}
myservo3.write(0);
if(munit == 16) { myservo1.write(0);
myservo3.write(180);}
if(munit == 20) { myservo1.write(180);
myservo3.write(0);}
if(munit == 21) { myservo1.write(0);
myservo3.write(180);}
if(munit == 25) { myservo1.write(180);
myservo3.write(0);}
if(munit == 26) { myservo1.write(0);
myservo3.write(180);}
if(munit == 30) { myservo1.write(180);
myservo3.write(0);}
if(munit == 31) { myservo1.write(0);}
myservo3.write(180);
if(munit == 35) { myservo1.write(180);
myservo3.write(0);}
if(munit == 36) { myservo1.write(0);
myservo3.write(180); }
if(munit == 40) { myservo1.write(180);
myservo3.write(0);}
if(munit == 41) { myservo1.write(0);
myservo3.write(180);}
if(munit == 45) { myservo1.write(0);
myservo3.write(180);}
if(munit == 46) { myservo1.write(180);
myservo3.write(0);}
if(munit == 50) { myservo1.write(0);
myservo3.write(180);}
if(munit == 51) { myservo1.write(180);
myservo3.write(0);}
if(munit == 55) { myservo1.write(0);
myservo3.write(180);}
if(hunit == 1) { myservo1.write(180); //acknowledges an hour has passed
myservo2.write(180);
myservo3.write(0);
myservo4.write(0);
myservo5.write(180);}
if(hunit == 12) { myservo1.write(180); //acknowledges that 12 hours have passed
myservo2.write(180); //releases all the ball bearings
myservo3.write(180);
myservo4.write(180);
myservo5.write(180);}
delay (1000);
minute++;
}