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..

No comments:

Post a Comment