Dear people !
As we have to make a project that has to control robot with ps2 keyboard connected to arduino and control it with relay .We have some troubles.
We use ps2keyboard 2.3 libary. Five motors are controlled through 5 relay's.
We want to control each motor with keyboard. Our main problem is that when we click for example 'LEFTARROW" . Motor doesn't stop moving eventhough button isnt pressed anymore. We know the reason of course , that's why we used else loop . But it doesn't solve a problem .
Another thing is that we have to control movement from left to the middle and from right to the middle automatic. That means that after pushing key "q" robot has to move to the middle eventhough key is released.
This is our code :
#include <PS2Keyboard.h>
PS2Keyboard keyboard;
int DataPin = 2;
int IRQpin = 3;
boolean automatisch =false;
int sensorPin1 = A0;
int sensorPin2 = A1;
int sensorPin3 = A2;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int onderste_omlaag = 12;
int onderste_omhoog = 13;
int grijper_dicht = 4;
int grijper_open = 5;
int grijper_omlaag = 8;
int grijper_omhoog = 9;
int rechts = 6;
int links = 7;
int arm_omlaag = 10;
int arm_omhoog = 11;
char b =' ';
char c =' ';
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(onderste_omlaag, OUTPUT);
pinMode(onderste_omhoog,OUTPUT);
pinMode(grijper_dicht, OUTPUT);
pinMode(grijper_open,OUTPUT);
pinMode(grijper_omlaag, OUTPUT);
pinMode(grijper_omhoog,OUTPUT);
pinMode(rechts, OUTPUT);
pinMode(links,OUTPUT);
pinMode(arm_omlaag, OUTPUT);
pinMode(arm_omhoog,OUTPUT);
delay(1000);
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);
Serial.println("keyboard test ");
}
// the loop routine runs over and over again forever:
void loop() {
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
if (keyboard.available()) {
char b = keyboard.read();
if(b == 'Q') // To the right.
{
digitalWrite(rechts,HIGH);
automatisch=true;
}
if(b == 'W') // To the left
{
digitalWrite(links,HIGH);
automatisch=true;
}
if(b == 'E') // From right to the middle
{
digitalWrite(rechts,HIGH);
automatisch=true;
}
if( sensorValue1 == '1023')
{
digitalWrite(rechts,LOW);
automatisch=false;
}
if( sensorValue3 == '1023')
{
digitalWrite(rechts,LOW);
automatisch=false;
}
if( sensorValue2 == '1023')
{
digitalWrite(links,LOW);
automatisch=false;
}
//-------------------------- einde van de automatische bewegingen------------
if (automatisch == false){
char c = keyboard.read();
if( c == PS2_LEFTARROW){
digitalWrite(links,HIGH);
Serial.println("links");
}
if( c == PS2_RIGHTARROW ){
digitalWrite(rechts,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_UPARROW ){
digitalWrite(arm_omhoog,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_DOWNARROW ){
digitalWrite(arm_omlaag,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_PAGEDOWN ){
digitalWrite(grijper_omlaag,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_PAGEUP ){
digitalWrite(grijper_omhoog,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_DELETE){
digitalWrite(grijper_open,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
if( c == PS2_TAB ){
digitalWrite(grijper_dicht,HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("links");
}
delay(100);
}
else{
char c= 'a';
digitalWrite(links,LOW);
digitalWrite(rechts,LOW);
digitalWrite(arm_omhoog,LOW);
digitalWrite(arm_omlaag,LOW);
digitalWrite(onderste_omlaag,LOW);
digitalWrite(grijper_dicht,LOW);
digitalWrite(grijper_open,LOW);
digitalWrite(rechts,LOW);
digitalWrite(links,LOW);
digitalWrite(arm_omhoog,LOW);
}
}
Serial.println(b);
Serial.println(c);
//Serial.println(keyboard.read);
}
//
//delay(1000);