Someone please help I am inexperienced with C and cant seem to solve this problem.
I have built a machine with three Servos connected to a Uno board.
What i am trying to achieve is for Servo s2 to activate 3 times (inner loop) then loop around and activate servo s7 and then back to the inner loop to activate s2 3 times etc until servo s7 has been activated 4 times and then jump out of the loops to activate servo s5 then go back to the beginning.
What is happening is:
s7 once
s2 3times
s5 once and then back to the beginning
I would be so appreciative of any help with this.
Cliff
Code below
#include <Servo.h>
Servo servos7; //creates a servo object to control servo s7 which is the minute ball lift
Servo servos2; //creates a servo object to control servo s2 which releases all minute balls at the botom of the minute ramp
Servo servos5; //creates a servo object to control servo s5 which releases all hour balls at the botom of the hour ramp
//the following provide the starting positions for Servos
int poss7 = 0; // this is the starting position for Servo S7
int poss2 = 0; // this is the starting position for Servo S2
int poss5 = 0; // this is the starting position for Servo S5
void setup() {
servos7.attach(12); // attaches the servo S7 to pin 12 to the servo object
servos2.attach(5); // attaches the servo s2 to pin 5 to the servo object
servos5.attach(10); // attaches the servo s5 to pin 10 to the servo object
// the next steps set the servos up to their start positions.
for (poss7 =160; poss7 >= 20; poss7 -= 1) { // this makes Lift Servo S7 go from 130 degrees to 20 degrees in steps of one degree
servos7.write(poss7); // tell servo to go to position in variable 'poss7'
delay(10); // waits 10m for the servo to reach the position during each 1 degree step
}
for (poss2 = 130; poss2 >= 35; poss2 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos2.write(poss2); // tell servo to go to position in variable 'poss2'
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
for (poss5 = 130; poss5 >= 35; poss5 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos5.write(poss5); // tell servo to go to position in variable 'poss2'
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
}
void loop()
{
for (int i = 0; i < 4; i++) { //S7 activates once then S2 activates 3 times and then S5 activates and it circles back to S7 it looks like i is not incrementing
s7 () ;
for (int j = 0; j < 3; j++) {
s2 () ;
delay(1000); // This delay is to allow for
}
s5 () ;
}
}
//FUNCTIONS
void s7 ()
// SrvoS7
{
for (poss7 = 20; poss7 <= 160; poss7 += 1) { // this makes S7 go from 20 degrees to 160 degrees in steps of 1 degree
servos7.write(poss7);
delay(10); // waits 10m for the servo to reach the position during each 1 degree step
}
delay(2000); // introduce a delay
for (poss7 =160; poss7 >= 20; poss7 -= 1) { // this makes Servo S7 go from 160 degrees to 20 degrees in steps of one degree
servos7.write(poss7);
delay(10); // waits 10m for the servo to reach the position during each 1 degree step
}
}
void s2 ()
// Servo S2
{
for (poss2 = 35; poss2 <= 130; poss2 += 1) { // this makes Servo S2 go from 35 degrees to 130 degrees in steps of 1 degree
servos2.write(poss2);
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
delay(3000); // introduce a delay
for (poss2 = 130; poss2 >= 35; poss2 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos2.write(poss2);
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
}
void s5 ()
// Servo S5
{
for (poss5 = 35; poss5 <= 130; poss5 += 1) { // this makes Servo S5 go from 35 degrees to 130 degrees in steps of 1 degree
servos5.write(poss5);
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
delay(3000); // introduce a delay for all the balls to pass over the stop (THIS WILL HAVE TO BE ADJUSTED FOR ALL BALLS TO PASS)
for (poss5 = 130; poss5 >= 35; poss5 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos5.write(poss5); // tell servo to go to position in variable 'poss2'
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
}