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.

No comments:

Post a Comment