PS4 controller battery level

Hey everyone,

Just wondering if anyone could share code that would allow me to read the battery level of my PS4 controller. I found this snippet of info by TKJ electronics, but being a newbie, I am not sure how to implement this in my code.

https://github.com/felis/USB_Host_Shield_2.0/blob/2c5f5e8c0964c1639265486b6fac17739a55f1d2/PS4Parser.h#L259-L265

/** * Return the battery level of the PS4 controller. * @return The battery level in the range 0-15. */ uint8_t getBatteryLevel() { return ps4Data.status.battery; };

I would like it to serial print and I will have a string of 5 leds. I would like the all leds to all be lit when fully charged and decrease the amount of leds lit as the charge lessens.

I have tried

if (PS4.getButtonPress(UP)) { Serial.print(F("\r\nUp")); PS4.setLed(Red); CamPitch = CamPitch + 20; Serial.print(PS4.getBatteryLevel());

Thanks in advance for any help..

Did you add the github library to your IDE and #include it in your code?

Also, post all of your code in tags. Lastly, show us an example of what happens when you run the code.

Just curious - how are you hooking everything up? What Arduino/shields are you using?

Thanks for your help. You were correct, I did not # include <PS4Parser.h>. Now I can get the battery level reading displayed on the serial monitor.

the code looks like this:

// http://jakubstas.com/build-your-own-robot-arm-remote-control/
/*
Example sketch for the PS4 Bluetooth library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <PS4BT.h>
#include <PS4USB.h>
#include <usbhub.h>
#include <PS4Parser.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int BlueLed = 3;
int ClearLed = 4;
int RedLed = 5;
int GreenLed = 6;
int BlueLed2 = 7;

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);

PS4USB PS4(&Usb);

float SERVOFREQ = 50;
float pulseconstant;
boolean limit_reached = false;

int timer = 40;

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

for ( int thispin = 3; thispin <7; thispin++) {
pinMode(thispin, OUTPUT);
}
pinMode(7,OUTPUT);

#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"));

pulseconstant = (1000000/SERVOFREQ)/4096;

pwm.begin();
pwm.setPWMFreq(SERVOFREQ);

}

void loop() {
Usb.Task();

if (PS4.connected()) {

limit_reached = false;

int LYValue;
LYValue = PS4.getAnalogHat(LeftHatY);
analogWrite(4, LYValue);

if (PS4.getButtonClick(CROSS)) {
Serial.print(F("\r\nCross "));
PS4.setLedFlash(10, 10); // Set it to blink rapidly
PS4.setRumbleOn(RumbleHigh);
Serial.print(PS4.getBatteryLevel());
}
}

Now I would like an led to blink when the cross is pressed. It should blink as many times as the voltage. ie: voltage reading = 9. llink led on off 9 times.