The subject topic of this post maybe wrong, best I could think of Sorry.... :o
I need to run a dc motor every 30secs run forward 2 secs and then backward 2 secs.
Using a servo or a relay is not an option for this test project.
This is my objective using an arduino and it works as a stand alone program
const int outA = 11;
const int outB = 12 ;
void setup() {
pinMode(outA,OUTPUT);
pinMode(outB,OUTPUT);
digitalWrite(outA,LOW); // Motor Stop 0
digitalWrite(outB,LOW); // Motor Stop 0
delay (10000); // Arduino settle down on Power ON !
}
void loop() {
digitalWrite(outA,HIGH); // Motor Forward +
digitalWrite(outB,LOW); // Motor Forward -
delay(2000); // 2 seconds go forward
digitalWrite(outA,LOW); // Motor Backward -
digitalWrite(outB,HIGH); // Motor Backward +
delay (2000); // 2 seconds go backward
digitalWrite(outA,LOW); // Motor Stop 0
digitalWrite(outB,LOW); // Motor Stop 0
delay(30000); // 30 seconds wait
}
But I needed it to work as a function when called, so using millis I have managed it to get it working with the 2 second on/off
const int outA = 11;
const int outB = 12 ;
//ON and OFF Times 2 seconds
const unsigned int onTime = 2000;
const unsigned int offTime = 2000;
//Do Nothing Time
const unsigned int OverallTime = 30000;
// Store the last time we used
unsigned long previousMillis=0;
// Action wait period could be onTime or offTime
int interval = onTime;
// we keep track of the Output state on or off
boolean AOutstate = true; // ON
boolean BOutstate = false; // OFF
void setup() {
pinMode(outA, OUTPUT);
pinMode(outB,OUTPUT);
}
void loop(){
Motor_Action();
}
void Motor_Action() {
// Set the Outputs according to the loop state !
digitalWrite(outA, AOutstate);
digitalWrite(outB, BOutstate);
// capture and keep track of the time/clock!
unsigned long currentMillis = millis();
// Check the time against the main time/clock!
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
//Change the Output state
if (AOutstate) {
// If Motor is currently Forward, set time to go Backward
/*
digitalWrite(outA,LOW); // Motor Backward -
digitalWrite(outB,HIGH); // Motor Backward +
delay(2000);
*/
interval = offTime;
}
else if (BOutstate) {
// If Motors is currently Backward, set time to go Forward
/*
digitalWrite(outA,HIGH); // Motor Forward +
digitalWrite(outB,LOW); // Motor Forward -
delay(2000);
*/
interval = onTime;
}
/*
//############# This Section ##################
{ //####
interval = OverallTime; //####
digitalWrite(outA,LOW); // Motor Stop 0 //####
digitalWrite(outB,LOW); // Motor Stop 0 //####
} //####
//############# ############ //####
*/
// Toggle the Output pins state
AOutstate = !(AOutstate);
BOutstate = !(BOutstate);
// Save the current time to compare and check later
previousMillis = currentMillis;
}
}
Where I would like help is getting an overall time of 30 secs when the function waits with the digital out's LOW
any and all help is appreciated