Skip to content

Instantly share code, notes, and snippets.

@krunal2103 krunal2103/PS4 Controlled Robot Secret
Created Feb 26, 2015

Embed
What would you like to do?
PS4 Controlled Robot
#include <PS4BT.h>
#include <usbhub.h>
//*****Setup********
USB Usb;
BTD Btd(&Usb);
//PS4BT PS4(&Btd, PAIR); //comment if controller is already paired
PS4BT PS4(&Btd); //uncomment if controller is already paired
//******************
//*****Pin Declaration********
int mot[4][2] = {
{
14,15 }
,{
16,17 }
,{
18,19 }
,{
20,21 }
};
//****************************
void setup()
{
Serial.begin(115200);
if (Usb.Init() == -1)
{
Serial.print(F("\r\nOSC did not start"));
while (1); // Halt
}
Serial.print("\r\nPS4 Bluetooth Library Started");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 2; j++)
{
pinMode(mot[i][j], OUTPUT);
}
}
}
void loop() {
Usb.Task();
if(PS4.connected())
{
if(PS4.getButtonClick(PS))
{
digitalWrite(mot[0][0], LOW);
digitalWrite(mot[0][1], LOW);
digitalWrite(mot[1][0], LOW);
digitalWrite(mot[1][1], LOW);
digitalWrite(mot[2][0], LOW);
digitalWrite(mot[2][1], LOW);
digitalWrite(mot[3][0], LOW);
digitalWrite(mot[3][1], LOW);
PS4.disconnect(); //To turn the remote OFF
}
else if(PS4.getAnalogHat(LeftHatY) == 0)
{
digitalWrite(mot[0][0], HIGH);
digitalWrite(mot[0][1], LOW);
digitalWrite(mot[1][0], LOW);
digitalWrite(mot[1][1], LOW);
digitalWrite(mot[2][0], HIGH);
digitalWrite(mot[2][1], LOW);
digitalWrite(mot[3][0], LOW);
digitalWrite(mot[3][1], LOW); //Forward
}
else if(PS4.getAnalogHat(LeftHatY) == 255)
{
digitalWrite(mot[0][0], LOW);
digitalWrite(mot[0][1], HIGH);
digitalWrite(mot[1][0], LOW);
digitalWrite(mot[1][1], LOW);
digitalWrite(mot[2][0], LOW);
digitalWrite(mot[2][1], HIGH);
digitalWrite(mot[3][0], LOW);
digitalWrite(mot[3][1], LOW); //Backward
}
else if(PS4.getAnalogHat(LeftHatX) == 0)
{
digitalWrite(mot[0][0], LOW);
digitalWrite(mot[0][1], LOW);
digitalWrite(mot[1][0], LOW);
digitalWrite(mot[1][1], HIGH);
digitalWrite(mot[2][0], LOW);
digitalWrite(mot[2][1], LOW);
digitalWrite(mot[3][0], LOW);
digitalWrite(mot[3][1], HIGH); //Right
}
else if(PS4.getAnalogHat(LeftHatX) == 255)
{
digitalWrite(mot[0][0], LOW);
digitalWrite(mot[0][1], LOW);
digitalWrite(mot[1][0], HIGH);
digitalWrite(mot[1][1], LOW);
digitalWrite(mot[2][0], LOW);
digitalWrite(mot[2][1], LOW);
digitalWrite(mot[3][0], HIGH);
digitalWrite(mot[3][1], LOW); //Left
}
else
{
digitalWrite(mot[0][0], LOW);
digitalWrite(mot[0][1], LOW);
digitalWrite(mot[1][0], LOW);
digitalWrite(mot[1][1], LOW);
digitalWrite(mot[2][0], LOW);
digitalWrite(mot[2][1], LOW);
digitalWrite(mot[3][0], LOW);
digitalWrite(mot[3][1], LOW);
}
}
else
{
PS4BT PS4(&Btd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.