Hi, I just got the Arduino micro, and I'm very bad at writing code...
I found this code down below at http://forum.arduino.cc/index.php?topic=121129.0
My problem is that when I press the toggle switch it just spits out G's with 100ms delay, and stops when I turn the toggle switch off. My goal is to make it write a G only once on my computer when the toggle switch is pressed on or off (changing state). This way I can make a toggle switch that retracts a landing gear ingame whenever its pressed. I do not know what to do next... I'm hoping anyone have a solution. Thanks.
GIF by silviustro
#include <Keyboard.h>
boolean toggleState; // hold current state of switch
boolean lastToggleState; // hold last state to sense when switch is changed
long toggleTimer = millis(); // // debounce timer
const int toggleSwitchPin = 9;
void setup(){
// setup stuff here
}
void loop(){
toggleState = digitalRead(toggleSwitchPin);
if (millis() - toggleTimer > 100){ // debounce switch 100ms timer
if (toggleState && !lastToggleState) { // if switch is on but was previously off
lastToggleState = true; // switch is now on
toggleTimer = millis(); // reset timer
Keyboard.write('G');
}
if (!toggleState && lastToggleState){ // if switch is off but was previously on
lastToggleState = false; // switch is now off
toggleTimer = millis(); // reset timer
}
}
}
Code is originally written by