Friday, October 7, 2011

Tasks 18 & 19 - Arduino motor mini-board spinning both directions - software revised

/*
 * 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

1 comment:

  1. i've decided i need to simplify my software - as in 'rationalize' the way i call on various functions.

    to that end, i've created a Library of basic functions which i can 'Add' to any sketch to provide the already written & tested functionality; i just have to ensure that the parameters are correct..

    ReplyDelete