/*
Andrew Ingram
01 July 2023
UEEIC0010 Counter project
Details:
Faults/trouble shooting:
1. cchange from individual LED pin assignments to group
2. All LEDS lighting up when push button pressed
Add in delay bounce
3. Add in resistors for pushbuttons and LEDs
4. Add in i = 0 to start counter from zero
5. Add in i++ to continue the count after it reaches 8
6. ++count added to execute whie in state
Tested and working 8/07/202
*/
//Set constant integers for each pin
const int LED_PINS[] = {8, 9, 10, 11}; // LED Pin assignments updated to be grouped for conditions setup
const int UPpin = 2; // Up push button pin
const int Downpin = 3; // Down push button pin
// Set integer for starting count
int count = 0;
// Variables to track button states and debounce
bool upButton_State = LOW; // Real state of the up button
bool downButton_State = LOW; // Real state of the down button
bool Upbutton_pushed = false; // Flag when button is pushed
bool Downbutton_pushed = false; // Flag when button is pushed
//add debounce for push buttons
const unsigned long DEBOUNCE_DELAY = 20; // Debounce delay in milliseconds
void setup() {
// Set LED pins as outputs
for (int i = 0; i < 4; i++) { //execute the LED pin output modes if these conditions are met
pinMode(LED_PINS[i], OUTPUT); // Set all LED pins as outputs
}
// Set pushbutton pins as Inputs
pinMode(UPpin, INPUT); // Up push button pin input
pinMode(Downpin, INPUT); // Down push button pin input
}
void loop() {
// Read Button States
bool Actual_Upbutton_State = digitalRead(UPpin); // Read state of the up button
bool Actual_Downbutton_state = digitalRead(Downpin); // Read state of the down button
// Check for push button states and debounce delay
//if not equal add debounce delay
if (Actual_Upbutton_State != upButton_State) { // If instant up button state does not equal boolean Up button state
//delay bounce installed after all LED were lighting up when Up Push Button was pushed
delay(DEBOUNCE_DELAY); // Debounce delay
//
if (Actual_Upbutton_State == digitalRead(UPpin)) { // If instant state is equal after debounce
upButton_State = Actual_Upbutton_State; // Update the up button state
//If upbutton state is high up button pushed is true
if (upButton_State == HIGH) { // If the up button is pushed
Upbutton_pushed = true; // Set the up button pressed flag
}
}
}
if (Actual_Downbutton_state != downButton_State) { // // If instant down button state does not equal boolean Up button state
// debounce delay added as above
delay(DEBOUNCE_DELAY); // Debounce delay
if (Actual_Downbutton_state == digitalRead(Downpin)) { // If instant state is equal after debounce
downButton_State = Actual_Downbutton_state; // Update the down button state
//If down button state is high down button pushed is true
if (downButton_State == HIGH) { // If the down button is pushed
Downbutton_pushed = true; // Set the down button pushed flag
}
}
}
// Counter actions
if (Upbutton_pushed && upButton_State == LOW) { // If the up button is still pushed
Upbutton_pushed = false; // Reset the up button pushed flag
++count; // Increment count value while executing
if (count > 15) {// If count is greater than 15
count = 0; // Reset count to zero
}
}
if (Downbutton_pushed && downButton_State == LOW) { // If the down button is still pushed
Downbutton_pushed = false; // Reset the down button pressed flag
--count; // Decrement count value while executing
if (count < 0) {
count = 15; // Set count to 15 if it goes below 0
}
}
// Display the count on LEDs
for (int i = 0; i < 4; i++) {
digitalWrite(LED_PINS[i], bitRead(count, i)); // Write to LED pins depending on count
}
}
1
2
4
8
UP
DOWN