Start dc motors at different times

Hi all

I'm new around here and at the moment trying to make a project work without success. I want to have up to 4 dc motors accelerating from 0 to 255 and then reversing to 0 again, this part I have managed to do, but my intention is to have them to start with a 10 second delay in between them.

My question is, is this possible to do? If so could you guys help me please?

Thanks in advanced

Hi, and welcome to the forum.

Nunoplima: ...my intention is to have them to start with a 10 second delay in between them. My question is, is this possible to do?

If so could you guys help me please?

Sure.

Here we help you with YOUR code.

You need to understand millis() timing for this. Best place to start is the BlinkWithoutDelay sketch from the examples of the IDE.

Start with one motor and one millis() delay. Add more motors if it works the way you want. Post your code if you have problems. Read the forum rules before you do. Leo..

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

Hi Guys

Thanks for the advice and apologies for my rush in obtain an answer. The code I have made so far is as bellow :

int MOTOR1=3;
int MOTOR2=11;
int i;

void setup() {
// put your setup code here, to run once:
pinMode(MOTOR1, OUTPUT);
pinMode(MOTOR2, OUTPUT);
Serial .begin(9600);
analogWrite(MOTOR1, 0);
analogWrite(MOTOR2, 0);
}

void loop() {
// put your main code here, to run repeatedly:
for(i=0; i<=250; i++){
analogWrite(MOTOR1, i);
Serial.println(i);
delay(500);
analogWrite(MOTOR2, i);
Serial.println(i);
delay(500);

}
for(i=250; i<=250; i++){
analogWrite(MOTOR1, i);
Serial.println(i);
delay(10000);
analogWrite(MOTOR2,i);
Serial.println(i);
delay(10000);
}
for(i=250; i>=0; i–){
analogWrite(MOTOR1, i);
Serial.println(i);
delay(500);
analogWrite(MOTOR2,i);
Serial.println(i);
delay(500);
}
}[/code]

I have followed the steps from the initial forum post and hope I’m doing it wright.
With this code both motors are starting and finishing at the same time, I have tried to delay motor 2 but it just messes up every thing, after a quick research on google I have found that using the variable IF instead of delay would achieve better results.

Hope you guys can help me, at the moment I have lost count of how many different coddings I have saved for this experiment.

Thanks in advance.

Hi Guys Me again,sorry, have messed up the above post but don't know how to get the coding on the window

I'm really sorry Thanks for all you patience with me.

delay(10000); analogWrite(MOTOR2,i); Serial.println(i); delay(10000);

Code execution stops here dead in it's tracks for 2*10 seconds. The other motor is not updated during these 20 seconds.

That's why I told you to look at Blink*Without*Delay. Leo..

Read Robin2’s discussion how to do several things at the same time:

Here is an example I think should help with the BWD theory:

//swap the comment marks // on the next two lines to see a comparison, lines 56 and 57

//usingDelay();
usingMillis();

/*
  GetRidOfDelay.ino
  LarryD
  Version YY/MM/DD
  1.00    17/02/01  Running code

*/

//LED wiring:
// +5V---220 ohm resistor---LED anode---LED cathode---Arduino pin

const byte heartBeatLED            = 13;    //used to see if the sketch is blocking
const unsigned long heartBeatDelay = 100UL;
const byte myLED                   = 12;    //wired so a LOW turns the LED on

//SRAM variables
unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long myLEDmillis;
unsigned long waitTime = 0;
unsigned long currentMicros;

byte LEDstate = 0;

//                          s e t u p ( )
//**********************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);

  pinMode(myLED, OUTPUT);
  digitalWrite(myLED, HIGH);        //HIGH is LED off

} //                 E n d  o f  s e t u p ( )

//                          l o o p ( )
//**********************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing
  currentMicros = micros(); //for mirco second timing

  //***************************
  //HeartBeat LED, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    heartBeatMillis = heartBeatMillis + heartBeatDelay; //reset timing

    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //***************************
  //swap the comment marks // on the next two lines to see a comparison

  //usingDelay();
  usingMillis();


} //                    E n d  o f  l o o p ( )

//======================================================================
//                        F U N C T I O N S
//======================================================================

//                     u s i n g D e l a y ( )
//**********************************************************************
void usingDelay()
{
  digitalWrite(myLED, LOW);  //LOW is LED on
  delay(2000);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(1000);
  digitalWrite(myLED, LOW);  //LOW is LED on
  delay(500);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(250);

}//                E n d  o f  u s i n g D e l a y ( )

//                     u s i n g M i l l i s ( )
//**********************************************************************
void usingMillis()
{
  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }

  //Yes, it is now time
  myLEDmillis = currentMillis;      //reset timing

  //state code
  switch (LEDstate)
  {
    case 0:
      digitalWrite(myLED, LOW);     //turn LED on
      LEDstate = 1;                 //next state
      waitTime = 2000UL;            //setup the wait amount
      break;

    case 1:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 2;                //next state
      waitTime = 1000UL;           //setup the wait amount
      break;

    case 2:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 3;                //next state
      waitTime = 500UL;            //setup the wait amount
      break;

    case 3:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 0;                //next state
      waitTime = 250UL;            //setup the wait amount
      break;

  } //End of switch/case

}//                E n d  o f  u s i n g M i l l i s ( )

//======================================================================
//                        E N D  O F  C O D E
//======================================================================

Thanks guys for the quick Kelly's. Will try this tonight once get home. Hope to be able to do it. Thanks