Hello Arduino's experts,
I am writing my R2 codes for controlling my droid over a PS3 controller.
My setup contains an Arduino Mega 2560, USB Shield, USB Bluetooth dongle, 16 x 2 LCD display, PS3 Controller
Now I want to add a LCD display to my project that would show up what keys/functions is pressed on my PS3 controller.
So far everything works great, but I would like my display to show up that my PS3 have been connected successfully just after the board have boot up, but cannot find how to do it.
I can call a function ( if (PS3.PS3NavigationConnected) ) that would trigger the lcd.print("PS3 Connected"), but I don't know how to set it up so it would only display it one time only , once the controller is connected, and would go away as soon as any other key on my remote is press.
if i put it in the void setup, it doesn't work, since it took about 5 to 6 seconds for the library to initiate the Bluetooth dongle, so it have to be put in the void loop in order to work, but if I put it on a simple "if" command, it just stay on the display since it run over and over and doesn't show up my other messages from other keys being pressed...
Thanks in advance
Sergio
/*
Arduino DC Motor Control - PWM | H-Bridge | controlled by PS3 Navigation Move Controller
Modified version in progress, working with PS3 Navigation Controller and DROKK PWM, Emergency Stop action added when pressing PS button
Adding multipe button actions from the PS3 remote to turn on different output on the MEGA
Adding HEX serial write commands for MP3 player control
*/
#define up 22 // misc output pins for various droid action to be programmed later on
#define down 24 // misc output pins for various droid action to be programmed later on
#define left 26 // misc output pins for various droid action to be programmed later on
#define right 28 // misc output pins for various droid action to be programmed later on
#define cross 30 // output pin to send the dome to the center position
#define circle 32 // output pin to control the direction of the dome motor
#define l1 34 // misc output pins for various droid action to be programmed later on
#define l3 36 // misc output pins for various droid action to be programmed later on
#define enA 44 // left motor PWM control and direction
#define in1 40
#define in2 42
#define enB 46 // right motor PWM control and direction
#define in3 48
#define in4 50
#define dome 45 // dome rotation PWM speed control
int motorSpeedA = 0;
int motorSpeedB = 0;
int domeSpeed = 0;
int LEDState=0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <PS3BT.h>
#include <SoftwareSerial.h>
SoftwareSerial pwm (19, 18); // Rx, Tx to send serial command to MP3 Player
byte File1[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x01,0xEF};
byte File2[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x02,0xEF};
byte File3[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x03,0xEF};
byte File4[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x04,0xEF};
byte File5[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x05,0xEF};
byte File6[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x06,0xEF};
byte File7[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x07,0xEF};
byte File8[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x08,0xEF};
byte File9[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x09,0xEF};
byte File10[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0a,0xEF};
byte File11[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0b,0xEF};
byte File12[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0c,0xEF};
byte File13[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0d,0xEF};
byte File14[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0e,0xEF};
byte File15[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x0f,0xEF};
byte File16[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x10,0xEF};
byte File17[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x11,0xEF};
byte File18[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x12,0xEF};
byte File19[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x13,0xEF};
byte File20[] = {0x7E,0xFF,0x06,0x0F,0x00,0x01,0x14,0xEF};
byte Vol20[] = {0x7E,0xFF,0x06,0x06,0x00,0x00,0x20,0xEF};
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
void(* resetFunc) (void) = 0;
void setup()
{
pinMode(enA, OUTPUT); //PWM output motor 1
pinMode(in1, OUTPUT); //direction of motor 1
pinMode(in2, OUTPUT); //direction of motor 1
pinMode(enB, OUTPUT); //PWM output motor 2
pinMode(in3, OUTPUT); //direction of motor 2
pinMode(in4, OUTPUT); //direction of motor 2
pinMode(dome, OUTPUT); //PWM output dome motor
pinMode(up, OUTPUT); // set of pins output for various droid actions to be programmed later on
pinMode(down, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
pinMode(cross, OUTPUT); // output pin to send the dome to the center position
pinMode(circle, OUTPUT); // output pin to control the direction of the dome motor
pinMode(l1, OUTPUT); // pin output for various droid action to be programmed later on
pinMode(l3, OUTPUT); // pin output for various droid action to be programmed later on
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
// Serial.begin(57600); // works better along with the pwm.begin set at (9600)
pwm.begin(9600); // set at this rate to communicate with the MP3 default baud
if (Usb.Init() == -1)
{
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
delay(3000);
pwm.write(File6,sizeof(File6));
lcd.clear();
lcd.print("R2 BOOTING UP");
}
void loop()
{
Usb.Task();
if (PS3.PS3NavigationConnected) // the loop codes between those brakets will only run when the PS3 Navigation is connected
{
int xAxis = PS3.getAnalogHat(LeftHatX); // Read PS3 Joysticks X-axis
int yAxis = PS3.getAnalogHat(LeftHatY); // Read PS3 Joysticks Y-axis
int dAxis = (PS3.getAnalogButton(L2)); // Read the Joysticks value of L2 for dome rotation speed
if (yAxis < 90)
{
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 128 to 0 into 0 to 255 value for the motor PWM signal
motorSpeedA = map(yAxis, 128, 0, 0, 255); // CHANGED TO MATCH PS3 VALUE
motorSpeedB = map(yAxis, 128, 0, 0, 255); // CHANGED TO MATCH PS3 VALUE
}
else if (yAxis > 150)
{
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 150 to 255 into 0 to 255 value for the motor PWM signal
motorSpeedA = map(yAxis, 150, 255, 0, 255); // TO BE CHANGE TO MATCH PS3 VALUE
motorSpeedB = map(yAxis, 150, 255, 0, 255); // TO BE CHANGE TO MATCH PS3 VALUE
}
// If joystick stays in middle the motors are not moving
else
{
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 100)
{
// Convert the declining X-axis readings from 100 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 100, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0)
{
motorSpeedA = 0;
}
if (motorSpeedB > 255)
{
motorSpeedB = 255;
}
}
if (xAxis > 160)
{
// Convert the increasing X-axis readings from 160 to 255 into 0 to 255 value
int xMapped = map(xAxis, 160, 255, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255)
{
motorSpeedA = 255;
}
if (motorSpeedB < 0)
{
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. Motors couldn't start moving if PWM value was below value of 70)
// if (motorSpeedA < 70)
// {
// motorSpeedA = 0;
// }
// if (motorSpeedB < 70)
// {
// motorSpeedB = 0;
// }
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
analogWrite(dome, domeSpeed); // Send PWM signal to dome motor
if (PS3.getButtonPress(PS)) // Emergency stop option, reset the Arduino Board and disconnect the PS3 Controller
{
Serial.println("EMERGENCY STOP ACTIVATED, RESETTING MEGA BOARD");
lcd.clear();
lcd.print("EMERGENCY STOP");
delay(500);
resetFunc();
}
if (dAxis > 10)
{
domeSpeed = map(dAxis, 0, 255, 0, 255);
}
else
{
domeSpeed = 0;
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(UP)) // Play file 1 of R2 STAR WAR OPENING MUSIC
{
pwm.write(File1,sizeof(File1));
delay(50);
Serial.println("L1+UP = FILE 1");
lcd.clear();
lcd.print("FILE 1");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(DOWN)) // Play file 2 of R2 THE SITH SOUNDTRACK
{
pwm.write(File2,sizeof(File2));
delay(50);
Serial.println("L1+DOWN = FILE 2");
lcd.clear();
lcd.print("FILE 2");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(LEFT)) // Play file 3 of R2 LEA MESSAGE
{
pwm.write(File3,sizeof(File3));
delay(50);
Serial.println("L1 & LEFT = FILE 3");
lcd.clear();
lcd.print("FILE 3");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(RIGHT)) // Play file 4 of R2 THE EMPIRE MARCH
{
pwm.write(File4,sizeof(File4));
delay(50);
Serial.println("L1 & RIGHT = FILE 4");
lcd.clear();
lcd.print("FILE 4");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(CROSS)) // Play file 7 of R2 THE FORCE THEME
{
pwm.write(File7,sizeof(File7));
delay(50);
Serial.println("L1 & CROSS = FILE 7");
lcd.clear();
lcd.print("FILE 7");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(CIRCLE)) // Play file 8 of R2 BAR MUSIC XAXOPHONE EPISODE 1
{
pwm.write(File8,sizeof(File8));
delay(50);
Serial.println("L1 & CIRCLE = FILE 8");
lcd.clear();
lcd.print("FILE 8");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(UP)) // Play file 10 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File10,sizeof(File10));
delay(50);
Serial.println("L2 & UP = FILE 10");
lcd.clear();
lcd.print("FILE 10");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(DOWN)) // Play file 11 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File11,sizeof(File11));
delay(50);
Serial.println("L2 & DOWN = FILE 11");
lcd.clear();
lcd.print("FILE 11");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(LEFT)) // Play file 12 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File12,sizeof(File12));
delay(50);
Serial.println("L2 & LEFT = 12");
lcd.clear();
lcd.print("FILE 12");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(RIGHT)) // Play file 13 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File13,sizeof(File13));
delay(50);
Serial.println("L2 & RIGHT = FILE 13");
lcd.clear();
lcd.print("FILE 13");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(CROSS)) // Play file 14 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File14,sizeof(File14));
delay(50);
Serial.println("L2 & CROSS = FILE 14");
lcd.clear();
lcd.print("FILE 14");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(CIRCLE)) // Play file 15 of R2 //testing the L2 and other keys for 2nd set of audio files
{
pwm.write(File15,sizeof(File15));
delay(50);
Serial.println("L2 & CIRCLE = FILE 15");
lcd.clear();
lcd.print("FILE 15");
}
if (PS3.getButtonPress(L1) && PS3.getButtonClick(L3)) // Play file 9 of R2 pouatttttt fart
{
pwm.write(File9,sizeof(File9));
delay(50);
Serial.println("L1 & L3 = FILE 9");
lcd.clear();
lcd.print("FILE 9");
}
if (PS3.getButtonPress(L2) && PS3.getButtonClick(L3)) // Play file 20 of R2 pouatttttt fart
{
pwm.write(File20,sizeof(File20));
delay(50);
Serial.println("L2 & L3 = FILE 20");
lcd.clear();
lcd.print("FILE 20");
}
if (PS3.getButtonClick(L3)) // Push L3 to play file 5 of R2 screaming while running wild
{
pwm.write(File5,sizeof(File5));
delay(50);
Serial.println("L3 = FILE 5");
lcd.clear();
lcd.print("FILE 5");
}
if (PS3.getButtonClick(UP)) // Control function to be added later for any servo action
{
Serial.println("UP");
lcd.clear();
lcd.print("UP");
}
if (PS3.getButtonClick(DOWN)) // Control function to be added later for any servo action
{
Serial.println("DOWN");
lcd.clear();
lcd.print("DOWN");
}
if (PS3.getButtonClick(LEFT)) // Control function to be added later for any servo action
{
Serial.println("LEFT");
lcd.clear();
lcd.print("LEFT");
}
if (PS3.getButtonClick(RIGHT)) // Control function to be added later for any servo action
{
Serial.println("RIGHT");
lcd.clear();
lcd.print("RIGHT");
}
if (PS3.getButtonClick(CROSS)) // this will send command to the dome motor to turn back to the default position
{
Serial.println("CROSS = DEFAULT DOME POSITION");
lcd.clear();
lcd.print("DEF DOME POSITION");
digitalWrite(cross, HIGH);
delay(100);
digitalWrite(cross, LOW);
}
if (PS3.getButtonClick(CIRCLE)) // that will set the direction of the dome motor controller (clockwise or counterclockwise)
{
Serial.println("CIRCLE = DOME DIR CHANGE");
lcd.clear();
lcd.print("DOME DIR CHANGE");
if (LEDState==0)
{
digitalWrite(circle,HIGH);
LEDState=1;
}
else
{
digitalWrite(circle, LOW);
LEDState=0;
}
}
}
}