I built a robot that moves in all directions.it sprays water in desired direction. what i have used for the movement of the bot is 6 DC 12v motors. The water is sprayed by two pumps and its direction by a servo. Control is done using a PS2 wireless joystick.
The problem is there are aweful delays and unnecessary commands are sometimes carried out.
Please take a lookat my code.
HELP ME
int dirl=2;
int pwml=3;
int dirr=4;
int pwmr=5;
int pl=7;
int pr=8;
int a;
char x;
int i=90;
int error = 0;
byte type = 0;
byte vibrate = 0;
#include <Servo.h>
#include <PS2X_lib.h> //for v1.6
PS2X ps2x; // create PS2 Controller Class
Servo myservo;
void setup() {
Serial.begin(9600);
error = ps2x.config_gamepad(13,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring");
else if(error == 2)
Serial.println("Controller found but not accepting commands");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
case 2:
Serial.println("GuitarHero Controller Found");
break;
}
pinMode(dirl, OUTPUT);
pinMode(pwml, OUTPUT);
pinMode(dirr, OUTPUT);
pinMode(pwmr, OUTPUT);
pinMode(pr, OUTPUT);
pinMode(pl, OUTPUT);
myservo.attach(6);
myservo.write(i);
}
void loop() {
ps2x.read_gamepad();
if(ps2x.Button(PSB_BLUE))
{
brake();
}
else if(ps2x.Button(PSB_PAD_UP))
{
Serial.println("Forward");
forward();
}
else if(ps2x.Button(PSB_PAD_RIGHT))
{
Serial.println("Right");
right();
}
else if(ps2x.Button(PSB_PAD_LEFT))
{
Serial.println("Left");
left();
}
else if(ps2x.Button(PSB_PAD_DOWN))
{
Serial.println("Reverse");
reverse();
}
else if(ps2x.Button(PSB_L1))
{
Serial.println("Left Pump ");
pumpl();
}
else if(ps2x.Button(PSB_R1))
{
Serial.println("Right Pump");
pumpr();
}
else if(ps2x.Button(PSB_R2)) //will be TRUE if button was JUST pressed
{
myservo.attach(6);
Serial.println("Spray right");
i--;
myservo.write(i);
delay(15);
}
else if(ps2x.Button(PSB_L2)) //will be TRUE if button was JUST released
{
myservo.attach(6);
Serial.println("Spray left");
i++;
myservo.write(i);
delay(15);
}
else brake();
}
void pumpl()
{
digitalWrite(pl,HIGH);
}
void pumpr()
{
digitalWrite(pr,HIGH);
}
void forward()
{
digitalWrite(dirl, LOW);
digitalWrite(pwml,HIGH);
digitalWrite(dirr, LOW);
digitalWrite(pwmr,HIGH);
}
void reverse()
{
digitalWrite(dirl, HIGH);
digitalWrite(pwml,HIGH);
digitalWrite(dirr, HIGH);
digitalWrite(pwmr,HIGH);
}
void right()
{
digitalWrite(dirl,LOW);
digitalWrite(pwml,HIGH);
digitalWrite(dirr, HIGH);
digitalWrite(pwmr,HIGH);
}
void left()
{
digitalWrite(dirl, HIGH);
digitalWrite(pwml,HIGH);
digitalWrite(dirr, LOW);
digitalWrite(pwmr,HIGH);
}
void brake()
{
digitalWrite(pwml,LOW);
digitalWrite(pwmr,LOW);
digitalWrite(pl,LOW);
digitalWrite(pr,LOW);
}