Serial how to's

Ok first, what is this Protocol example called “0x0000”. Ive gooogled it many different ways and its not telling me.

Ok i im trying to get a pololu Maestro to take commands from an Arduino Nano via a PS2handle. I know i can do a serialread() to listen for the byte packets but what function call then activates the code when the serial reads my byte commands.

I don’t understand your questions. Links to the datasheets for the Maestro and the Protocol may help.

If you have a program post it here. Please use the code button </> so your code looks like this and is easy to copy to a text editor

You may find some useful stuff in Serial Input Basics

…R

Ok i want to use a wireless PS2 handle to send commands to a maestro via an Arduino nano.

Ive compiled the PS2 handle code and uploaded it to the nano.

Heres my PS2arduino 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);

}

Now there is an Arduino/pololu script example found here…

   /* 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);
}

This is the link to the PSX arduino tutorial…

http://playground.arduino.cc/Main/PSXLibrary

So when a button is pressed on the PS2 controller it sends a byte packet through the arduino like this 0x0000. I dont know what protocol that is but i really need to know.

Any way i need to add to the script a serial read to listen for the byte packet sent and to activate a subroutine in the pololu script. The pololu understands byte commands in hexidecimal.

Also im not understanding the comment above the button constants on the arduino playground. Will the bytes be printed in Hex or this protocol 0X0000.

Have you read this ? https://www.pololu.com/docs/0J40/5.c

For example, if we want to set the target of servo 0 to 1500 µs for a Maestro with device number 12, we could send the following byte sequence:

in hex: 0xAA, 0x0C, 0x04, 0x00, 0x70, 0x2E in decimal: 170, 12, 4, 0, 112, 46

Note that 0x04 is the command 0x84 with its most significant bit cleared.

Over and over but it gives no example on the code functions needed to activate them.

All i need to do im assuming is do a serial read at the top of the void loop of the scriptino example then do a function to activate each particular subroutine when it reads the byte packets.

So, say i have subroutine 1 as walking forward. When i press the up button on the controller i need it to activate subroutine 1.

And pleases do you know what this protocol is? "0x0000" Its driving me nuts!!!

Also how do get the Nano to send the 0xAA before sending the other command byte?

0x0000 is not a protocol. It's a common way to write the number 0 in hexadecimal.

Ok please dont make fun if me this is my first try at writing code on my own. I gleened all this off the Arduino site and used the Arduino/pololu script examples to put this together. Im sure its got more than a few mistakes.

My question is would sometjing like this work to activate subroutine 1 when the up button is pressed? 0x0008 would be the up button.

Also the maestro needs the byte commands in hexidecimal (example “0xAA”). Does the arduino put out hexidecimal format or the “0x0008” format?

#include <PololuMaestro.h>

#ifdef SERIAL_PORT_HARDWARE_OPEN
#define maestroSerial SERIAL_PORT_HARDWARE_OPEN
#else
#include <SoftwareSerial.h>
SoftwareSerial maestroSerial(10, 11);
#endif


void setup() {
maestroSerial.begin(9600);

}

void loop() {

Serial.read(); //open serial port

if (serial.input = 0x0008); // up button Ps2 handle

maestro.restartScript(1); // starts subroutine 1
delay(4000);

else if (serial.input = 0x0000); // when buuton is released stop script

maestro.stopScript(1 maestro.restartScriptWithParameter(1, 2000);
delay(4000););



}
Serial.read(); //open serial port

Nonsense.

if (serial.input = 0x0008); // up button Ps2 handle

You have not declared an object named serial. So, more nonsense.

Also the maestro needs the byte commands in hexidecimal (example "0xAA"). Does the arduino put out hexidecimal format or the "0x0008" format?

Is there some difference in your mind between the "0xAA" format and the "0x08" format? Not value, clearly those are different. Format.

I thought there was a difference between the "0x0008" and "0xAA" format yes. Ive just been converting 0x0008 to decimal then to hexidecimal. I dont know about these formats.

Ok so what is the proper way to write a serial read function?

Type "serial input" into the search box at the upper right. Read the first hit.

At the top of this page it says serial.read() opens the serial port.

https://www.arduino.cc/en/Serial/Read

Duhjoker: At the top of this page it says serial.read() opens the serial port.

https://www.arduino.cc/en/Serial/Read

No, it does NOT!

https://www.arduino.cc/en/Serial/Read

read() Description

Reads incoming serial data. read() inherits from the Stream utility class.

At the top of this page it says serial.read() opens the serial port.

I don’t know what you did, but I used the search box, “Search the Arduino forum”. I got this:

Im reading the link now.

Ok reads incoming serial data. Corrected! Below that gives the syntax serial.read.

Ok so im seeing that i can use serial.printIn On the setup?

Duhjoker: Ok reads incoming serial data. Corrected!

Ok so im seeing that i can use serial.printIn On the setup?

No. But you can use Serial.println(). The "I" and "l" look similar in this font.

Ahhhhh keep forgetting the () and its Ln not in

Thank you for helping me.

Duhjoker: Ahhhhh keep forgetting the () and its Ln not in

Thank you for helping me.

Don't mention it. Serial functions can be used in any regular function, including setup() and loop(). But they can not be used in an ISR (interrupt service routine).