Kayos
Gone......
- Messages
- 9,422
- Location
- Yorkshire
I know a few on here use them so thought would be a good place to start
I bought my boy an Arduino powered robot for Christmas, all good fun except neither of us know the first thing about coding.....
The code below will set the robot going forward and (try) avoiding obstacles using the ultrasonic sensors, this can be over ridden with the PS2 style controller, as a starting point I'd like to make it just respond to the PS2 controller.
I have unplugged the sensor but it still goes forward with no input, I deleted the sensor section of code but it then did nothing
Is anyone able to show me how to modify the code to make it just respond to the controller inputs?
Thanks
I bought my boy an Arduino powered robot for Christmas, all good fun except neither of us know the first thing about coding.....
The code below will set the robot going forward and (try) avoiding obstacles using the ultrasonic sensors, this can be over ridden with the PS2 style controller, as a starting point I'd like to make it just respond to the PS2 controller.
I have unplugged the sensor but it still goes forward with no input, I deleted the sensor section of code but it then did nothing
Is anyone able to show me how to modify the code to make it just respond to the controller inputs?
Thanks
#include <PS2X_lib.h> //for v1.6
#include <oseppRobot.h>
#include <avr/wdt.h>
/******************************************************************
set pins connected to PS2 controller:
- 1e column: original
- 2e colmun: Stef?
replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT A3 //14
#define PS2_CMD A2 //15
#define PS2_SEL A1 //16
#define PS2_CLK A0 //17
/******************************************************************
select modes of PS2 controller:
- pressures = analog reading of push-butttons
- rumble = motor rumbling
uncomment 1 of the lines for each mode selection
******************************************************************/
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
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 connect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup()
{
wdt_disable();
Serial.begin(115200);
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if (error == 0)
{
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
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("Note: 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. ");
// Serial.print(ps2x.Analog(1), HEX);
type = ps2x.readType();
switch (type)
{
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
}
OseppTBMotor L(12, 11);
OseppTBMotor R(8, 3);
OseppRangeFinder U(2);
unsigned long begin=0;
void loop()
{
if(analogRead(A4)>300){
digitalWrite(13,HIGH);
} else {
digitalWrite(13,LOW);
}
if (error == 1 || type != 1)
{
L.forward(0);
R.forward(0);
Serial.println("丢失遥控器信号");
delay(500);
wdt_enable(WDTO_15MS);
while (1)
{
}
}
// 复位变量,依次为:前进,平移,转向
int my = 0;
int mx = 0;
// 按钮功能处理
ps2x.read_gamepad(false, vibrate);
if (ps2x.Button(PSB_PAD_DOWN))
{
my = -150;
}
else if (ps2x.Button(PSB_PAD_UP))
{
my = 150;
}
else
{
my = (127 - ps2x.Analog(PSS_LY)) * 2;
if (abs(my) < 20)
{
my = (127 - ps2x.Analog(PSS_RY)) * 2;
}
}
if (ps2x.Button(PSB_PAD_LEFT))
{
mx = -150;
}
else if (ps2x.Button(PSB_PAD_RIGHT))
{
mx = 150;
}
else
{
mx = (ps2x.Analog(PSS_LX) - 127) * 2;
}
int sl = my + mx;
int sr = my - mx;
if (abs(sl) > 10 || abs(sr) > 10) {
L.forward(sl);
R.forward(sr);
} else {
if (U.ping() < 350) {
begin = millis();
while (U.ping() < 100) {
L.backward(100);
R.backward(100);
}
if (random(0, 2)) {
L.forward(200);
R.backward(200);
} else {
L.backward(200);
R.forward(200);
}
while (U.ping() < 400) {
if (millis() - begin > 3000) {
break;
}
}
} else if (U.ping() < 700) {
L.forward(map(U.ping(), 350, 700, 80, 200));
R.forward(map(U.ping(), 350, 700, 80, 200));
} else {
L.forward(200);
R.forward(200);
}
}
delay(20);
}