BB8 Droid Coding - trying to trigger sounds via serial

Hi all,

I'm most of the way through building a working replica of a BB8 Droid. The electronics and mechanics are all complete, and I'm into the coding. Unfortunately for me this is where I rapidly ran out of talent and experience.

My droid is running two Arduino's, a Mega with a USB host shield and Bluetooth Dongle (to connect to a PS3 controller), and an Uno with a geeetech MP3 shield for the sounds.

Using a sketch written for an RC car I have so far successfully paired the controller and Mega/USB shield combo, and have edited the sketch to control three servos which my droid uses to control the head. That all works perfectly.

I also (up until tonight) managed to edit in some code which turned button pushes from the controller, into serial printed numbers, which (because the TX/RX pins of my two arduinos are connected, was triggering sounds from the MP3 shield, which has a simple MP3 player sketch running on it which plays tracks 1-9 from an SD card when it receives the numbers 1-9 via serial data.

I actually had this all working with about 4 different buttons on the remote triggering different sounds, and was trying to expand my button pushes by adding more, but for whatever reason, it stopped working. By stopped working, I mean the remote will still turn the head as before, but the button pushes no longer trigger any sounds.

I know the MP3 shield is still working, because when the controller pairs with the USB dongle, there is a serial command which I added which switches the shield to Mono output ('M') increases the volume to maximum (19 consecutive '+' symbols), and then plays track 3 on the SD card. This all still works when the controller is paired.

Note that the serial data for the 'circle' button push also raises the volume, as I found that the shield appeared to 'reset' itself after being initialized. I wanted one (lesser used) sound to also raise the volume, and when I use the droid, I'll just play that one first to set the volume at max.

I suspect the reason is down to my syntax and my simplistic addition of lines without really knowing what I'm doing.

Can anyone please possibly help get my sounds working again?

/*
 Barrett Anderies
 March 11, 2013
 This is a modified version of the PS3BT.ino example sketch by Kristian Lauszus
 For more information visit his blog: http://blog.tkjelectronics.dk/ or 
 send him an e-mail:  kristianl@tkjelectronics.com
 */

#include <PS3BT.h>                                                    //Include the necessary libraries.
#include <Servo.h>
#include <SPI.h>

USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd); 

Servo servo1;                                                         //Create instances of type Servo. servo1 is the dome rotate servo or steer and servo2 and servo3 is the head tilt.
Servo servo2;
Servo servo3;

int offset = 0; // put this with the other global variables (above the void setup() function).
int steer = 127; // put this with the other global variables (above the void setup() function).

void setup() {
  Serial.begin(115200);                                              
  if (Usb.Init() == -1) {                                            
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nM++++++++++++++++++3"));              
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  servo1.attach(5);                                                  //Dome rotate servo on digital pin 5
  servo2.attach(3);                                                  //head tilt right side on digital pin 3
  servo3.attach(4);                                                   //head tilt left side on digital pin 4
}
void loop() 
{
  Usb.Task();

  if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
    
    steer = PS3.getAnalogHat(RightHatX); // read right joystick
    
    if(PS3.getButtonClick(RIGHT)) { // trim right
      offset += 1;
    }
    if(PS3.getButtonClick(LEFT)) { // trim left
      offset -= 1;
    }
    
    steer = steer + offset; // incorporate trim into steer command
    if(steer > 255) steer = 255; // enforce upper limit
    if(steer < 0) steer = 0; // enforce lower limit
    
    servo1.write(map(steer, 0, 255, 180, 0)); // write steer command
    servo2.write(map(PS3.getAnalogHat(RightHatY), 0, 255, 0, 135)); // write throttle command
    servo3.write(map(PS3.getAnalogHat(RightHatY), 0, 255, 180, 45));
   
  }
  else 
   {
    servo1.write(90);
    servo2.write(90);
    servo3.write(90);
   }
    if(PS3.getButtonClick(PS)) {
      PS3.disconnect();
    }
      else
       if(PS3.getButtonClick(CIRCLE)) {
      Serial.print(F("\r\nM+++++++++++++++++++8"));
      if(PS3.getButtonClick(R1))
      Serial.print(F("\r\n1"));
      if(PS3.getButtonClick(R3))
      Serial.print(F("\r\n2"));
      if(PS3.getButtonClick(L1))
      Serial.print(F("\r\n3"));
      if(PS3.getButtonClick(UP))
      Serial.print(F("\r\n4"));
      if(PS3.getButtonClick(DOWN))
      Serial.print(F("\r\n5"));
    }
}

Maybe something like this:

void loop()
{
  Usb.Task();

  if (PS3.PS3Connected || PS3.PS3NavigationConnected) {

    steer = PS3.getAnalogHat(RightHatX); // read right joystick

    if (PS3.getButtonClick(RIGHT)) { // trim right
      offset += 1;
    }
    if (PS3.getButtonClick(LEFT)) { // trim left
      offset -= 1;
    }

    steer = steer + offset; // incorporate trim into steer command
    if (steer > 255) steer = 255; // enforce upper limit
    if (steer < 0) steer = 0; // enforce lower limit

    servo1.write(map(steer, 0, 255, 180, 0)); // write steer command
    servo2.write(map(PS3.getAnalogHat(RightHatY), 0, 255, 0, 135)); // write throttle command
    servo3.write(map(PS3.getAnalogHat(RightHatY), 0, 255, 180, 45));

    if (PS3.getButtonClick(CIRCLE))
      Serial.print(F("\r\nM+++++++++++++++++++8"));
    if (PS3.getButtonClick(R1))
      Serial.print(F("\r\n1"));
    if (PS3.getButtonClick(R3))
      Serial.print(F("\r\n2"));
    if (PS3.getButtonClick(L1))
      Serial.print(F("\r\n3"));
    if (PS3.getButtonClick(UP))
      Serial.print(F("\r\n4"));
    if (PS3.getButtonClick(DOWN))
      Serial.print(F("\r\n5"));

    if (PS3.getButtonClick(PS))
      PS3.disconnect();

  }
  else
  {
    servo1.write(90);
    servo2.write(90);
    servo3.write(90);
  }
}

(Neither test compiled nor tested.)

Thank you for replying :slight_smile:

Your code change seems to have done the trick! Thank you :slight_smile:

For the benefit of anyone else reading the thread. Initially it still behaved the same, so I checked the serial monitor on my Uno (with the MP3 shield) and it was being flooded with erroneous reset commands from goodness knows where (it wasn't connected to anything else other than my PC at the time). I ended up simply taking out the SD Card and re-inserting it which seemed to fix that problem, then the modified code worked :slight_smile: