Robot arm controlled with an old computer keyboard

They’re hard to find nowadays. Except if you go to a skip. Or a school.  I had an old keyboard lying around so followed a tutorial to get it to work with an Arduino. It’s got 4 cables – Power, Ground, Data and IRQ. You can get adapters or just take the cables apart.

You need this library - it’s pskeyboard.h

I then got my robot arm – available online for about  £35owi-535-robotic-arm-edge_1

I just took the wires for the motors rather than using the control box.

Then I used the Adafruit MotorShield V2.3

motorshield

Wire it up. The code is below. I added 2 lights to 12 and 13 so you can tell the system status.

[code]
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2
---->    http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <PS2Keyboard.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *M1 = AFMS.getMotor(1); // connects motor 1
Adafruit_DCMotor *M2 = AFMS.getMotor(2); // connects motor 2
Adafruit_DCMotor *M3 = AFMS.getMotor(3); // connects motor 3
Adafruit_DCMotor *M4 = AFMS.getMotor(4); // connects motor 4

const int DataPin = 8; // Data pin for keyboard
const int IRQpin =  3; // IRQ pin for keyboard

PS2Keyboard keyboard;

char serialData = 0;
int power1 = 130; // sets Power of arm

boolean go = false;

void setup() {
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
keyboard.begin(DataPin, IRQpin);

AFMS.begin();  // create with the default frequency 1.6KHz

//AFMS.begin(1000);  // OR with a different frequency, say 1KHz

pinMode (13,OUTPUT); // lights
pinMode (12,OUTPUT);

}

void loop ()

{  while (!go) // waits for Go to be pressed

{
lights (1,0);
if (keyboard.available())

// read the next key

{
char c = keyboard.read(); // if 'g' pressed, we can go
if (c == 'g')
{go = true;
lights (0,1);
}
else
{lights (1,0);
go = false;}
}
}

if (keyboard.available())

// read the next key

{
char c = keyboard.read(); // sends the character to the function
Serial.println(c);
movearm (c);
}

}

void lights (int a,int b)
{digitalWrite (12,a);
digitalWrite (13,b);
}

void movearm (char a)

{
if (a == 32) // space bar
{
halt ();

}
if (a == '6')
{
motor1 (power1,FORWARD); // opens arm
}
if (a == '4')
{motor1 (power1,BACKWARD);}// closes arm

if (a == '8')
{motor2 (power1,FORWARD);}//front arm up

if (a == '2')
{motor2 (power1,BACKWARD);}//front arm down

if (a == 'w')
{motor3 (power1,FORWARD);}//mid arm up

if (a == 's')
{motor3 (power1,BACKWARD);}//mid arm down

if (a == 'd')
{motor4 (power1,FORWARD);}//moves right

if (a == 'a')
{motor4 (power1,BACKWARD);}//moves left

if (a == 'h')
{go = false;
}

}

// 4 motor functions. Direction and power

void motor1 (int power, int dir)

{ M1->run(dir);
M1->setSpeed(power);
}

void motor2 (int power, int dir)

{ M2->run(dir);
M2->setSpeed(power);
}

void motor3 (int power, int dir)

{ M3->run(dir);
M3->setSpeed(power);
}

void motor4 (int power, int dir)

{ M4->run(dir);
M4->setSpeed(power);
}

void halt ()

{
M1->setSpeed(0);
M2->setSpeed(0);
M3->setSpeed(0);
M4->setSpeed(0);
}

[/code]

Be Sociable, Share!

Leave a Reply