I have a bunch of momentary switches and I am having some trouble with figuring out how I should do my code for them. What I want to do is went a button is pressed it turns on, off, or makes the light inside the button flash. Then I want to execute a keyboard press. Then when pressed again I want it to change the light inside the button and execute a different keyboard press. What is the best way to do this? I have 6 momentary switches and 2 latching switches. I am currently waiting for help with the momentary ones so I figured I would try the latching ones. The first time the keyboard press on the latching switch executes happens immediately but the next one has like a 25 second delay in it. I didn't program that delay in and I can't figure out why it is happening. Thanks for your time and for any help you can provide. For those of you who are interested I am trying to make a control panel for No Limits 2 I will put a link below for a video that has a panel similar to mine in it. Code is also below. Thanks!
const int PowerSwitch = 12;
const int Floorlight = 11;
const int FloorButton = 10;
const int Stopresetlight = 9;
const int StopresetButton = 8;
const int Stoplight = 7;
const int StopButton = 6;
const int GatesSwitch = 5;
const int Harnesslight = 4;
const int HarnessButton = 3;
const int Dispatchlight = 2;
const int DispatchButton = 1;
boolean DispatchButtonbuttonPressed;
boolean GatesSwitchbuttonPressed;
#include <Keyboard.h>
void setup() {
pinMode(Dispatchlight,OUTPUT);
pinMode(DispatchButton, INPUT_PULLUP);
pinMode(Harnesslight, OUTPUT);
pinMode(HarnessButton, INPUT_PULLUP);
pinMode(GatesSwitch, INPUT_PULLUP);
pinMode(StopButton, INPUT_PULLUP);
pinMode(Stoplight, OUTPUT);
pinMode(StopresetButton, INPUT_PULLUP);
pinMode(Stopresetlight, OUTPUT);
pinMode(FloorButton, INPUT_PULLUP);
pinMode(Floorlight, OUTPUT);
pinMode(PowerSwitch, INPUT_PULLUP);
digitalWrite(DispatchButton, HIGH);
digitalWrite(HarnessButton, HIGH);
digitalWrite(GatesSwitch, HIGH);
digitalWrite(StopButton, HIGH);
digitalWrite(StopresetButton, HIGH);
digitalWrite(FloorButton, HIGH);
digitalWrite(PowerSwitch, HIGH);
}
void loop(){
if(digitalRead(GatesSwitch) == LOW) {
GatesSwitchbuttonPressed = true;
}else{
GatesSwitchbuttonPressed = false;
}
if(GatesSwitchbuttonPressed == true){
Keyboard.press(0xCB);
Keyboard.release(0xCB);
digitalWrite(Dispatchlight, LOW);
digitalWrite(Stopresetlight,LOW);
digitalWrite(Harnesslight,LOW);
digitalWrite(Floorlight,LOW);
digitalWrite(Stoplight,LOW);
}if(GatesSwitchbuttonPressed == false){
Keyboard.press(0xCC);
Keyboard.release(0xCC);
if (digitalRead(DispatchButton) == HIGH) {
DispatchButtonbuttonPressed = true;
} else {
DispatchButtonbuttonPressed = false;
}
if(DispatchButtonbuttonPressed == true) {
Keyboard.press(0xCD);
Keyboard.release(0xCD);
digitalWrite(Dispatchlight, HIGH);
digitalWrite(Stopresetlight,HIGH);
digitalWrite(Harnesslight,HIGH);
digitalWrite(Floorlight,HIGH);
digitalWrite(Stoplight,HIGH);
delay(500);
digitalWrite(Stoplight,LOW);
delay(500);
}
else {
digitalWrite(Dispatchlight, HIGH);
digitalWrite(Stopresetlight,HIGH);
digitalWrite(Harnesslight,HIGH);
digitalWrite(Floorlight,HIGH);
digitalWrite(Stoplight,HIGH);
delay(500);
digitalWrite(Dispatchlight, LOW);
digitalWrite(Stopresetlight,LOW);
digitalWrite(Harnesslight,LOW);
digitalWrite(Floorlight,LOW);
digitalWrite(Stoplight,LOW);
delay(500);
}
}
}