Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Provides a definition for each of the buttons
- #define btnRIGHT 1
- #define btnUP 2
- #define btnDOWN 3
- #define btnLEFT 4
- #define btnSELECT 5
- #define btnNONE 0
- int inPin = A0; // the number of the input pin
- int outPin = 13; // the number of the output pin
- int state = HIGH; // the current state of the output pin
- int reading; // the current reading from the input pin
- int previous = LOW; // the previous reading from the input pin
- // the follow variables are long's because the time, measured in miliseconds,
- // will quickly become a bigger number than can be stored in an int.
- unsigned long time = 0; // the last time the output pin was toggled
- unsigned long debounce = 200; // the debounce time, increase if the output flickers
- void setup()
- {
- pinMode(outPin, OUTPUT);
- }
- void loop()
- {
- reading = read_LCD_buttons(inPin); // checks to see which button is currently pressed, if any
- // if the input just went from LOW and HIGH and we've waited long enough
- // to ignore any noise on the circuit, toggle the output pin and remember
- // the time
- if ( (reading > 0) && (previous == LOW) && ((millis() - time) > debounce) ) {
- switch (reading) {
- case btnRIGHT:
- // the right button was pressed.
- // toggles the state of the LED pin
- state = !state;
- break;
- case btnUP:
- // Up button was pressed.
- break;
- default:
- // no button was pressed?
- break;
- }
- time = millis();
- }
- // Constantly "writing" the state of the pin
- digitalWrite(outPin, state);
- previous = reading;
- }
- int read_LCD_buttons(int pinToRead)
- {
- int adc_key_in = analogRead(pinToRead);
- delay(5);
- if (5 < abs(adc_key_in)) return btnNONE;
- if (adc_key_in > 1000) return btnNONE;
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- return btnNONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement