I have a sketch to move 4 servos at random intervals, the sketch is working well but I want to be able to start the sketch with a push button and to end after a set time.
I have been using the following sketch with a LED which works fine but I don't know how to put a Button Activated Millis Timer into my Servo Sketch which already uses millis. I have tried putting the two together and I seem to be a long way from getting it to work; can someone advise on how I can go about it?
Button activated Millis Timer for LED
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
unsigned long off_time;
boolean ledState=false;
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(ledPin, LOW);
}
void loop(){
if ((ledState) && (millis()>=off_time)) /* is it on and is it later or equal to off_time */
{
digitalWrite(ledPin,LOW);
ledState = false;
}
else if (!ledState) /* is it off? */
{
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
ledState = true;
off_time = millis() + 15000;
}
}}
Servo Code to move servo's at random intervals.
#include <Servo.h>
Servo servo;
Servo servo2;
Servo servo3;
Servo servo4;
int pos = 0;
int pos2 =0;
int pos3 = 0;
int pos4 =0;
unsigned long timerVal; // Variable to hold the timer for servo head
unsigned long timerVal2;
unsigned long timerVal3;
unsigned long timerVal4;
int countupdown = 1; // Variable to hold the positive or negative change in angle
int countupdown2 = 2;
int countupdown3 = 3;
int countupdown4 = 4;
unsigned long previousMillis1;
unsigned long previousMillis2;
unsigned long previousMillis3;
unsigned long previousMillis4;
unsigned long randInterval1;
unsigned long randInterval2;
unsigned long randInterval3;
unsigned long randInterval4;
const int buttonPin = 2; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
const int ledPin = 12; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
unsigned long off_time; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
boolean ledState=false;
int buttonState = 0;
void setup() {
servo.attach (8);
servo2.attach (9);
servo3.attach (10);
servo4.attach (11);
pinMode(ledPin, OUTPUT); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
pinMode(buttonPin, INPUT); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
digitalWrite(ledPin, LOW); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
//randomSeed (analogRead (0));
}
void loop()
{
if ((ledState) && (millis()>=off_time)) // TAKEN FROM SKETCH: Button activated Millis Timer for LED
{ // TAKEN FROM SKETCH: Button activated Millis Timer for LED
digitalWrite(ledPin,LOW); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
ledState = false; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
} // TAKEN FROM SKETCH: Button activated Millis Timer for LED
else if (!ledState) /* is it off? */ // TAKEN FROM SKETCH: Button activated Millis Timer for LED
{ // TAKEN FROM SKETCH: Button activated Millis Timer for LED
buttonState = digitalRead(buttonPin); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
if(buttonState == HIGH) // TAKEN FROM SKETCH: Button activated Millis Timer for LED
{ // TAKEN FROM SKETCH: Button activated Millis Timer for LED
digitalWrite(ledPin, HIGH); // TAKEN FROM SKETCH: Button activated Millis Timer for LED
if ((timerVal + randInterval1) <= millis()) { // SERVO LOOP STARTS
randInterval1 = random (15, 600);
servo.write(pos); // move head servo
pos = pos + countupdown; // increment or decrement value depending on countupdown value
if (pos == 120) { // If pos reaches this position, change
countupdown = -20; // directon by making countupdown negative
}
if (pos == 60) { // If pos reaches this position, change direction
countupdown = 20; // making countupdown positive
}
timerVal = millis(); // Reset the timer
}
if ((timerVal2 + randInterval2) <= millis()) { // Timer loop
randInterval2 = random (15, 1000);
servo2.write(pos); // move head servo
pos2 = pos2 + countupdown2; // increment or decrement value depending on countupdown value
if (pos2 == 120) { // If pos reaches this position, change
countupdown2 = -20; // directon by making countupdown negative
}
if (pos2 == 60) { // If pos reaches this position, change direction
countupdown2 = 20; // making countupdown positive
}
timerVal2 = millis(); // Reset the timer
}
if ((timerVal3 + randInterval3) <= millis()) { // Timer loop
randInterval3 = random (15, 1000);
servo3.write(pos); // move head servo
pos3 = pos3 + countupdown3; // increment or decrement value depending on countupdown value
if (pos3 == 120) { // If pos reaches this position, change
countupdown3 = -20; // directon by making countupdown negative
}
if (pos3 == 60) { // If pos reaches this position, change direction
countupdown3 = 20; // making countupdown positive
}
timerVal3 = millis(); // Reset the timer
}
if ((timerVal4 + randInterval4) <= millis()) { // Timer loop
randInterval4 = random (15, 1000);
servo4.write(pos); // move head servo
pos4 = pos4 + countupdown4; // increment or decrement value depending on countupdown value
if (pos4 == 120) { // If pos reaches this position, change
countupdown4 = -20; // directon by making countupdown negative
}
if (pos4 == 60) { // If pos reaches this position, change direction
countupdown4 = 20; // making countupdown positive
}
timerVal4 = millis(); // Reset the timer
ledState = true; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
off_time = millis() + 15000; // TAKEN FROM SKETCH: Button activated Millis Timer for LED
}
}
}
}