I am currently working on a DIY controller for Train Sim World. I am relatively new to the Arduino and the C programming language. My code is structured to look for button presses or PS2 joystick inputs and print a specific character for each button. I have also included a kill switch on pin 3. However when I run the code and open Notepad to see the results, the Arduino keeps printing the = character even if pin 3 is not pulled low. After I pull pin 3 low and press a button, there is a delay of about two seconds during which the = character keeps being printed over and over again, and after these 2 seconds elapse the Arduino prints the character attributed to the button several times before returning to printing = over and over again.
What can I do to fix this?
#include <Keyboard.h>
void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
}
bool pantograph = 0;
void loop() {
while (digitalRead(3) == LOW) {
Keyboard.write("M");
if (digitalRead(1) == LOW) {
Keyboard.write("W");
} else if (digitalRead(2) == LOW) {
Keyboard.write("S");
} else if (digitalRead(0) == LOW) {
Keyboard.write("Q");
} else if (digitalRead(5) == LOW) {
Keyboard.write("Y");
} else if (digitalRead(6) == LOW) {
Keyboard.write("T");
} else if (digitalRead(7) == LOW) {
Keyboard.write("G");
} else if (digitalRead(8) == LOW) {
Keyboard.write("K");
} else if (digitalRead(9) == LOW) {
Keyboard.write("Z");
} else if (digitalRead(10) == LOW) {
if (pantograph == 0) {
Keyboard.write("P");
} else {
Keyboard.write("O");
}
} else if (digitalRead(11) == LOW) {
Keyboard.write(9);
} else if (digitalRead(12) == LOW) {
Keyboard.write("N");
} else if (digitalRead(13) == LOW) {
Keyboard.write(32);
} else if (analogRead(A0) > 700) {
Keyboard.write(93);
} else if (analogRead(A0) < 200) {
Keyboard.write(91);
} else if (analogRead(A1) > 700) {
Keyboard.print(39);
} else if (analogRead(A1) < 200) {
Keyboard.print(59);
} else if (analogRead(A2) > 700) {
Keyboard.print("A");
} else if (analogRead(A2) < 200) {
Keyboard.print("D");
}
}
delay(1000);
}
Keyboard.begin()at the end of the setup function. – PeterJ Nov 18 '18 at 05:22whilethe program slows down to printing once every second, as expected, and the buttons' associated characters only print once, but the program still prints the "=" over and over again – Nov 18 '18 at 06:24