Instructables

How to write code for an Arduino with the Arduino motor shield.

What would the code for an Arduino, with the Arduino motorsheild, look like to run a 6V 300 mA DC motor? I want to stop and start the motor at a certain frequency (I don't have to reverse the direction of the motor). For example at every 5 minute interval I want it to run the motor for 2 minutes and stop it for 3 minutes. Any help or advice is appreciated thanks.

frollard2 years ago
That should be a very simple program to make.

The motor shield just expects a pin to go high or low to enable the output to the motor (if I recall).

Handling the timing aspect in code should be very easy;
There are 3 easy methods I can think of to handle the timing;

assuming your sketch is already set up with something like
motorPin = 6;
pinmode(motorPin,OUTPUT);

you should be able to set in loop()

digitalWrite(motorPin,HIGH); //turn motor on
delay(500); //wait
digitalWrite(motorPin,LOW); //turn motor off
delay(500); //wait

(this is the example from 'blink' essentially).

The EASIEST method would be to change the delay values to the 120 seconds (120000ms) on, and 180seconds off (180000ms).

You can feed delay(); an unsigned long value, so there is no fear of sending too large a number in this instance.

This method is BRUTALLY inefficient, because during the delay(), you can do NOTHING else with the processor. This is fine so long as you ONLY want to switch the motor on and off, no input/output.

A more efficient method, but a bit more convoluted is to use the millis(); function. calling millis(); returns the number of milliseconds since the arduino started up. Knowing that time value means you can set a timecode of when you want the function to fire, and keep checking that millis value (along with all your other code you may be doing).

might not compile - I'm just typing this, so excuse typos.

//Written by Frollard May 2012
//Should toggle a pin on and off without using the delay(); command
//CC NC SA

void setup()
{
int motorPin = 8; //define which pin to use
pinmode(motorPin,OUTPUT); //set up motor pin
boolean motorStatus = FALSE; //motor off
digitalWrite(motorPin,motorStatus); //turn motor off
unsigned long onDuration = 120000; //how many ms to stay on
unsigned long offDuration = 180000; //how many ms to stay off
unsigned long NextToggle = millis() + offDuration; //next time we do anything is in 180000 ms from power on
} //end setup


void loop()
{
if (millis() > NextToggle) //check to see if current time exceeds the timestamp we set.
{//time has elapsed
MotorToggle(); //changes the motor and updates the timer
}
else
{ //time hasn't elapsed
//don't care, don't do anything
} //end if
//*************here is where all the other code you could want would go

//*************this gets looped regardless of whether we are waiting or not!
} //end loop


void motorToggle(){
motorStatus = !motorStatus; //toggle if motor was on/off
digitalwrite(motorPin,motorStatus); //set the motor to whatever
if (motorStatus)
{//motor is turned on
NextToggle = millis() + onDuration;
}
else
{ //motor is turned off
NextToggle = millis() + offDuration;
} //end if
}//end motortoggle
So, I initially couldn't find the motorshield pinout; I looked a bit closer, and found http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
which explains all the pinouts
Function  Channel A  Channel B
Direction  Digital 12  Digital 13
Speed (PWM)  Digital 3  Digital 11
Brake  Digital 9  Digital 8
Current Sensing  Analog 0  Analog 1

"In order to get the board to do anything, you need to initialize the motor channel by toggling three parameters:

    First you need to set the motor direction (polarity of the power supply) by setting it either HIGH or LOW.
    Then you need to disengage the brake pin for the motor channel by setting it to LOW.
    Finally, to get the motor to start moving, you need to set the speed by sending a PWM command (analogWrite) to the appropriate pin.


If you do not do all three of these things, the motor will not turn on."

So, we need

MotorADirection
MotorABrake
MotorASpeed

declared...

resulting in:

//Written by Frollard May 2012
//Should toggle a pin on and off without using the delay(); command
//CC NC SA

void setup()
{
int motorADirectionPin = 12; //Boolean, high = forward
int motorABrakePin = 9; //boolean, low = allow to run
int motorASpeedPin = 3; //pwm output for speed, 0-1023
pinmode(motorADirectionPin,OUTPUT); //set up motor pin
pinmode(motorABrakePin,OUTPUT); //set up motor pin
pinmode(motorADirectionPin,OUTPUT); //set up motor pin

boolean motorStatus = FALSE; //motor off
analogWrite(motorASpeedPin,0); //turn motor off (speed = 0)
digitalWrite(motorADirectionPin,HIGH); //set motor to forward direction
digitalWrite(motorABrakePin,LOW); //disable brake, leave it that way.
unsigned long onDuration = 120000; //how many ms to stay on
unsigned long offDuration = 180000; //how many ms to stay off
unsigned long NextToggle = millis() + offDuration; //next time we do anything is in 180000 ms from power on
} //end setup


void loop()
{
if (millis() > NextToggle) //check to see if current time exceeds the timestamp we set.
{//time has elapsed
MotorToggle(); //changes the motor and updates the timer
}
else
{ //time hasn't elapsed
//don't care, don't do anything
} //end if
//*************here is where all the other code you could want would go

//*************this gets looped regardless of whether we are waiting or not!
} //end loop


void motorToggle(){
motorStatus = !motorStatus; //toggle if motor was on/off
if (motorStatus)
{//motor is turned on
analogWrite(motorASpeedPin,1023); //set the motor to ON 1023
NextToggle = millis() + onDuration;
}
else
{ //motor is turned off
analogWrite(motorASpeedPin,0); //set the motor to OFF 0
NextToggle = millis() + offDuration;
} //end if

}//end motorToggle
thecoonskin (author)  frollard2 years ago
thanks thats exactly what I was looking for.
oops, I just noticed a problem;

in motorToggle()

...the analogwrite values should be 255 and 0, not 1023 and 0 -- analog write only takes a number from 0-255. It works here because 1023 is all 1's in binary so its tricked into thinking it has the right number.

remember, don't forget to mark answered if this helps :)