I’m trying to make a wireless hexapod powered by a maestro24 as the servo controller and an Arduino nano and wireless PS2 handle to send commands to activate the scripts. I’ve managed to program my arduino nano to accept the commands from my PS2. At least I think it works it did compile and load after fixing a few syntax errors.
Any way I’m fairly new to coding and only understand a lil of what I’m doing. I’m used to compiling pre-coded .ino files. So I’m still trying to learn. The code for the maestro and arduino to communicate back and fourth has few commands but I don’t really understand how to add it to my PS2 code and script to get it to activate my scripts.
For reference here is my ps2 arduino code
[code] #include <PS2X_lib.h>
PS2X ps2x; //create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup() {
Serial.begin(57600);
error = ps2x.config_gamepad(13,11,10,12, true, true); // setup pins and settings
if(error == 0){
Serial.println(“Found Controller, configured successful”);
Serial.println(“Try out all the buttons, X will vibratethe 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.”);
else if(error == 2)
Serial.println(“Controller found but not accepting commands. see raedme.txt to enable debug.”);
else if(error == 3)
Serial.println(“Controller refusing to enter pressure mode, may not support it”);
// Serial.print(ps2x.Analog(1),Hex);
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;
}
}
void loop() {
if(error == 1) //skip loop if no controller found
return;
if(type == 2){ //Guitar Hero Controller
ps2x.read_gamepad(); //read controller
if(ps2x.ButtonPressed(GREEN_FRET))
Serial.println(“Green Fret Pressed”);
if(ps2x.ButtonPressed(RED_FRET))
Serial.println(“Red Fret Pressed”);
if(ps2x.ButtonPressed(YELLOW_FRET))
Serial.println(“Yellow Fret Pressed”);
if(ps2x.ButtonPressed(BLUE_FRET))
Serial.println(“Blue Fret Pressed”);
if(ps2x.ButtonPressed(ORANGE_FRET))
Serial.println(“Orange Fret Pressed”);
if(ps2x.ButtonPressed(STAR_POWER))
Serial.println(“Star Power Command”);
if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
Serial.println(“Up Strum”);
if(ps2x.Button(DOWN_STRUM))
Serial.println(“DOWN Strum”);
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println(“Start is being held”);
if(ps2x.Button(PSB_SELECT))
Serial.println(“Select is being held”);
if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
{
Serial.print(“Wammy Bar Position:”);
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
}
}
else { //DualShock Controller
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at ‘vibrate’ speed
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println(“Start is being held”);
if(ps2x.Button(PSB_SELECT))
Serial.println(“Select is being held”);
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on
//how hard you press the blue (X) button
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
{
if(ps2x.Button(PSB_L3))
Serial.println(“L3 pressed”);
if(ps2x.Button(PSB_R3))
Serial.println(“R3 pressed”);
if(ps2x.Button(PSB_L2))
Serial.println(“L2 pressed”);
if(ps2x.Button(PSB_R2))
Serial.println(“R2 pressed”);
if(ps2x.Button(PSB_GREEN))
Serial.println(“Triangle pressed”);
}
if(ps2x.ButtonPressed(PSB_RED)) //will be TRUE if button was JUST pressed
Serial.println(“Circle just pressed”);
if(ps2x.ButtonReleased(PSB_PINK)) //will be TRUE if button was JUST released
Serial.println(“Square just released”);
if(ps2x.NewButtonState(PSB_BLUE)) //will be TRUE if button was JUST pressed OR released
Serial.println(“X just changed”);
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
{
Serial.print(“Stick Values:”);
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
}
}
delay(50);
}
[/code]
And here is the code I am supposed to add to the script and my arduino nano.
[code] /* This example shows how to control Maestro scripts with an
Arduino. It assumes you have an RC hobby servo connected on
channel 0 of your Maestro, and that you have already used the
Maestro Control Center software to verify that the servo is
powered correctly and moves when you command it to from the
Maestro Control Center software.
Before using this example, you should go to the Serial Settings
tab in the Maestro Control Center and apply these settings:
- Serial mode: UART, fixed baud rate
- Baud rate: 9600
- CRC disabled
Be sure to click “Apply Settings” after making any changes.
It also assumes you have connected your Arduino to your Maestro
appropriately. If you have not done so, please see
https://github.com/pololu/maestro-arduino for more details on how
to make the connection between your Arduino and your Maestro.
Also, the Maestro needs to have the script in the comment
below installed on it. */
/*
Maestro script for Script.ino Arduino library example.
Copy into the Script tab of the Maestro Control Center and
click Apply Settings.
Don’t do anything by default.
begin
repeat
Subroutine 0
sub alternate_mid_to_high
begin
6000 0 servo
1000 delay
7000 0 servo
1000 delay
repeat
Subroutine 1
Expects the delay time to be on the top of the stack
sub alternate_mid_to_low
begin
6000 0 servo
dup delay
5000 0 servo
dup delay
repeat
*/
#include <PololuMaestro.h>
/* On boards with a hardware serial port available for use, use
that port to communicate with the Maestro. For other boards,
create a SoftwareSerial object using pin 10 to receive (RX) and
pin 11 to transmit (TX). */
#ifdef SERIAL_PORT_HARDWARE_OPEN
#define maestroSerial SERIAL_PORT_HARDWARE_OPEN
#else
#include <SoftwareSerial.h>
SoftwareSerial maestroSerial(10, 11);
#endif
/* Next, create a Maestro object using the serial port.
Uncomment one of MicroMaestro or MiniMaestro below depending
on which one you have. */
MicroMaestro maestro(maestroSerial);
//MiniMaestro maestro(maestroSerial);
void setup()
{
maestroSerial.begin(9600);
}
void loop()
{
/* Subroutine numbers can be found by clicking “View Compiled
Code…” on the Maestro Control Center script tab.
Restart script at subroutine 0. Subroutine 0 constantly
alternates the servo position from the neutral position to the
high end of the range. */
maestro.restartScript(0);
delay(4000);
// Stop the running script.
maestro.stopScript();
/* Restart subroutine 1 with the value 2000 on the stack.
Subroutine 1 constantly alternates the servo position from the
neutral position to the low end of the range. In this case, The
parameter put on to the stack represents how long the delay is
between servo movements.*/
maestro.restartScriptWithParameter(1, 2000);
delay(4000);
}[/code]
All right so I want to control multiple scripts using multiple buttons. How would I re-write it and add it to the arduino code in the first code block to activate a certain script when a certain button is pressed?