Created
May 4, 2020 13:42
-
-
Save fisherds/baf3f0733ffc49157f6802e4a4bec54b to your computer and use it in GitHub Desktop.
Dr. Fisher solution to the LCD and Analog lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <LiquidCrystal.h> | |
| /*** LCD ***/ | |
| LiquidCrystal lcd(13, 12, 11, 10, 9, 8); | |
| #define LINE_1 0 | |
| #define LINE_2 1 | |
| #define PIN_PUSHBUTTON_GREEN 2 | |
| #define PIN_PUSHBUTTON_YELLOW 3 | |
| #define PIN_PUSHBUTTON_BLUE 4 | |
| #define PIN_LED_GREEN 6 | |
| #define PIN_LED_YELLOW 7 | |
| #define PIN_ANALOG_POT 0 | |
| unsigned long lastPrintTime = 0; | |
| uint16_t greenCounter = 0; | |
| uint16_t yellowCounter = 0; | |
| /*** Interrupt flags ***/ | |
| volatile int mainEventFlags = 0; | |
| #define FLAG_PUSHBUTTON_GREEN 0x01 | |
| #define FLAG_PUSHBUTTON_YELLOW 0x02 | |
| void setup() { | |
| Serial.begin(9600); | |
| lcd.begin(16, 2); | |
| lcd.print("Dave Fisher"); | |
| pinMode(PIN_PUSHBUTTON_YELLOW, INPUT_PULLUP); | |
| pinMode(PIN_PUSHBUTTON_GREEN, INPUT_PULLUP); | |
| pinMode(PIN_PUSHBUTTON_BLUE, INPUT_PULLUP); | |
| attachInterrupt(digitalPinToInterrupt(PIN_PUSHBUTTON_GREEN), green_pushbutton_isr, FALLING); | |
| attachInterrupt(digitalPinToInterrupt(PIN_PUSHBUTTON_YELLOW), yellow_pushbutton_isr, FALLING); | |
| pinMode(PIN_LED_GREEN, OUTPUT); | |
| pinMode(PIN_LED_YELLOW, OUTPUT); | |
| digitalWrite(PIN_LED_GREEN, LOW); | |
| digitalWrite(PIN_LED_YELLOW, LOW); | |
| // No need to setup the Analog inputs, that is the default | |
| } | |
| void loop() { | |
| int potReading = analogRead(PIN_ANALOG_POT); | |
| unsigned long currentTime = millis(); | |
| if ((currentTime - lastPrintTime) > 2000) { | |
| lastPrintTime = currentTime; | |
| Serial.print("Pot = "); | |
| Serial.println(potReading); | |
| } | |
| lcd.setCursor(0, LINE_2); | |
| lcd.print("Y="); | |
| lcd.print(yellowCounter); | |
| lcd.print(" G="); | |
| lcd.print(greenCounter); | |
| lcd.print(" T="); | |
| lcd.print(millis()/1000); | |
| lcd.print(" "); | |
| if (mainEventFlags & FLAG_PUSHBUTTON_GREEN) { | |
| delay(20); | |
| mainEventFlags &= ~FLAG_PUSHBUTTON_GREEN; | |
| if (!digitalRead(PIN_PUSHBUTTON_GREEN)) { | |
| // do stuff | |
| Serial.println("Press green"); | |
| greenCounter++; | |
| digitalWrite(PIN_LED_GREEN, HIGH); | |
| digitalWrite(PIN_LED_YELLOW, LOW); | |
| } | |
| } | |
| if (mainEventFlags & FLAG_PUSHBUTTON_YELLOW) { | |
| delay(20); | |
| mainEventFlags &= ~FLAG_PUSHBUTTON_YELLOW; | |
| if (!digitalRead(PIN_PUSHBUTTON_YELLOW)) { | |
| // do stuff | |
| Serial.println("Press yellow"); | |
| yellowCounter++; | |
| digitalWrite(PIN_LED_GREEN, LOW); | |
| digitalWrite(PIN_LED_YELLOW, HIGH); | |
| } | |
| } | |
| if (!digitalRead(PIN_PUSHBUTTON_BLUE)) { | |
| // do stuff | |
| digitalWrite(PIN_LED_GREEN, LOW); | |
| digitalWrite(PIN_LED_YELLOW, LOW); | |
| greenCounter = 0; | |
| yellowCounter = 0; | |
| } | |
| delay(100); | |
| } | |
| // Simple ISRs that set flags only | |
| void green_pushbutton_isr() { | |
| mainEventFlags |= FLAG_PUSHBUTTON_GREEN; | |
| } | |
| void yellow_pushbutton_isr() { | |
| mainEventFlags |= FLAG_PUSHBUTTON_YELLOW; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment