Created
July 2, 2017 16:09
-
-
Save mnemocron/3b577a32b9c9da6ee867a6205fee9617 to your computer and use it in GitHub Desktop.
Control a brushless DC motor wirelessly using a Dualshock 4 controller and an ESC.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author Simon Burkhardt - github.com/mnemocron | |
| * @date 2017-07-02 | |
| * | |
| * This sketch demonstrates, that you can control a brushless DC motor wirelessly using a Dualshock 4 controller. | |
| * | |
| * Connections: 11.1V power supply | |
| * | | |
| * v | |
| * Dualshock 4 --(Bluetooth)--> USB Host Shield (w/Bluetooth adapter) --> Arduino --> ESC --> brushless DC motor | |
| * | |
| * It might be necessary to calibrate your ESC before using. | |
| * Also the first time connecting a new Dualshock 4 to your USB host shield, you need to | |
| * conntect using the following code below: PS4BT PS4(&Btd, PAIR); | |
| * | |
| * WARNING ! | |
| * I recommend not using a Li-Po battery on a breadboard setup. | |
| * Li-Po batteries can easily deliver 100 Amps and more on a short ciruit. | |
| * This can damage components and could result in a fire. | |
| * Be safe, use a bench power supply. | |
| * | |
| */ | |
| #include <PS4BT.h> | |
| #include <usbhub.h> | |
| #include <Servo.h> | |
| #ifdef dobogusinclude | |
| #include <spi4teensy3.h> | |
| #include <SPI.h> | |
| #endif | |
| 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 paring 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); | |
| Servo ESC; | |
| int esc_value = 1500; | |
| int left_y = 127; | |
| void setup() { | |
| ESC.attach(7); // don't use pin an SPI pin! | |
| ESC.writeMicroseconds(esc_value); | |
| 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(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117) { | |
| // 255 = down / 0 = up | |
| left_y = 255 - PS4.getAnalogHat(LeftHatY); | |
| esc_value = map(left_y, 0, 255, 1000, 2000); | |
| // Serial.print(F("\tLeftHatY: ")); | |
| // Serial.print(PS4.getAnalogHat(LeftHatY)); | |
| Serial.println(esc_value); | |
| } else { | |
| esc_value = 1500; | |
| } | |
| ESC.writeMicroseconds(esc_value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment