Programing Motor to Run and Delay on Schedule

Hi,

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);
}

}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

You have created a timer that responds to a set delay of 1000ms. You need to tweak it.

Think about how the timer works

  1. You note the time (startTime)
  2. You check how much time has elapsed each time through loop (currentTime-startTime)
  3. 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.

Can you follow this sketch ?

#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()




not sure how you tell if the motor is on for 20 msec. i tested with it on for 500

const byte finger2 = 9;

struct Event {
    byte            on;
    unsigned long   period;
};

enum { Off = LOW, On = HIGH };

#define M_ON  500

Event events [] = {
    { On,  M_ON },
    { Off, 2000 },
    { On,  M_ON },
    { Off,  500 },
    { On,  M_ON },
    { Off, 3000 },
    { On,  M_ON },
    { Off, 1000 },
};
const unsigned Nevents = sizeof (events) / sizeof (Event);

unsigned  idx;

unsigned long msecPeriod;
unsigned long msecLst;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (msec - msecLst >= msecPeriod)  {
        Serial.println (idx);

        msecLst    = msec;
        msecPeriod = events [idx].period;

        digitalWrite (finger2, events [idx].on);

        if (Nevents <= ++idx)
            idx = 0;
    }
}

void setup ()
{
    Serial.begin (9600);
    pinMode (finger2, OUTPUT);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.