總網頁瀏覽量

2012年8月4日 星期六

Arduino PS2 Keyboard Controls DC Motor

PS2 keyboard connected to Arduino to control a DC motor. The motor speed will be increased 10 units when pressed UPARROW key once, decreased 10 units when pressed DOWNARROW key once.

DIY Material:
1. Motoduino (Arduino + L293D)
2. PS2 Keyboard
3. 5V DC Motor
4. 9V battery
5. Female PS2 Connector

Description:
    Pin connection:
    PS2         Ardunio
      1     -->     3
      2     -->     No Connection
     3      -->     GND
     4      -->     Vcc
     5      -->    2
     6      -->    No Connection  






Arduino  Sketch:

#include <PS2Keyboard.h>

const int DataPin = 3;  // PS2 data pin
const int IRQpin =  2;  // PS2 clock pin

PS2Keyboard keyboard;
const int Motor_E1 = 5; // digital pin 5 of Arduino (PWM)    
const int Motor_E2 = 6;  // digital pin 6 of Arduino (PWM)  
const int Motor_M1 = 7;     // digital pin 7 of Arduino
const int Motor_M2 = 8;    // digital pin 8 of Arduino

int motorSpeed = 0;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(57600);
  Serial.println("Keyboard Ready:");
  
  pinMode(Motor_M1, OUTPUT);

}

void loop() {
  
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();
    
    switch(c)
    {
      case PS2_LEFTARROW:
      case PS2_RIGHTARROW:
      case PS2_UPARROW:
                motorSpeed = motorSpeed + 10;
                if(motorSpeed > 255)
                    motorSpeed = 255;
                break;
      case PS2_DOWNARROW:
                motorSpeed = motorSpeed - 10;
                if(motorSpeed <= 0 )
                    motorSpeed = 0;
                break;
      case PS2_ENTER:
      case PS2_TAB:
      case PS2_ESC:
      case PS2_PAGEDOWN:
      case PS2_PAGEUP:
      case PS2_DELETE:
                  break;
    }
    
    motor(0, motorSpeed);
  }
}

void motor(byte flag, int motorspeed)
{
  digitalWrite( Motor_M1, HIGH);
  analogWrite( Motor_E1, motorspeed);
  
  Serial.println("motor speed: ");
  Serial.println(motorspeed);
}

-------------------------------------------------


Motoduino Information: http://motoduino.com
My Email : sinocgtchen@gmail.com

沒有留言:

張貼留言