/*
* Project: MotorSpin_621task18
* Author: Jane-Maree Howard
* Date: Saturday 03/09/2011; modified Friday 07/10/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor, using my ARDUINO Library function(s)
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
- See LIB01_MotorSpin_ETMINI for more details
- Declare: Pin variables (byte), delay multiplier (int)
- Setup(): Serial.begin = 9600 baud; initialise digital control pins
- Procedure(): RotateMotor(params) takes 3 bytes (for the motor)
- Loop(): Set RotateMotor() parameters; delay for 5 seconds
*/
// digital control pins for ET-MINI DC-MOTOR
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
// delay control in seconds
int iDelay = 1000; // delay variable
void setup()
{
Serial.begin(9600); //SM @ 9600baud
//initialise digital control pins
MotorPinMode(bRight,bLeft,bEnable);
}//end setup()
void loop()
//
{
RotateMotor(bRight,bLeft,bEnable,10); // rotate motor Right..
delay(iDelay*5); // ..for 5 seconds, then..
RotateMotor(bRight,bLeft,bEnable,0); // ..stop..
delay(iDelay*5); // ..for 5 seconds, then..
}//end loop()
//END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
* Project: MotorSpin_621task19_1
* Author: Jane-Maree Howard
* Date: Saturday 03/09/2011; modified Friday 07/10/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor, using my ARDUINO Library function(s)
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
- Declare: Pin variables (byte), delay multiplier (int)
- Setup(): Serial.begin = 9600 baud;
- initialise digital control pins as outputs
- Procedure(): RotateMotor(params) takes 3 bytes (for the motor)
- Loop(): Set RotateMotor() parameters;
- delay for 5 seconds then reverse
*/
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
int iDelay = 1000; // delay variable
void setup()
{
Serial.begin(9600); //Serial connection @ 9600baud
//initialise digital control pins as outputs
MotorPinMode(bRight,bLeft,bEnable); //LIB01_MotorSpin_ETMINI
}//end setup()
void loop()
{
RotateMotor(bRight,bLeft,bEnable,10); // LIB01_MotorSpin_ETMINI: rotate motor Right..
delay(iDelay*5); // ..for 5 seconds, then..
RotateMotor(bRight,bLeft,bEnable,0); // ..stop briefly..
delay(150);
RotateMotor(bRight,bLeft,bEnable,11); // ..rotate motor Left..
delay(iDelay*5); // ..for 5 seconds
RotateMotor(bRight,bLeft,bEnable,0); // ..stop briefly..
delay(150);
}//end loop()
//END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
to see how the following is included with the above sketches, click on this link.
/*
* Project: LIB01_MotorSpin_ETMINI
* Author: Jane-Maree Howard
* Date: Friday 07/10/2011
* Platform: Arduino 22
* Purpose: To make a library function for an ET-MINI DC-MOTOR board
* Operation: Description: The motor is an ET-MINI DC-MOTOR board.
- The board's terminals are:
- EN - Enable, must be set at logic "1" for operation.
- IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
- IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
- Declare: NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
- Setup(): NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- (used only in testing)
- Procedure(): void MotorPinMode(byte, byte, byte);
- void RotateMotor(byte,byte,byte,byte);
- Loop(): NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- (used only in testing)
*/
void MotorPinMode(byte bR, byte bL, byte bEN)
{
/* initialise digital control pins as outputs.
CALLED IN MAIN SKETCH */
pinMode(bR, OUTPUT); // IN1
pinMode(bL, OUTPUT); // IN2
pinMode(bEN, OUTPUT); // EN
}//MotorPinMode()
// RotateMotor- CALLED IN MAIN SKETCH
void RotateMotor(byte bR, byte bL, byte bEN, byte bMO)
{
/* Rotate ET-MINI DC-MOTOR: bR,bL,bEN are Pins; bM en/dis-ables */
switch (bMO)
{
case 01: // PAUSE
digitalWrite(bR,0); // write '0' to motorboard pin IN1 - no rotation right
digitalWrite(bL,0); // write '0' to motorboard pin IN2 - no rotation left
digitalWrite(bEN,1); // write '1' to motorboard pin EN - enables motor
break;
case 10: // rotate RIGHT
digitalWrite(bR,1); // write '1' to motorboard pin IN1
digitalWrite(bL,0); // write '0' to motorboard pin IN2 - no rotation left
digitalWrite(bEN,1); // write '1' to motorboard pin EN - enable motor
break;
case 11: // rotate LEFT
digitalWrite(bR,0); // write '0' to motorboard pin IN1 - no rotation right
digitalWrite(bL,1); // write '1' to motorboard pin IN2
digitalWrite(bEN,1); // write '1' to motorboard pin EN - enable motor
break;
default: // STOP - disable motor
digitalWrite(bR,0); // write '0' to motorboard pin IN1 - no rotation right
digitalWrite(bL,0); // write '0' to motorboard pin IN2 - no rotation left
digitalWrite(bEN,0); // write '0' to motorboard pin EN - disable motor
}//switch()case
}//RotateMotor()
//END
This is about robots & automation. It's a companion blog to JaneMareesTech, which is about the Arduino microcontroller system & related electronics
Showing posts with label motor. Show all posts
Showing posts with label motor. Show all posts
Friday, October 7, 2011
Tasks 18 & 19 - Arduino motor mini-board spinning both directions - software revised
tasks 18 & 19: revised software showing library use
First of all i re-wrote the software for tasks 18 & 19, as you will see in the next post
i then rewrote the functions in a test LIB sketch, & when it compiled & ran ok, i deleted the setup() & loop() parts & saved it in a separate folder which i have called Arduino Libraries.
These should NOT BE CONFUSED with the C-libraries, for which you must write the line, at the head of your code:
#include Clib1.h
(which normally has those pointy brackets enclosing the .h file - but which means something entirely different in this blog's HTML so i can't put them in).
In the Arduino task sketch, click on 'Sketch' as shown on the right.
Click on 'Add File...'
You get the dialog box as shown on the left.
You have to know where your required file is, & open it..
Click on the 'Open' tab.
You now have TWO sketches open, & they can be combined to operate as one.
And when you save the original sketch, the LIBrary sketch is saved with it, so that every time you subsequently open the main sketch you get the LIB sketch opening with it.
i should add a line to the LIB sketch, describing the required parameters.
in theory (hehe - famous last words!) i should be able:
to write anything that has the right parameters for the right job;
to plug in a different motor provided the parameters fit;
change the motor type used with only minor code alterations.
this has been a bit of a holdup, & i think is worth the extra effort i've made for it.
ps: the same probably applies to Processing sketches i'd imagine..
Here's some example code - Keypress input via the Serial Monitor:
/*
* Project: LIB04_KeypressInput
* Author: Jane-Maree Howard
* Date: Saturday 08/10/2011
* Platform: Arduino 22
* Purpose: To make a library function for inputting a key-press via Serial comm link
* Operation: Description: inputs a key-press & returns an Integer
- Procedure(): int KeypressInput(); no parameters
*/
int KeypressInput()
{
/* inputs a keypress via the Serial comms link
& returns a non-zero Integer only if there is an input
CALLED IN MAIN SKETCH */
int iKey = 0;
// send data only when you receive data
if (Serial.available() > 0)
iKey = Serial.read();
return iKey;
}//KeypressInput().
//END
Used in the following posts (click to return to them if necessary):
Project 02 - Motor Project;
Task 13;
Task 14;
i then rewrote the functions in a test LIB sketch, & when it compiled & ran ok, i deleted the setup() & loop() parts & saved it in a separate folder which i have called Arduino Libraries.
These should NOT BE CONFUSED with the C-libraries, for which you must write the line, at the head of your code:
#include Clib1.h
(which normally has those pointy brackets enclosing the .h file - but which means something entirely different in this blog's HTML so i can't put them in).
In the Arduino task sketch, click on 'Sketch' as shown on the right.
Click on 'Add File...'
You get the dialog box as shown on the left.
You have to know where your required file is, & open it..
Click on the 'Open' tab.
You now have TWO sketches open, & they can be combined to operate as one.
And when you save the original sketch, the LIBrary sketch is saved with it, so that every time you subsequently open the main sketch you get the LIB sketch opening with it.
i should add a line to the LIB sketch, describing the required parameters.
in theory (hehe - famous last words!) i should be able:
to write anything that has the right parameters for the right job;
to plug in a different motor provided the parameters fit;
change the motor type used with only minor code alterations.
this has been a bit of a holdup, & i think is worth the extra effort i've made for it.
ps: the same probably applies to Processing sketches i'd imagine..
Here's some example code - Keypress input via the Serial Monitor:
/*
* Project: LIB04_KeypressInput
* Author: Jane-Maree Howard
* Date: Saturday 08/10/2011
* Platform: Arduino 22
* Purpose: To make a library function for inputting a key-press via Serial comm link
* Operation: Description: inputs a key-press & returns an Integer
- Procedure(): int KeypressInput(); no parameters
*/
int KeypressInput()
{
/* inputs a keypress via the Serial comms link
& returns a non-zero Integer only if there is an input
CALLED IN MAIN SKETCH */
int iKey = 0;
// send data only when you receive data
if (Serial.available() > 0)
iKey = Serial.read();
return iKey;
}//KeypressInput().
//END
Used in the following posts (click to return to them if necessary):
Project 02 - Motor Project;
Task 13;
Task 14;
Wednesday, September 14, 2011
Tuesday, September 13, 2011
Task 22 - use photointerrupter to output motor RPM with PWM speed control
Labels:
Arduino,
motor,
photo-interrupter,
PWM,
robots,
serial monitor,
software,
TASKS
Friday, September 9, 2011
Task 21 - use photointerrupter to output motor RPM
Labels:
Arduino,
motor,
photo-interrupter,
robots,
serial monitor,
TASKS
Thursday, September 8, 2011
analogWrite() - a way of controlling motor speed
analogWrite()
Description: Writes an analog value (PWM wave) to a pin.
Can be used to light a LED at varying brightnesses or drive a motor at various speeds.
After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle
until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).
The frequency of the PWM signal is approximately 490 Hz.
On most Arduino boards (those with the ATmega168 or ATmega328),
this function works on pins 3, 5, 6, 9, 10, and 11.
You do not need to call pinMode() to set the pin as an output before calling analogWrite().
The analogWrite function has nothing whatsoever to do with the analog pins or the analogRead function.
Syntax: analogWrite(pin, value)
Parameters: pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on).
Returns: nothing
Reference: http://arduino.cc/en/Reference/AnalogWrite
Task 20a - Motor spinning at different rates using digital PWM: software & video
This version of Task-20 is slightly different, in that the motor speeds up, then slows down again, speeds up, slows down..
The variation in software is shown below:
void loop()
{
for (int j=100; j>=0; j--) // slowly decrease mark-space ratio, increasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
for (int j=0; j<=100; j++) // slowly increase mark-space ratio, decreasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
}//end loop()
void loop()
{
for (int j=100; j>=0; j--) // slowly decrease mark-space ratio, increasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
for (int j=0; j<=100; j++) // slowly increase mark-space ratio, decreasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
}//end loop()
Wednesday, September 7, 2011
Task 20 - Motor spinning at different rates using PWM: software & output
/*
* Project: MotorSpin_621task20
* Author: Jane-Maree Howard
* Date: Wednesday 07/09/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
The board's terminals are:
EN - Enable, must be set at logic "1" for operation.
IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
Declare: Pin variables (byte), delay multiplier (int); pulse width & mark-space varibles (int)
Setup(): Serial.begin = 9600 baud; initialise digital control pins as outputs
Procedure(): RotateMotor(params) takes 3 bytes (for the motor) & an integer ('space')
Loop(): Within a 'for'-loop, set RotateMotor() parameters & steadily decrease
the mark-space ratio, thus increasing the motor speed
until mark:zero is reached, i.e. full speed;
*/
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
int iDelay = 1000; // delay variable
int iPulse = 4; // pulse width
int iFreq = 1; // (mark-)space variable
void setup()
{
Serial.begin(9600); //SM @ 9600baud
//initialise digital control pins as outputs
pinMode(bRight, OUTPUT); // IN1
pinMode(bLeft, OUTPUT); // IN2
pinMode(bEnable, OUTPUT); // EN
}//end setup()
void loop()
{
for (int j=100; j>=0; j--) // slowly decrease mark-space ratio, increasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
}//end loop()
// RotateMotor()
void RotateMotor(byte bR, byte bL, byte bE, int iSlow)
{
/*the 3 byte variables enable motor, 'iSlow' regulates speed in that
the larger the 'space' variable the longer the mark-space ratio is,
& so the slower the motor turns */
digitalWrite(bRight, bR); // write to motorboard pin IN1
digitalWrite(bLeft, bL); // write to motorboard pin IN2
digitalWrite(bEnable, bE); // write to motorboard pin EN - enable motor
delay(iPulse); // 'pulse' motor for 'iPulse' milliseconds,
// 4ms is the minimum for function
digitalWrite(bEnable, 0); // write zero to motorboard pin EN - disable motor..
delay(iFreq*iSlow); // .. for 'iFreq' milliseconds
}//RotateMotor()
//END
The mark-space ratio begins at 4 - 100,
i.e. the motor receives a 4 millisecond impulse, then is disabled (EN=0) for 100 milliseconds - the result is a rather jerky turning motion.
From experience, it was found that a 4 millisecond impulse was the minimum needed to turn the motor at all - with shorter pulses it just twitched but did not rotate.
The for-loop begins with its count variable set at 100 & decreases to 0; this variable is set as a parameter for the RotateMotor(), in which it performs the role of the 'space' variable in a mark-space ratio. As this ratio decreases (i.e. the 'space' gets shorter relative to the fixed 'mark' variable), the speed of the motor increases, until it reaches full-speed (mark-space=4-0).
The motor thus begins turning slowly, gradually speeding up until it reaches full speed; then the loop() repeats itself & the slow-increase-fast cycle begins again.
The image at right clearly shows the serial monitor output, with its constant 'mark' & varying 'space' variables, beginning at 100 & decreasing to 0.
* Project: MotorSpin_621task20
* Author: Jane-Maree Howard
* Date: Wednesday 07/09/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
The board's terminals are:
EN - Enable, must be set at logic "1" for operation.
IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
Declare: Pin variables (byte), delay multiplier (int); pulse width & mark-space varibles (int)
Setup(): Serial.begin = 9600 baud; initialise digital control pins as outputs
Procedure(): RotateMotor(params) takes 3 bytes (for the motor) & an integer ('space')
Loop(): Within a 'for'-loop, set RotateMotor() parameters & steadily decrease
the mark-space ratio, thus increasing the motor speed
until mark:zero is reached, i.e. full speed;
*/
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
int iDelay = 1000; // delay variable
int iPulse = 4; // pulse width
int iFreq = 1; // (mark-)space variable
void setup()
{
Serial.begin(9600); //SM @ 9600baud
//initialise digital control pins as outputs
pinMode(bRight, OUTPUT); // IN1
pinMode(bLeft, OUTPUT); // IN2
pinMode(bEnable, OUTPUT); // EN
}//end setup()
void loop()
{
for (int j=100; j>=0; j--) // slowly decrease mark-space ratio, increasing speed
{
Serial.print("Mark-space ratio is 4 - ");
Serial.println(j);
RotateMotor(1,0,1,j); // rotate motor Right @ set speed..
}//for()
}//end loop()
// RotateMotor()
void RotateMotor(byte bR, byte bL, byte bE, int iSlow)
{
/*the 3 byte variables enable motor, 'iSlow' regulates speed in that
the larger the 'space' variable the longer the mark-space ratio is,
& so the slower the motor turns */
digitalWrite(bRight, bR); // write to motorboard pin IN1
digitalWrite(bLeft, bL); // write to motorboard pin IN2
digitalWrite(bEnable, bE); // write to motorboard pin EN - enable motor
delay(iPulse); // 'pulse' motor for 'iPulse' milliseconds,
// 4ms is the minimum for function
digitalWrite(bEnable, 0); // write zero to motorboard pin EN - disable motor..
delay(iFreq*iSlow); // .. for 'iFreq' milliseconds
}//RotateMotor()
//END
The mark-space ratio begins at 4 - 100,
i.e. the motor receives a 4 millisecond impulse, then is disabled (EN=0) for 100 milliseconds - the result is a rather jerky turning motion.
From experience, it was found that a 4 millisecond impulse was the minimum needed to turn the motor at all - with shorter pulses it just twitched but did not rotate.
The for-loop begins with its count variable set at 100 & decreases to 0; this variable is set as a parameter for the RotateMotor(), in which it performs the role of the 'space' variable in a mark-space ratio. As this ratio decreases (i.e. the 'space' gets shorter relative to the fixed 'mark' variable), the speed of the motor increases, until it reaches full-speed (mark-space=4-0).
The motor thus begins turning slowly, gradually speeding up until it reaches full speed; then the loop() repeats itself & the slow-increase-fast cycle begins again.
The image at right clearly shows the serial monitor output, with its constant 'mark' & varying 'space' variables, beginning at 100 & decreasing to 0.
Task 20 - Motor spinning at different rates using PWM
To the right is an image of the Fritzing diagram (breadboarding) - the same as Tasks 18 & 19.
Below it is an image of the actual ET-MINI DC-MOTOR board.
On it is mounted the motor (which is bidirectional), circuitry known as an H-Bridge (for protecting the circuitry connected to it from any induction spikes), 2 direction-indicating LEDs
(L & R), & 2 photo-interrupters labelled
OPA & OPB (for detecting pulses for measuring motor speed etc).
IN this task, we attempt to regulate the motor speed using Pulse Width Modulation (PWM).
This will involve inputting a pulse at varying intervals - the longer the interval the slower the speed.
Clearly, the input pulses must be frequent enough to keeping the motor spinning, but not so frequent as to cause the motor to hit its maximum speed.
The video shows the motor in action, starting slowly & speeding up.
Below it is an image of the actual ET-MINI DC-MOTOR board.
On it is mounted the motor (which is bidirectional), circuitry known as an H-Bridge (for protecting the circuitry connected to it from any induction spikes), 2 direction-indicating LEDs
(L & R), & 2 photo-interrupters labelled
OPA & OPB (for detecting pulses for measuring motor speed etc).
IN this task, we attempt to regulate the motor speed using Pulse Width Modulation (PWM).
This will involve inputting a pulse at varying intervals - the longer the interval the slower the speed.
Clearly, the input pulses must be frequent enough to keeping the motor spinning, but not so frequent as to cause the motor to hit its maximum speed.
The video shows the motor in action, starting slowly & speeding up.
Saturday, September 3, 2011
Tasks 18 & 19 - Arduino motor mini-board spinning both directions - software
/*
* Project: MotorSpin_621task19
* Author: Jane-Maree Howard
* Date: Saturday 03/09/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
The board's terminals are:
EN - Enable, must be set at logic "1" for operation.
IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
Declare: Pin variables (byte), delay multiplier (int)
Setup(): Serial.begin = 9600 baud; initialise digital control pins as outputs
Procedure(): RotateMotor(params) takes 3 bytes (for the motor) & an integer (delay)
Loop(): Set RotateMotor() parameters; delay for 10 seconds
*/
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
int iDelay = 1000; // delay variable
void setup()
{
Serial.begin(9600); //SM @ 9600baud
//initialise digital control pins as outputs
pinMode(bRight, OUTPUT); // IN1
pinMode(bLeft, OUTPUT); // IN2
pinMode(bEnable, OUTPUT); // EN
}//end setup()
void loop()
{
RotateMotor(1,0,1,5); // rotate motor Right for 5 seconds
delay(iDelay*10); // wait 10 seconds
//RotateMotor(0,1,1,3); // rotate motor Left for 3 seconds
//delay(iDelay*10); // wait 10 seconds
}//end loop()
// RotateMotor() - rotates motor in one direction
void RotateMotor(byte bR, byte bL, byte bE, int iSecs)
{
digitalWrite(bRight, bR); // write to motorboard pin IN1
digitalWrite(bLeft, bL); // write to motorboard pin IN2
digitalWrite(bEnable, bE); // write to motorboard pin EN - enable motor
delay(iDelay*iSecs); // delay for 'iSecs' seconds
digitalWrite(bEnable, 0); // write zero to motorboard pin EN - disable motor
}//RotateMotor()
//END
Note the line in the loop() that reads rotateMotor(0,1,1,3);
This rotates the motor Left for 3 seconds.
Be careful NOT to enable BOTH directions at once.
There should probably some kind of 'if ()'-clause to cope with that..
* Project: MotorSpin_621task19
* Author: Jane-Maree Howard
* Date: Saturday 03/09/2011
* Platform: Arduino 22
* Purpose: To demonstrate control of a DC motor
* Operation: Description: The motor is a ET-MINI DC-MOTOR board.
The board's terminals are:
EN - Enable, must be set at logic "1" for operation.
IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
Declare: Pin variables (byte), delay multiplier (int)
Setup(): Serial.begin = 9600 baud; initialise digital control pins as outputs
Procedure(): RotateMotor(params) takes 3 bytes (for the motor) & an integer (delay)
Loop(): Set RotateMotor() parameters; delay for 10 seconds
*/
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 8; // IN2 is pin 8 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
int iDelay = 1000; // delay variable
void setup()
{
Serial.begin(9600); //SM @ 9600baud
//initialise digital control pins as outputs
pinMode(bRight, OUTPUT); // IN1
pinMode(bLeft, OUTPUT); // IN2
pinMode(bEnable, OUTPUT); // EN
}//end setup()
void loop()
{
RotateMotor(1,0,1,5); // rotate motor Right for 5 seconds
delay(iDelay*10); // wait 10 seconds
//RotateMotor(0,1,1,3); // rotate motor Left for 3 seconds
//delay(iDelay*10); // wait 10 seconds
}//end loop()
// RotateMotor() - rotates motor in one direction
void RotateMotor(byte bR, byte bL, byte bE, int iSecs)
{
digitalWrite(bRight, bR); // write to motorboard pin IN1
digitalWrite(bLeft, bL); // write to motorboard pin IN2
digitalWrite(bEnable, bE); // write to motorboard pin EN - enable motor
delay(iDelay*iSecs); // delay for 'iSecs' seconds
digitalWrite(bEnable, 0); // write zero to motorboard pin EN - disable motor
}//RotateMotor()
//END
Note the line in the loop() that reads rotateMotor(0,1,1,3);
This rotates the motor Left for 3 seconds.
Be careful NOT to enable BOTH directions at once.
There should probably some kind of 'if ()'-clause to cope with that..
Tuesday, August 30, 2011
Project 2 - Arduino Motor Project - Serial input speed & direction control
Here's the Fritzing diagram, showing pins 9 & 10 used since they have the Pulse Width Modulation (PWM) option enabled on them./*
* Project: MotorSpin_621project02_1
* Author: Jane-Maree Howard
* Date: Tuesday 11/10/2011
* Platform: Arduino 22
* Purpose: To demonstrate Serial input motor control
* Operation: Description: The ET-MINI DC-MOTOR board has an H-bridge & a DC motor
- The board's terminals are:
- EN - Enable, must be set at logic "1" for operation.
- IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
- IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
- Arduino Libraries: LIB04_KeypressInput; LIB06_PWMmotorSpeed_ETMINI;
- Setup(): Serial @ 9600 baud; Print heading;
- Loop(): Via Serial Monitpr, input a PAIR of characters, e.g. r7, ss, L9;
- Press 'Enter' or 'Send';
- Motor direction & speed range are outputted &..
- ..PWMmotorControl(bRight, bLeft, bEnable, bMotor, bSpeed) operates
- the motor according to instruction-pair;
*/
int iKeyPress = 0; // for incoming serial data
char chInput; // for 'char' parameters
// digital control pins for ET-MINI DC-MOTOR
byte bRight = 10; // IN1 is pin 10 - rotate Right
byte bLeft = 9; // IN2 is pin 9 - rotate Left
byte bEnable = 13; // EN is pin 13 - Enable rotation, must = '1'
byte bMotor = 0; // in LIB06_PWMmotorControl(), '0'=STOP, '1'=RIGHT, '11'=LEFT
byte bSpeed = 0; // in ditto, PWM duty cycle parameter - initialised to "stop"
void setup()
{
Serial.begin(9600); //SM @ 9600 baud
pinMode(bEnable, OUTPUT); // EN - ENable pin on motorboard
pinMode(bRight, OUTPUT); // IN1 - 'RIGHT' pin on motorboard
pinMode(bLeft, OUTPUT); // IN2 - 'LEFT' pin on motorboard
Serial.println("\nEnter your control characters in pairs e.g.: R5, ss");
}//end setup()
void loop()
{
// first input Motor Direction, STOP, RIGHT, or LEFT
iKeyPress = KeypressInput();
if (iKeyPress !=0)
{
Serial.print("\nInput direction\t");
chInput = (char)iKeyPress;
Serial.println(chInput, BYTE);
switch (chInput)
{
case ('s'):
bMotor = 0;
break;
case ('S'):
bMotor = 0;
break;
case ('r'):
bMotor = 1;
break;
case ('R'):
bMotor = 1;
break;
case ('l'):
bMotor = 11;
break;
case ('L'):
bMotor = 11;
break;
default: bMotor = 0;
}//switch()
// now input Motor Speed on a scale of 1-9
iKeyPress = KeypressInput();
Serial.print("Input speed\t");
chInput = (char)iKeyPress;
Serial.println(chInput, BYTE);
switch (chInput)
{
case ('1'):
bSpeed = 50;
break;
case ('2'):
bSpeed = 75;
break;
case ('3'):
bSpeed = 100;
break;
case ('4'):
bSpeed = 125;
break;
case ('5'):
bSpeed = 150;
break;
case ('6'):
bSpeed = 175;
break;
case ('7'):
bSpeed = 200;
break;
case ('8'):
bSpeed = 225;
break;
case ('9'):
bSpeed = 255;
break;
default: bMotor = 0;
}//switch()
}//if()
/* now that control characters have been entered e.g.L7,
call PWMmotorControl with all parameters present
*/
PWMmotorControl(bRight, bLeft, bEnable, bMotor, bSpeed);
}//end loop()
//END
The control characters must be entered as a pair,
otherwise it will not function.
The direction can be upper or lower case.
The duty cycle (input speed 1-9) can in theory range from 0-255, but in practice, anything < 50 won't fire the motor up.
255 represents 100%, or full-speed.
/*
* Project: LIB06_PWMmotorSpeed_ETMINI
* Author: Jane-Maree Howard
* Date: Tuesday 11/10/2011
* Platform: Arduino 22
* Purpose: To use PWM speed-control for an ET-MINI DC-MOTOR board
* Operation: The ET-MINI DC-MOTOR board has an H-bridge & a DC motor
- The board's terminals are:
- EN - Enable, must be set at logic "1" for operation.
- IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
- IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
- Declare: NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
- Setup(): NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- (used only in testing)
- Procedure(): void PWMmotorControl(byte,byte,byte,byte,byte);
- Loop(): NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- (used only in testing)
*/
// RotateMotor - CALLED IN MAIN SKETCH
void PWMmotorControl(byte bR, byte bL, byte bEN, byte bMO, byte bSPEED)
{
/*
Rotate ET-MINI DC-MOTOR: bR,bL,bEN are Pins; bMO en/dis-ables;
bSPEED takes values 0-255 as part of the PWM duty cycle;
analogWrite() is a PWM operation on a Digital pin,
. & has nothing to do with Analog pins or AnalogRead()
*/
// pause motor to allow change of direction
delay(20); // delay 20 milliseconds
digitalWrite(bEN,1); // write '1' to motorboard pin EN - enable motor
switch (bMO)
{
case 0: // STOP - disable motor
digitalWrite(bEN,0); // write '0' to motorboard pin EN
break;
case 1: // rotate RIGHT @
digitalWrite(bL,0); // write '0' to motorboard pin IN2 - no rotation left
analogWrite(bR,bSPEED); // write to motorboard pin IN1
break;
case 11: // rotate LEFT @
digitalWrite(bR,0); // write '0' to motorboard pin IN1 - no rotation right
analogWrite(bL,bSPEED); // write to motorboard pin IN2
break;
default: // STOP - disable motor
digitalWrite(bEN,0); // write '0' to motorboard pin EN - disable motor
}//switch()case
}//PWMmotorControl()
//END
Go to the following link for the operating software
/*
* Project: LIB04_KeypressInput
* Author: Jane-Maree Howard
* Date: Saturday 08/10/2011
* Platform: Arduino 22
* Purpose: To make a library function for inputting a key-press via Serial comm link
* Operation: Description: inputs a key-press & returns an Integer
.
Task 19 - Arduino motor mini-board spinning both directions
The Hardware & Software for this task are practically identical.
Only the software features a minor difference, namely, in the line:
rotateMotor(0,1,1,3);
This rotates the motor Left for 3 seconds.
Only the software features a minor difference, namely, in the line:
rotateMotor(0,1,1,3);
This rotates the motor Left for 3 seconds.
Task 18 - Arduino motor mini-board spinning one direction
Above is a Fritzing diagram for controlling the ET-MINI DC-MOTOR board.
EN must be at logic "1" to enable the motor function.
IN1 at logic "1" (with IN2 at logic "0") rotates the motor to the Right.
IN2 at logic "1" (with IN1 at logic "0") rotates the motor to the Left.
To rotate Right, set EN & IN1 @ logic "1" & IN2 @ "0".
On either side (near the top, see board) are indicator LEDs.
Below are some pictures of the actual board:
Also shown are +Vcc, GND (obscured)
& two other terminals to be used later.
The software is posted separately.
EN must be at logic "1" to enable the motor function.
IN1 at logic "1" (with IN2 at logic "0") rotates the motor to the Right.
IN2 at logic "1" (with IN1 at logic "0") rotates the motor to the Left.
To rotate Right, set EN & IN1 @ logic "1" & IN2 @ "0".
On either side (near the top, see board) are indicator LEDs.
Below are some pictures of the actual board:
![]() |
| Terminal block showing IN1, EN, & IN2 |
![]() |
| ET-MINI DC-MOTOR |
Also shown are +Vcc, GND (obscured)
& two other terminals to be used later.
The software is posted separately.
Subscribe to:
Posts (Atom)








