Created
December 2, 2015 04:05
-
-
Save schie/ab9af92c4ce4e0276b7d to your computer and use it in GitHub Desktop.
Arduino Blinker w/ LCD KeyPad Shield
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> | |
| // select the pins used on the LCD panel | |
| LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | |
| // define some values used by the panel and buttons | |
| #define btnRIGHT 0 | |
| #define btnUP 1 | |
| #define btnDOWN 2 | |
| #define btnLEFT 3 | |
| #define btnSELECT 4 | |
| #define btnNONE 5 | |
| #define toggledRelay 23 // relay 1 | |
| int currentPosition = 0; | |
| int previousButtonPressTime = 0; | |
| int previousFlashingTime = 0; | |
| int amount = 0; | |
| bool relayIsOn = false; | |
| // read the buttons | |
| int read_LCD_buttons() | |
| { | |
| int adc_key_in = analogRead(0); // read the value from the sensor | |
| // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 | |
| // we add approx 50 to those values and check to see if we are close | |
| if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result | |
| // For V1.1 us this threshold | |
| if (adc_key_in < 50) return btnRIGHT; | |
| if (adc_key_in < 250) return btnUP; | |
| if (adc_key_in < 450) return btnDOWN; | |
| if (adc_key_in < 650) return btnLEFT; | |
| if (adc_key_in < 850) return btnSELECT; | |
| return btnNONE; // when all others fail, return this... | |
| } | |
| void setup() | |
| { | |
| pinMode(toggledRelay, OUTPUT); | |
| pinMode(toggledRelay + 2, OUTPUT); | |
| pinMode(toggledRelay + 4, OUTPUT); | |
| digitalWrite(toggledRelay + 2, LOW); | |
| digitalWrite(toggledRelay + 4, LOW); | |
| lcd.begin(16, 2); // start the library | |
| lcd.setCursor(0, 0); | |
| lcd.print("---D F T B A---"); // print a simple message | |
| lcd.setCursor(0, 1); | |
| lcd.print("0 0 0 0 0 0 0 0"); // print a simple message | |
| lcd.blink(); | |
| lcd.cursor(); | |
| Serial.begin(9600); | |
| } | |
| void loop() | |
| { | |
| lcd.setCursor(currentPosition * 2, 1); // move to the begining of the second line | |
| int lcd_key = read_LCD_buttons(); // read the buttons | |
| int newTime = millis(); | |
| int delta = newTime - previousButtonPressTime; | |
| if (delta >= 500) | |
| { | |
| switch (lcd_key) // depending on which button was pushed, we perform an action | |
| { | |
| case btnRIGHT: | |
| { | |
| currentPosition = (currentPosition >= 7) ? 7 : currentPosition + 1; | |
| previousButtonPressTime = newTime; | |
| break; | |
| } | |
| case btnLEFT: | |
| { | |
| currentPosition = (currentPosition <= 0) ? 0 : currentPosition - 1; | |
| previousButtonPressTime = newTime; | |
| break; | |
| } | |
| case btnSELECT: | |
| { | |
| bool isAlreadySelected = amount & (1 << currentPosition); | |
| int temp = (1 << currentPosition) * pow(-1 , isAlreadySelected); | |
| amount += temp; | |
| Serial.println(amount); | |
| lcd.print(!isAlreadySelected); | |
| previousButtonPressTime = newTime; | |
| break; | |
| } | |
| case btnNONE: | |
| { | |
| break; | |
| } | |
| } | |
| lcd.setCursor(currentPosition * 2, 1); | |
| } | |
| delta = newTime - previousFlashingTime; | |
| if(amount != 0 && delta > (1000.0 / amount)){ | |
| previousFlashingTime = newTime; | |
| relayIsOn = !relayIsOn; | |
| digitalWrite(toggledRelay, relayIsOn ? HIGH : LOW); | |
| } else if(amount == 0 && delta > (1000.0 / amount)){ | |
| previousFlashingTime = newTime; | |
| relayIsOn = true; | |
| digitalWrite(toggledRelay, HIGH); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment