Hey JackReacher,
Not giving up on you,
Try this:
Code: Select all
#include <Bounce.h>
#define NUM_KEYS 12
struct Key {
char keycode;
Bounce* bounce;
};
Key keys[NUM_KEYS];
Key key(char keycode, int pin) {
Key *ret = new Key;
ret->keycode = keycode;
ret->bounce = new Bounce(pin, 10);
pinMode(pin, INPUT_PULLUP);
return *ret;
}
void setupKeys() {
keys[0] = key('w', 0);
keys[1] = key('s', 1);
keys[2] = key('a', 2);
keys[3] = key('d', 3);
keys[4] = key('p', 4);
keys[5] = key('l', 5);
keys[6] = key('o', 6);
keys[7] = key('k', 7);
keys[8] = key('x', 8);
keys[9] = key('z', 9);
keys[10] = key('q',10);
keys[11] = key('e',11);
}
const int ledPin = 20;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
char i;
void setup() {
setupKeys();
Keyboard.begin();
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// pinMode(0, INPUT_PULLUP);
}
void loop() {
for (int i = 0; i < NUM_KEYS; i++) {
keys[i].bounce->update();
if (keys[i].bounce->fallingEdge()) {
Keyboard.press(keys[i].keycode);
} else if (keys[i].bounce->risingEdge()) {
Keyboard.release(keys[i].keycode);
}
}
int sensorValue = analogRead(21); // read the input on analog pin 0:
//Serial.println(sensorValue); // print out the value you read
float voltage = sensorValue * (4.88 / 1023.00);
//Serial.println(voltage);
if(Serial.available())
{
i=Serial.read();
}
if(i=='1')
{
Serial.println(sensorValue);
}
i='0';
if (voltage > 4.5)
{
digitalWrite(16, HIGH);
}
else
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
delay(10);
}
A couple of things you have to do before compiling and loading the sketch on your teensy:
The analog input is set to pin 21 on your teensy, change that if you wan't to use an other (analog) input.
The ledpin is set to pin 20 (so you have the two pins for battery monitoring close to each other) but you can change that also to your liking.
You also can edit the input (buttons) pins to your setup that's in the "void setupKeys()" part, every number after
"keys[0] = key("letter", "number")" is the pin you want to assign to a button (do NOT edit the number between the two []).
You can find a keyboard/retropie listing here
https://github.com/RetroPie/RetroPie-Se ... ontrollers
And one more other thing, set up your board like this:
- screenshot.jpg (234.7 KiB) Viewed 21157 times
(the screenshot is the dutch version of the ide but i thing you will get it)
Its important to do this or you will (again) get a lot of annoying errors (and you've had enough of those).
Now your sketch should compile and upload without any errors!
The sketch handles keypresses but the result is the same!
Hope this does the trick, and battery monitoring will work...
Keep me posted!
And one final edit, if you realy want to go to joystick mode take a look at this page
https://www.pjrc.com/teensy/td_joystick.html