Skip to content

Instantly share code, notes, and snippets.

@honestcomrade
Created June 15, 2017 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honestcomrade/a6acc963103365480ae052b8484f28da to your computer and use it in GitHub Desktop.
Save honestcomrade/a6acc963103365480ae052b8484f28da to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <SPI.h>
#include <PS3USB.h>
#include <Usb.h>
USB Usb;
PS3USB PS3(&Usb);
Servo right_servo;
Servo left_servo;
void setup() {
Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.println(F("\r\nPS3USB Library Started!"));
right_servo.attach(9);
if (!right_servo.attached()) {
Serial.println("Right Servo not loaded");
}
Serial.println("Right Servo attached!");
left_servo.attach(8);
if (!right_servo.attached()) {
Serial.println("Left Servo not loaded");
}
Serial.println("Left Servo attached!");
}
void loop() {
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
// map ps3 right-stick motion to right servo
right_servo.write(map(PS3.getAnalogHat(RightHatX), 0, 255, 0, 180));
// map ps3 left-stick motion to left servo
left_servo.write(map(PS3.getAnalogHat(LeftHatX), 0, 255, 0, 180));
// listen for detach/reattach for right servo
if (PS3.getButtonClick(R2)) {
right_servo.detach();
Serial.println("Servo detached!");
}
if (PS3.getButtonClick(R1)) {
right_servo.attach(9);
Serial.println("Servo attached!");
}
// listen for detach/reattach for left servo
if (PS3.getButtonClick(L2)) {
left_servo.detach();
Serial.println("Servo detached!");
}
if (PS3.getButtonClick(L1)) {
left_servo.attach(8);
Serial.println("Servo attached!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment