Created
July 27, 2013 01:45
-
-
Save RCTank/6093318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Glue together PS2X controller code with DFRobot Motor Shield code | |
// | |
#include <PS2X_lib.h> //for v1.6 | |
PS2X ps2x; // create PS2 Controller Class | |
//right now, the library does NOT support hot pluggable controllers, meaning | |
//you must always either restart your Arduino after you conect the controller, | |
//or call config_gamepad(pins) again after connecting the controller. | |
int error = 0; | |
byte type = 0; | |
byte vibrate = 0; | |
//Arduino PWz Speed Control for DFRobot Motor Shield (default pins) | |
int M1 = 12; | |
int M2 = 13; | |
int E1 = 3; | |
int E2 = 11; | |
int lmotor = 0; | |
int rmotor = 0; | |
int duration, distance; | |
#define trigPin 10 | |
#define echoPin 2 | |
// These are all setting pin numbers and values with a certain name. | |
void setup() | |
{ | |
Serial.begin(57600); | |
// set pin modes for Arduino Motor Shield | |
pinMode(M1, OUTPUT); // This sets motor 1 pin (pin 12) as an output to the motor. | |
pinMode(M2, OUTPUT); // This sets motor 2 pin (pin 13) as an output to the motor. | |
Serial.begin (9600); | |
pinMode(trigPin, OUTPUT); //This sets the trigPin (pin 10) as the output to the ultrasonic sensor. | |
pinMode(echoPin, INPUT); //This sets the echoPin (pin 2) as the input from the ultrasonic sensor. | |
error = ps2x.config_gamepad(7,5,4,6, 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"); | |
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;"); | |
Serial.println("holding L1 or R1 will print out the analog stick values."); | |
Serial.println("Go to www.billporter.info for updates and to report bugs."); | |
} | |
else if(error == 1) | |
{ | |
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips"); | |
} | |
else if(error == 2) | |
{ | |
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips"); | |
} | |
else if(error == 3) | |
{ | |
Serial.println("Controller refusing to enter Pressures mode, may not support it. "); | |
} | |
type = ps2x.readType(); | |
if (type != 1) | |
{ | |
Serial.println("warning: DualShock Controller Not Found!"); | |
} | |
} | |
void loop() | |
{ | |
if(error == 1) //skip loop if no controller found | |
return; | |
if (type == 1) | |
{ | |
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed | |
lmotor = 0; | |
if (ps2x.Button(PSB_L1)) // this tells the Arduino that when I press L1, the left motor moves forward at full speed. | |
lmotor = 255; | |
if (ps2x.Button(PSB_L2)) // this tells the Arduino that when I press L2, the left motor moves backwards at full speed. | |
lmotor = -255; | |
rmotor = 0; | |
if (ps2x.Button(PSB_R1)) // this tells the Arduino that when I press R1, the right motor moves forward at full speed. | |
rmotor = 255; | |
if (ps2x.Button(PSB_R2)) // this tells the Arduino that when I press R2, the right motor moves backwards at full speed. | |
rmotor = -255; | |
} | |
else | |
{ | |
lmotor = 0; | |
rmotor = 0; | |
} | |
// update motors | |
if (lmotor < 0) | |
{ | |
digitalWrite(M1, LOW); | |
analogWrite(E1, -lmotor); //PWM Speed Control | |
} | |
else | |
{ | |
digitalWrite(M1, HIGH); | |
analogWrite(E1, lmotor); //PWM Speed Control | |
} | |
if (rmotor < 0) | |
{ | |
digitalWrite(M2, LOW); | |
analogWrite(E2, -rmotor); //PWM Speed Control | |
} | |
else | |
{ | |
digitalWrite(M2, HIGH); | |
analogWrite(E2, rmotor); //PWM Speed Control | |
} | |
delay(30); // HIGH for digitalWrite means that it moves forward and LOW for digitalWrite means that it moves backwards. | |
// AnalogWrite controls speed and therefore direction of the two motors. | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(1000); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration/2) / 29.1; | |
if (distance >= 200 || distance <= 0){ | |
Serial.println("Hello"); // This tells me that that when the distance detected is over 200 cm away or is 0cm away, the monitor will say " | |
} | |
else { | |
Serial.print(distance); | |
Serial.println(" cm"); | |
} | |
if (distance<=10){ | |
digitalWrite(M2, HIGH); // This tells the Arduino that when the ultrasonic sensor detects a distance of 10cm or less, then the tank moves to the left | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment