I have an Arduino Leonardo which i am doing a proof of concept with for my flight sim. Currently, I am just using standard microswitches that are usually used for rc remotes. Some are simple on off switches some are momentary switches. I have the wiring done correctly to the best of my knowledge, as i have power running to switch, signal wire running to input and in parallel i have a 220 ohm resistor also coming from signal side of switch going to ground to avoid a floating switch. I have done this for all 12 inputs, and I feel like there is a better way to do this.
Now, I have had simple code written that works for my application but I need to be able to throw my switch and not have the input to repeat over and over, so i am using arguments on a loop to solve this, but I believe there is something wrong with my code. I have only written out 2 of the total 12 inputs i have wired up as a test after wiring my board and I get no outputs. I just have my outputs as typing a number as a keyboard. This is how I am going to do my switches anyways, so I'll just have to change mapping on my simulator to get everything set up.
So my question is threefold:
- Is my coding correct
- Is there a more efficient way of checking change of input value (high or low) without causing a noticeably long delay while working with 12 inputs (maybe more in the future).
- I am sure there is a better way to avoid a floating switch than wire a single 220 ohm resister on EVERY input, if so please let me know.
I am still learning and have been able to make this work with continuous outputs being triggered, but as I said I just want the output to trigger once until a change of state is detected.
I have looked around a lot and the way to do this but a lot of people's suggestions on other posts seems to confuse me.
#include <Keyboard.h>
int input01 = A0
int input01 = A1
int input01 = A2
int input01 = A3
int input01 = A4
int input01 = A5
int input01 = 01
int input01 = 02
int input01 = 03
int input01 = 04
int input01 = 05
int input01 = 06
// Current States of Switches
int input01c = 0;
int input02c = 0;
int input03c = 0;
int input04c = 0;
int input05c = 0;
int input06c = 0;
int input07c = 0;
int input08c = 0;
int input09c = 0;
int input10c = 0;
int input11c = 0;
int input12c = 0;
// Previous states of switches
int input01p = 0;
int input02p = 0;
int input03p = 0;
int input04p = 0;
int input05p = 0;
int input06p = 0;
int input07p = 0;
int input08p = 0;
int input09p = 0;
int input10p = 0;
int input11p = 0;
int input12p = 0;
void setup() {
pinMode(input01, INPUT);
pinMode(input02, INPUT);
pinMode(input03, INPUT);
pinMode(input04, INPUT);
pinMode(input05, INPUT);
pinMode(input06, INPUT);
pinMode(input07, INPUT);
pinMode(input08, INPUT);
pinMode(input09, INPUT);
pinMode(input10, INPUT);
pinMode(input11, INPUT);
pinMode(input12, INPUT);
}
void loop() {
input01c = digitalRead(input01);
input02c = digitalRead(input02);
input01c = digitalRead(input03);
input02c = digitalRead(input04);
input01c = digitalRead(input05);
input02c = digitalRead(input06);
input01c = digitalRead(input07);
input02c = digitalRead(input08);
input01c = digitalRead(input09);
input02c = digitalRead(input10);
input01c = digitalRead(input11);
input02c = digitalRead(input12);
if (input01c == input01p) {
if (input01c == HIGH) {
Keyboard.write(1);
}
else {
// Do nothing
}
delay(20);
}
input01c = input01p;
if (input02c == input02p) {
if (input02c == HIGH) {
Keyboard.write(2);
}
else {
// Do nothing
}
delay(20);
}
input02c = input02p;
}