2013年12月17日 星期二

Using an Arduino's PWM to control a DC motor

I using an Arduino's PWM to control speed of the motor.

Chip : Arduino Leonardo
Module : LCD KeyPad Shield (SKU: DFR0009)
Transistors : NPN TIP122
Motor : DC 3~6V

Demo

PWM Library
Arduino/Wiring SoftPWM Library
Code
/*******************************************************
Ex_06 : Using PWM to control a DC motor
2013/12/18 SteveChang, PMP (sown813@gmail.com)
********************************************************/

// Motor Control library
#include "SoftPWM.h"
#include "SoftPWM_timer.h"

//Sample using LiquidCrystal library
#include "LiquidCrystal.h"
 
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

// LCD Backlit Control
int BacklitControl = 10;
static int LCDBacklit;

// PWM Status
int PWM_Status = 0;

// Button define
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

#define fadePin 2

// read the buttons
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor 
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 
// For V1.1 us this threshold
/*
  if (adc_key_in < 50)   return btnRIGHT;  
  if (adc_key_in < 250)  return btnUP; 
  if (adc_key_in < 450)  return btnDOWN; 
  if (adc_key_in < 650)  return btnLEFT; 
  if (adc_key_in < 850)  return btnSELECT;  
*/
 
// For V1.0 comment the other threshold and use the one below:

  if (adc_key_in < 50)   return btnRIGHT;  
  if (adc_key_in < 195)  return btnUP; 
  if (adc_key_in < 380)  return btnDOWN; 
  if (adc_key_in < 555)  return btnLEFT; 
  if (adc_key_in < 790)  return btnSELECT;   

  return btnNONE;  // when all others fail, return this...
}
 
void setup()
{
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  lcd.print("Push the buttons"); // print a simple message
  pinMode(BacklitControl, OUTPUT);
  digitalWrite(BacklitControl, LOW);
    
  // Initialize
  SoftPWMBegin();
  // Create and set pin 13 to 0 (off)
  SoftPWMSet(fadePin, 0);
  // Set fade time for pin ? to 300 ms fade-up time, and 700 ms fade-down time
  SoftPWMSetFadeTime(fadePin, 300, 700);    
}
  
void loop()
{  
  lcd.setCursor(0,1);            // move to the begining of the second line
  lcd_key = read_LCD_buttons();  // read the buttons
  SoftPWMSetPercent(fadePin, PWM_Status);
  delay(15);
  
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
     case btnRIGHT:
       {
          lcd.print("RIGHT ");
          break;
       }
     case btnLEFT:
       {
          lcd.print("LEFT   ");
          break;
       }
     case btnUP:
       {
          PWM_Status += 20;
          if(PWM_Status > 100)
             PWM_Status = 100;
          lcd.print("Status    ");
          lcd.print(PWM_Status);
          lcd.print("%     ");
          delay(500);
          break;
       }
     case btnDOWN:
       {
          PWM_Status -= 20;
          if( PWM_Status < 0 )
              PWM_Status = 0;              
          lcd.print("Status    ");
          lcd.print(PWM_Status);
          lcd.print("%     ");
          delay(500);
          break;
       }
     case btnSELECT:
       {
          lcd.print("SELECT");
          LCDBacklit = LCDBacklit == HIGH ? LOW : HIGH;
          digitalWrite(BacklitControl, LCDBacklit);
          delay(500);
          break;
       }
       case btnNONE:
       {
          lcd.print("NONE     ");
          break;
       }
   }
}

沒有留言:

張貼留言