I was hoping someone can review my code for a project I've been working on. I've only been at this a few weeks and I'm sure I've implemented this in the most roundabout way possible.
Either way - it works for my purpose, but would appreciate any feedback so I know what I could do better on next time (or improve on this code!).
Thank you very much.
/*
* The goal is to have X (currently 2) toggle switches which, when used, turn an LED on or off and performs a single keystroke.
*/
int switch1Pin = 4; // Indicates Toggle1 is connected to Pin 4.
int led1Pin = 7; // Indicates LED1 is connected to Pin 7.
int val; // Indicates variable for reading Toggle1 status.
int val2; // Indicates variable for reading Toggle1 delayed/debounced status.
int button1State; // Indicates the state of Toggle1.
int light1Mode = 0; // Indicates the status of the LED1 (On/Off).
int switch2Pin = 9; // Indicates Toggle2 is connected to Pin 9.
int led2Pin = 12; // Indicates LED2 is connected to Pin 7.
int val3; // Indicates variable for reading Toggle2 status.
int val4; // Indicates variable for reading Toggle2 delayed/debounced status.
int button2State; // Indicates the state of Toggle2.
int light2Mode = 0; // Indicates the status of LED2 (On/Off).
void setup() {
pinMode(switch1Pin, INPUT); // Indicates Toggle1 is the input.
pinMode(led1Pin, OUTPUT); // Indicates LED1 is the output.
pinMode(switch2Pin, INPUT); // Indicates Toggle2 is the input.
pinMode(led2Pin, OUTPUT); // Indicates LED2 is the output.
Serial.begin(9600); // Sets up serial communication with PC at 9600bps.
button1State = digitalRead(switch1Pin); // Indicates the intial state of Toggle1.
button2State = digitalRead(switch2Pin); // Indicates the intial state of Toggle2.
}
void loop(){
val = digitalRead(switch1Pin); // Indicates the value of Toggle1 and stores in it a variable.
delay(10); // Indicates a 10 milliseconds delay.
val2 = digitalRead(switch1Pin); // Indicates the value of Toggle1 and stores it in a variable.
if (val == val2) { // Confirmation of 2 consistent readings of Toggle1.
if (val != button1State) { // If values are not consistent and,
if (val == LOW) { // If Toggle1 was used and,
if (light1Mode == 0) { // If LED1 is off,
light1Mode = 1; // Then turn on LED1 and,
digitalWrite(led1Pin, HIGH); // Indicates the voltage of LED1 is changed to high.
Keyboard.write('a'); // Type the letter indicated and,
delay(50); // Indicates a 50 milliseconds delay.
Keyboard.release('a'); // Release the letter indicated.
} else { // else
light1Mode = 0; // Then turn off LED2 and,
digitalWrite(led1Pin, LOW); // Indicates the voltage of LED1 is changed to low.
Keyboard.write('a'); // Type the letter indicated and,
delay(50); // Indicates a 50 milliseconds delay.
Keyboard.release('a'); // Release the letter indicated.
}
}
}
button1State = val; // Indicates the changed state has been stored as a variable.
}
val3 = digitalRead(switch2Pin); // Indicates the value of Toggle2 and stores in it a variable.
delay(10); // Indicates a 10 milliseconds delay.
val4 = digitalRead(switch2Pin); // Indicates the value of Toggle2 and stores it in a variable.
if (val3 == val4) { // Confirmation of 2 consistent readings of Toggle1.
if (val3 != button2State) { // If values are not consistent and,
if (val3 == LOW) { // If Toggle2 was used and,
if (light2Mode == 0) { // If LED2 is off,
light2Mode = 1; // Then turn on LED2 and,
digitalWrite(led2Pin, HIGH); // Indicates the voltage of LED2 is changed to high.
Keyboard.write('b'); // Type the letter indicated and,
delay(50); // Indicates a 50 milliseconds delay
Keyboard.release('b'); // Release the letter indicated.
} else { // else
light2Mode = 0; // Then turn off LED2 and,
digitalWrite(led2Pin, LOW); // Indicates the voltage of LED2 is changed to low.
Keyboard.write('b'); // Type the letter indicated and,
delay(50); // Indicates a 50 milliseconds delay.
Keyboard.release('b'); // Release the letter indicated.
}
}
}
button2State = val3; // Indicates the changed state has been stored as a variable.
}
}