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

.

No comments:

Post a Comment