Created
February 19, 2021 23:23
-
-
Save mwinkler/62283120aeae0bdd1eab3df71b1ada37 to your computer and use it in GitHub Desktop.
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
#include <BleGamepad.h> | |
BleGamepad bleGamepad; | |
int btn_last = 0; | |
int btn_pin = 4; | |
int poti_pin = 34; | |
int con = 0; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("Starting BLE work!"); | |
bleGamepad.begin(); | |
pinMode(btn_pin, INPUT); | |
pinMode(poti_pin, INPUT); | |
} | |
void loop() | |
{ | |
if(bleGamepad.isConnected()) | |
{ | |
if (con == 0) | |
{ | |
con = 1; | |
Serial.println("BLE connected"); | |
} | |
// button | |
int btn_current = digitalRead(btn_pin); | |
if (btn_current != btn_last) | |
{ | |
btn_last = btn_current; | |
if (btn_current == 1) | |
{ | |
Serial.println("Press button 1"); | |
bleGamepad.press(BUTTON_1); | |
} | |
else | |
{ | |
Serial.println("Release button 1"); | |
bleGamepad.release(BUTTON_1); | |
} | |
} | |
// x axis | |
int poti_val = analogRead(poti_pin); | |
int poti_x = map(poti_val, 0, 4095, 32737, -32737); | |
bleGamepad.setX(poti_x); | |
} | |
else | |
{ | |
if (con == 1) | |
{ | |
con = 0; | |
Serial.println("BLE disconnected"); | |
} | |
} | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment