using ps4 controller to control arduino car

Thread Starter

Killerbee65

Joined May 15, 2017
214
Hello all!

I am trying to replicate this project using a arduino mega: https://maker.pro/arduino/projects/how-to-control-an-arduino-robot-with-a-ps4-bluetooth-controller

So far that I can tell, on start up, the arduino won't open the serial monitor automatically to initiate Bluetooth connectivity. I would have to connect the arduino to a computer and manually open the serial monitor in the Arduino IDE.

When paired the serial monitor tells me my inputs are being received but the motors I have connected won't turn with the inputs.

How can I code the arduino to automatically open the serial monitor, if possible do it without having to be connected to a PC.

Why when connected and running perfectly do the motors not run with the inputs of the controller?

Thank you for any and all advice!

if anymore information is needed please don't hesitate to mention it.
 

MrSoftware

Joined Oct 29, 2013
1,783
I didn't dig into the project, but do you mean that you're having trouble communicating with your Bluetooth device unless you open a serial console to the arduino and initialize the Bluetooth device yourself? If the interface to your Bluetooth device is serial, then what I would personally do is use a second instance of the serial class, and a different set of pins, to open a serial connection dedicated to the Bluetooth device. Use the first serial for your console, and the second serial to interact with the Bluetooth device. Just read and write to the Bluetooth serial connection to control the Bluetooth device. If I'm understanding your question correctly..
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
I didn't dig into the project, but do you mean that you're having trouble communicating with your Bluetooth device unless you open a serial console to the arduino and initialize the Bluetooth device yourself? If the interface to your Bluetooth device is serial, then what I would personally do is use a second instance of the serial class, and a different set of pins, to open a serial connection dedicated to the Bluetooth device. Use the first serial for your console, and the second serial to interact with the Bluetooth device. Just read and write to the Bluetooth serial connection to control the Bluetooth device. If I'm understanding your question correctly..
Yes I think you got it, sorry for the late reply btw. Also I'm really new to this so I dont quite understand sadly. I'll try to do my own research but, for a lack of better words, can you dumb it down for me:)
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
Yes I think you got it, sorry for the late reply btw. Also I'm really new to this so I dont quite understand sadly. I'll try to do my own research but, for a lack of better words, can you dumb it down for me:)
For clarity I an using a usb host shield and a bluetooth usb dongle on an arduino mega
 

djsfantasi

Joined Apr 11, 2010
6,439
Do you initialize the serial connection before the Bluetooth connection in the setup() function.

Nothing will happen unless the required serial ports are initialized in setup(). Why don’t you post your code (with code tags)?
 

MrSoftware

Joined Oct 29, 2013
1,783
On the arduino you can have multiple serial connections. If your bluetooth is controlled by serial, then that's one connection. If you want a console where you can type back and forth to control the arduino, that's another serial connection. The two separate serial connections will use separate pins on the arduino. Read the arduino docs for your specific board to see which pins are setup for serial in the arduino serial library, that will make your life easier. Google around for an article about multiple serial connections with the mega, there are surely a few HowTo's around. You can also post your code here for help.
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
Code:
//Source Code
#include <PS4BT.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statement in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
int IN1 = 3;       //control pin for first motor
int IN2 = 4;       //control pin for first motor
int IN3 = 5;        //control pin for second motor
int IN4 = 6;        //control pin for second motor
USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so

/* You can create the instance of the PS4BT class in two ways */
// This will start an inquiry and then pair with the PS4 controller - you only have to do this once
// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in pairing mode
PS4BT PS4(&Btd, PAIR);

// After that you can simply create the instance like so and then press the PS button on the device
//PS4BT PS4(&Btd);

bool printAngle, printTouch;
uint8_t oldL2Value, oldR2Value;

void setup() {
   pinMode(IN1, OUTPUT);  
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);  
  pinMode(IN4, OUTPUT);
  Serial.begin(115200);
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); // Halt
  }
  Serial.print(F("\r\nPS4 Bluetooth Library Started"));

}
void loop() {
  Usb.Task();

  if (PS4.connected()) {
  //  if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) {
      if(PS4.getAnalogHat(LeftHatX)<10)
      {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
    Serial.print("\nHatX: 50 ");
        }
      else if(PS4.getAnalogHat(LeftHatX)>240)
      {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
    
    Serial.print("\nHatX: 200 ");
        }
        else if(PS4.getAnalogHat(LeftHatY) < 20)
        {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
    
    Serial.print("\nHatY: 50 ");
          }
          else if(PS4.getAnalogHat(LeftHatY) > 200)
        {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
        Serial.print("\nHatY: 200 ");
          }
        else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
        Serial.print("\nrotate the joystick ");
         

          }
      
}
}

here is my code, I know there is a different way of copy and pasting and I'm not sure if I did it right but I "copy for forum" on the ide.
 

djsfantasi

Joined Apr 11, 2010
6,439
I’m not familiar with the USB Shield.

I would EXPECT the .init() function may require the serial pins to use on the Arduino. I GUESS that not supplying the pins results in the default serial port being used. The same port as is assigned to the console.

The Mega has four hardware serial ports. Serial, Serial1, Serial2 and Serial3.

if you’re using the console, then the USB port needs to connect to a different serial port.
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
I’m not familiar with the USB Shield.

I would EXPECT the .init() function may require the serial pins to use on the Arduino. I GUESS that not supplying the pins results in the default serial port being used. The same port as is assigned to the console.

The Mega has four hardware serial ports. Serial, Serial1, Serial2 and Serial3.

if you’re using the console, then the USB port needs to connect to a different serial port.
LM YN USB Host Shield for Arduino UNO MEGA 2560 1280 Support Google Android ADK USB HUB https://www.amazon.com/dp/B0777DR3T6/ref=cm_sw_r_cp_apa_i_zXVUEbV63HZBC

This is the shield I am using
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
If I am thinking right, and please correct me here, I'll have to bend the rx and tx pins outward and connect them to serial2 pins? While in the code, initiate the serial monitor and calling the second serial for the actual code?
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
If you're using the same exact code as the demo, what is different between your hardware and the hardware used for the demo?
He is using
Arduino Pro Mini
USB Host Shield Mini
FT232RL
I am using
Arduino mega
Usb host shield above

These are the only known differences I noticed
 

MrSoftware

Joined Oct 29, 2013
1,783
I'm sorry for the lack of replies, work has been so crazy lately I haven't had time to really look at this. If his example is working then you need to go through at the detail level and figure out what's different between yours and his. Go through his example and identify every pin that is in use and what it is being used for (serial, etc..). Make a table on paper. Now make the same table for your hardware and write down how your pins are physically connected. Identify where there are differences, then go through your source code and check to see if (1) the arduino libraries already account for the difference, or (2) if you need to make changes to the source code due to the differences in pin assignments. I hope this helps!
 

Thread Starter

Killerbee65

Joined May 15, 2017
214
I'm sorry for the lack of replies, work has been so crazy lately I haven't had time to really look at this. If his example is working then you need to go through at the detail level and figure out what's different between yours and his. Go through his example and identify every pin that is in use and what it is being used for (serial, etc..). Make a table on paper. Now make the same table for your hardware and write down how your pins are physically connected. Identify where there are differences, then go through your source code and check to see if (1) the arduino libraries already account for the difference, or (2) if you need to make changes to the source code due to the differences in pin assignments. I hope this helps!
ok,It's ok, gotta do what you gotta do. The problem is in his project page, and his video, he doesn't really go into detail about what he did. The most I can possibly do is look up the parts he used and see where he out them which I have done luckily. The main thing I found was that the serial receiver is in between the arduino and the usb as opposed to mine where I have just using the host shield and the mega. I also read somewhere that either the host shield or the mega have built in serial receivers. Not 100% on that though.
 

djsfantasi

Joined Apr 11, 2010
6,439
What do you mean by “serial receiver”? If you mean a “serial port”, all Arduino’s have at least one serial port. It is used for the console by default. The Mega has four serial ports built-in. Any Arduino can have more serial ports by using the SoftwareSerial library. But there are some restrictions of software serial ports.
 
Top