//Sample using LiquidCrystal library #include #define PRESENCE_THRESHOLD 300 // 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 int lcd_key = 0; int presence = 0; int adc_key_in = 0; int adc_presence_in = 0; #define btnRIGHT 0 #define btnUP 1 #define btnDOWN 2 #define btnLEFT 3 #define btnSELECT 4 #define btnNONE 5 #define btnPRESENT 6 #define inputSIZE 64 byte location = 0; byte b; char input[inputSIZE]; int x; int current_key; int last_key; // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers #define RED_LED 11 #define YELLOW_LED 12 #define GREEN_LED 13 void processCommand() { if (input[0] != '#') { return; } switch(input[1]){ case 'L': //LED //RED = 1 //YELLOW = 2 //GREEN = 3 if (input[2]=='0'){ digitalWrite(11,input[3]-'0'); digitalWrite(12,input[3]-'0'); digitalWrite(13,input[3]-'0'); } digitalWrite((input[2]-'0')+10,input[3]-'0'); break; case 'D': //DISPLAY if (input[2]=='C'){ lcd.clear(); return; } lcd.setCursor((input[2]-'0')*10+ (input[3]-'0'),(input[4]-'0')); x = 5; while(input[x]!=0 && x 800) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result 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; // when all others fail, return this... } // read the buttons int read_presence_button() { adc_presence_in = analogRead(1); // read the value from the sensor /* lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over lcd.print(adc_presence_in); // display seconds elapsed since power-up */ //Serial.println(adc_presence_in); // 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_presence_in > PRESENCE_THRESHOLD && presence == 0){ delay(500); adc_presence_in = analogRead(1); if (adc_presence_in > PRESENCE_THRESHOLD){ return 1; // We make this the 1st option for speed reasons since it will be the most likely result } } else if (adc_presence_in > PRESENCE_THRESHOLD && presence == 1){ return 1; } if (adc_presence_in < PRESENCE_THRESHOLD && presence == 1){ delay(500); adc_presence_in = analogRead(1); if (adc_presence_in < PRESENCE_THRESHOLD){ return 0; // We make this the 1st option for speed reasons since it will be the most likely result } } else if (adc_presence_in < PRESENCE_THRESHOLD && presence == 0){ return 0; } return 0; // when all others fail, return this... } void setup() { lcd.begin(16, 2); // start the library Serial.begin(57600); while(!Serial){ ; } pinMode(RED_LED, OUTPUT); pinMode(YELLOW_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT); pinMode(2, INPUT); pinMode(3, INPUT); lcd.setCursor(0,0); digitalWrite(2,HIGH); digitalWrite(3,HIGH); lcd.print("Not Connected"); lcd.setCursor(0,1); lcd.print("Waiting for pi"); Serial.println("#SB"); } void loop() { lcd_key = read_LCD_buttons(); // read the buttons if (last_key!=lcd_key){ lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (lcd_key!=current_key){ current_key = lcd_key; //lcd.setCursor(0,1); // move to the begining of the second line switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: { Serial.println("#BR"); break; } case btnLEFT: { Serial.println("#BL"); break; } case btnUP: { Serial.println("#BU"); break; } case btnDOWN: { Serial.println("#BD"); break; } case btnSELECT: { Serial.println("#BS"); break; } case btnNONE: { break; } } } } if (read_presence_button()!=presence){ presence = !presence; if (presence == 0){ Serial.println("#BP0"); } else if (presence ==1){ Serial.println("#BP1"); } } if (Serial.available() > 0) { b = Serial.read(); if (b=='\n' || location>=inputSIZE) { processCommand(); for (b = 0;b