hi i'm working on an inspection robot project i need to controle a pan and tilt camera and 2 dc motor
i want to use a game controller (logitech, ps2, ps3, xbox) for even more option like relay or electric air valve
i've got the 2 servo to work with only a thumbstick. but controling the 2 motor not so easy..
i use an Adafruit motor sheild and after some research here is what i've comme with
i would like some help and or advice and please i am still a newbie
#include <AFMotor.h>
#include <SoftwareSerial.h>
AF_DCMotor motor(1,2, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
int motor1; // left side motor
int motor2; // right side motor
int vertical; // y
int horizontal; // x
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(11,OUTPUT); //motor 1
pinMode(3,OUTPUT); //motor 2
}
void loop()
{
horizontal = analogRead(A0); //Read the horizontal axis of the joystick
vertical = analogRead(A1); //Read the vertical axis of the joystick
delay(5);
//Back Forward
if(vertical < 400 && horizontal > 500 && horizontal < 600)
{
Serial.println( motor1.run(BACKWARD) motor2.run(BACKWARD));
while(vertical < 400)
{
vertical = analogRead(A1);
}
}
//Left
else if(horizontal < 400 && vertical > 500 && vertical < 600)
{
Serial.println(motor1.run(BACKWARD)motor2.run(FORWARD));
while(horizontal < 400)
{
horizontal = analogRead(A0);
}
}
//Right
else if(horizontal > 600 && vertical > 500 && vertical < 600)
{
Serial.println(motor1.run(FORWARD)motor2.run(BACKWARD));
while(horizontal > 600)
{
horizontal = analogRead(A0);
}
}
//Forward
else if(vertical > 600 && horizontal > 500 && horizontal < 600)
{
Serial.println(motor1.run(FORWARD)motor2.run(FORWARD));
while(vertical > 600)
{
vertical = analogRead(A1);
}
}
//Stop
else if(horizontal > 500 && horizontal < 600 && vertical > 500 && vertical < 600)
{
Serial.println(motor1.run(RELEASE)motor2.run(RELEASE));
while(horizontal > 500 && horizontal < 600 && vertical > 500 && vertical < 600)
{
horizontal = analogRead(A0);
vertical = analogRead(A1);
}
}
}