I'm trying to program a motor to run for 20ms, pause for 2 seconds, run again for 20ms, pause for .5 a second, run again for 20ms and then pause for 3 second, run for 20ms and pause for 1 second. I have been able to figure out delaying the instance at a constant amount but any help or guidance with different runtimes and delays would be greatly appreciated.
#define finger2 9
unsigned long previousTimeFinger2 = 0;
unsigned long finger2Delay = 1000;
byte finger2MotorState = LOW;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(finger2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
unsigned long timeNow = millis();
//Finger 2
if (timeNow - previousTimeFinger2 > finger2Delay) {
previousTimeFinger2 += finger2Delay;
//vibrate Motor
if (finger2MotorState == HIGH) {
finger2MotorState = LOW;
}
else {
finger2MotorState = HIGH;
}
digitalWrite(finger2, finger2MotorState);
}
}
You have created a timer that responds to a set delay of 1000ms. You need to tweak it.
Think about how the timer works
You note the time (startTime)
You check how much time has elapsed each time through loop (currentTime-startTime)
you compare time elapsed with delay time
You can do this with any number of delay times but problems you should avoid is repeating the action of a previous delayTime. You can prevent this with finite state machines or setting flags so that the function associated with each delayTime will only run once.
#define finger2 9
#define motorON HIGH
#define motorOFF LOW
bool motorFlag = false;
bool pauseFlag = false;
byte count = 0;
unsigned int run[] = {20, 20, 20, 20};
unsigned int pause[] = {2000, 500, 3000, 1000};
unsigned long motorMillis;
unsigned long pauseMillis;
unsigned long pauseInterval = pause[0];
unsigned long motorInterval = run[0];
//********************************************^************************************************
void setup()
{
pinMode(finger2, OUTPUT);
//*****************************
//enable the motor TIMER
motorFlag = true;
//start the motor TIMER
motorMillis = millis();
digitalWrite(finger2, motorON);
} //END of setup()
//********************************************^************************************************
void loop()
{
//***************************** m o t o r T I M E R
if (motorFlag == true && millis() - motorMillis >= motorInterval)
{
//disable this TIMER
motorFlag = false;
digitalWrite(finger2, motorOFF);
//enable the pause TIMER
pauseFlag = true;
//restart the pause TIMER
pauseMillis = millis();
}
//***************************** p a u s e T I M E R
if (pauseFlag == true && millis() - pauseMillis >= pauseInterval)
{
//disable this TIMER
pauseFlag = false;
//***************
if (++count >= sizeof(pause)/sizeof(pause[0]))
{
//back to the beginning
count = 0;
}
//next pause delay
pauseInterval = pause[count];
//next motor delay
motorInterval = run[count];
//enable the motor TIMER
motorFlag = true;
//restart the motor TIMER
motorMillis = millis();
digitalWrite(finger2, motorON);
}
} //END of loop()