if(type==2)
return;
else {
ps2x.read_gamepad(false, vibrate);
PS_LX=ps2x.Analog(PSS_LX);
PS_RX=ps2x.Analog(PSS_RX);
PS_LY=ps2x.Analog(PSS_LY);
PS_RY=ps2x.Analog(PSS_RY);
vibrate = ps2x.Analog(PSAB_BLUE);
if (ps2x.Button(PSB_START)) {ch='w';Serial.println("Start is being held");myse.runActionGroup(13,1);test=1;}
if (ps2x.Button(PSB_SELECT)) {ch='x';Serial.println("Select is being held");myse.runActionGroup(0,1);test=2; }
if (ps2x.Button(PSB_PAD_UP)) {ch='a';myse.runActionGroup(9,1);}
if (ps2x.Button(PSB_PAD_DOWN) ) {ch='b';myse.runActionGroup(10,1);}
if (ps2x.Button(PSB_PAD_LEFT) ) {ch='c';myse.runActionGroup(11,1);}
if (ps2x.Button(PSB_PAD_RIGHT)) {ch='d';myse.runActionGroup(12,1);}
if (ps2x.Button(PSB_GREEN)) {ch='e';}
if (ps2x.NewButtonState(PSB_BLUE)){ch='f';}
if (ps2x.Button(PSB_PINK)) {ch='g';}
if (ps2x.Button(PSB_RED)) {ch='h';}
if (ps2x.Button(PSB_R2) ) {ch='i';}
if (ps2x.Button(PSB_L2)) {ch='j';test=3;}
if (ps2x.Button(PSB_R1) ) {ch='k';}
if (ps2x.Button(PSB_L1)) {ch='l';myse.runActionGroup(14,1);test=0;}
if (ps2x.Button(PSB_R3) ) {ch='m';}
if (ps2x.Button(PSB_L3)) {ch='n';}
if(PS_RX<5) {ch='o';}
if(PS_RX>250) {ch='p';}
if(PS_RY<5) {ch='q';}
if(PS_RY>250) {ch='r';}
if(PS_LX<5) {ch='s';}
if(PS_LX>250) {ch='t';}
if(PS_LY<5) {ch='u';}
if(PS_LY>250) {ch='v';}
if (ch=='z') stop();
Serial.print(" ");
Serial.println(ch);
delay(50);
}
}
void button_set3() ///中姿態
{
ch='z';
if(type==2)
return;
else {
ps2x.read_gamepad(false, vibrate);
PS_LX=ps2x.Analog(PSS_LX);
PS_RX=ps2x.Analog(PSS_RX);
PS_LY=ps2x.Analog(PSS_LY);
PS_RY=ps2x.Analog(PSS_RY);
vibrate = ps2x.Analog(PSAB_BLUE);
if (ps2x.Button(PSB_START)) {ch='w';Serial.println("Start is being held");myse.runActionGroup(13,1);test=1;}
if (ps2x.Button(PSB_SELECT)) {ch='x';Serial.println("Select is being held");myse.runActionGroup(0,1);test=2; }
if (ps2x.Button(PSB_PAD_UP)) {ch='a';myse.runActionGroup(5,1);}
if (ps2x.Button(PSB_PAD_DOWN) ) {ch='b';myse.runActionGroup(6,1);}
if (ps2x.Button(PSB_PAD_LEFT) ) {ch='c';myse.runActionGroup(7,1);}
if (ps2x.Button(PSB_PAD_RIGHT)) {ch='d';myse.runActionGroup(8,1);}
if (ps2x.Button(PSB_GREEN)) {ch='e';myse.runActionGroup(16,1);}
if (ps2x.NewButtonState(PSB_BLUE)){ch='f';}
if (ps2x.Button(PSB_PINK)) {ch='g';move1(); }
if (ps2x.Button(PSB_RED)) {ch='h';sonar();}
if (ps2x.Button(PSB_R2) ) {ch='i';}
if (ps2x.Button(PSB_L2)) {ch='j';test=3;}
if (ps2x.Button(PSB_R1) ) {ch='k';}
if (ps2x.Button(PSB_L1)) {ch='l';myse.runActionGroup(14,1);test=0;}
if (ps2x.Button(PSB_R3) ) {ch='m';}
if (ps2x.Button(PSB_L3)) {ch='n';}
if(PS_RX<5) {ch='o';for(int i=0;i<=7;i++) servopulse(servopin,160);}
else if(PS_RX>250) {ch='p';for(int i=0;i<=8;i++) servopulse(servopin,20);}
else for(int i=0;i<=5;i++) servopulse(servopin,90);
if(PS_RY<5) {ch='q';}
if(PS_RY>250) {ch='r';}
if(PS_LX<5) {ch='s';}
if(PS_LX>250) {ch='t';}
if(PS_LY<5) {ch='u';}
if(PS_LY>250) {ch='v';}
if (ch=='z') stop();
Serial.print(" ");
Serial.println(ch);
delay(50);
}
}
void button_set4() //////轉輪
{
if(type==2)
return;
else {
ch='z';
ps2x.read_gamepad(false, vibrate);
PS_LX=ps2x.Analog(PSS_LX);
PS_RX=ps2x.Analog(PSS_RX);
PS_LY=ps2x.Analog(PSS_LY);
PS_RY=ps2x.Analog(PSS_RY);
vibrate = ps2x.Analog(PSAB_BLUE);
if (ps2x.Button(PSB_START)) {ch='w';Serial.println("Start is being held");myse.runActionGroup(13,1);test=1;}
if (ps2x.Button(PSB_SELECT)) {ch='x';Serial.println("Select is being held");myse.runActionGroup(0,1);test=2; }
if (ps2x.Button(PSB_PAD_UP)) {ch='a';}
if (ps2x.Button(PSB_PAD_DOWN) ) {ch='b';}
if (ps2x.Button(PSB_PAD_LEFT) ) {ch='c';move(6, 255, 1);}
if (ps2x.Button(PSB_PAD_RIGHT)) {ch='d';move(6, 255, 0);}
if (ps2x.Button(PSB_GREEN)) {ch='e'; stop();}
if (ps2x.NewButtonState(PSB_BLUE)){ch='f';}
if (ps2x.Button(PSB_PINK)) {ch='g';}
if (ps2x.Button(PSB_RED)) {ch='h';}
if (ps2x.Button(PSB_R2) ) {ch='i';move(1, 255, 0);move(2, 255, 0);move(3, 255, 1);move(4, 255, 1);}
if (ps2x.Button(PSB_L2)) {ch='j';test=3;}
if (ps2x.Button(PSB_R1) ) {ch='k';move(1, 255, 1);move(2, 255, 1);move(3, 255, 0);move(4, 255, 0);}
if (ps2x.Button(PSB_L1)) {ch='l';myse.runActionGroup(14,1);test=0;}
if (ps2x.Button(PSB_R3) ) {ch='m';move(5, 255, 0);}
if (ps2x.Button(PSB_L3)) {ch='n';move(5, 255, 1);}
if(PS_RX<5) {ch='o';move(2, 255, 0);}
if(PS_RX>250) {ch='p';move(2, 255, 1);}
if(PS_RY<5) {ch='q';move(1, 255, 1);}
if(PS_RY>250) { ch='r';move(1, 255, 0);}
if(PS_LX<5) { ch='s';move(3, 255, 0);}
if(PS_LY<5) {ch='u';move(4,255,0);}
if(PS_LY>250) {ch='y';move(4,255,1);}
if (ch=='z') stop();
Serial.print(" ");
Serial.println(ch);
delay(50);
}
}
/**********主程序**************/
void setup()
{
Serial.begin(9600);
Serial2.begin(9600);
motor_start();
delay(300);
sensor_start();
delay(300);
pinMode(servopin,OUTPUT);//設定舵機接口為輸出接口
Wire.begin();
accelgyro.initialize();
delay(300);
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
delay(50);
}
void loop()
{
if (test==0) button_set(); // 低姿態
if (test==1) button_set2(); // 高姿態
if (test==2) button_set3(); // 中姿態 打架 搖頭 揮手轉向 搖頭避障
if (test==3) button_set4(); // 轉輪
}