Hi
I am new to this forum and quite new to the Arduino.
I have compiled a Sketch that will operate 5 servos, each will open a gate for 3 seconds then close wait 5 seconds them proceed to the next servo.
I have checked to program and it works as required.
I now need to be a able to select each gate randomly and not repeat in a sequence of 5.
I believe the random function is not truly random so I considered creating arrays with alternate sequences and selecting appropriately.
If someone could advise a solution or point in right direction I would really appreciate it.
The code I have compiled is listed below. I have tried to notate as much as possible.
// 5 Servos connected via separate power supply
//To create 5 gates each controlled by separate servo to to open for 3 seconds with 5 seconds between next event
#include <Servo.h>
Servo myservo5; // create servo object to control a servo
Servo myservo1; // 5 servos to control
Servo myservo2; // all numbered accordingly
Servo myservo3;
Servo myservo4;
int pos = 0; // variable to store the servo position
int del3 = 3000; // sets delay time to 3 secondd
int del5 = 5000; // sets delay time to 5 seconds
void setup()
{
myservo5.attach(13); // attaches the servos on pins 9 to 13 to the servo object
myservo1.attach(12);
myservo2.attach(11);
myservo3.attach(10);
myservo4.attach(9);
}
void loop() //loop opening and closing gates
{
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo5.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del3); // Holds gate open for 3 seconds before closing sequence
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo5.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del5); // Waits 5 seconds before next gate opens
}
//******************************servo5****************************************
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del3); // Holds gate open for 3 seconds before closing sequence
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del5); // Waits 5 seconds before next gate opens
}
//******************************servo4****************************************
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del3); // Holds gate open for 3 seconds before closing sequence
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del5); // Waits 5 seconds before next gate opens
}
//******************************servo3****************************************
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del3); // Holds gate open for 3 seconds before closing sequence
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del5); // Waits 5 seconds before next gate opens
}
//******************************servo2****************************************
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo4.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del3); // Holds gate open for 3 seconds before closing sequence
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo4.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
{delay(del5); // Waits 5 seconds before next gate opens
}
//******************************servo1****************************************
}
Many thanks in advance
Fred