Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(8,9,4,5,6,7);
- //This program will show a simple menu of car data: rpm, speed, temperature and consumption.
- int lcd_key=0;
- int adc_key_in=0, menuPos=0,menuPosc;
- String initMssg="Intentando conectar con el vehiculo...";
- String temperatureMssg="Ext. temp: ",rpmMssg="r.p.m.:", speedMssg="speed:",consumptionMssg="consumption:", noFunctionMssg="este boton no tiene funcion asignada,",okMssg="conectado con coche";
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- #define BUTTON_ADC_PIN A0 // A0 is the button ADC input
- #define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
- boolean firstTime=true;
- void setup(){
- pinMode(BUTTON_ADC_PIN,INPUT); //ensure A0 is an input
- digitalWrite(BUTTON_ADC_PIN,LOW); //ensure pullup is off on A0
- //lcd backlight control
- digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
- pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
- lcd.begin(16,2);
- lcd.setCursor(0,0);
- lcd.print(initMssg);
- showMessage(initMssg);
- lcd.clear();
- Serial.begin(9600);
- }
- void loop(){
- if(firstTime){
- lcd.clear();
- lcd.print(okMssg);
- showMessage(okMssg);
- firstTime=false;
- menuPosc=menuPos;
- }
- switch(read_KeypadBttn()){
- case btnUP:
- menuPos++;
- Serial.println("boton up pulsado");
- delay(170);
- break;
- case btnDOWN:
- delay(170);
- menuPos--;
- Serial.println("boton down pulsado");
- break;
- }
- if(menuPosc!=menuPos){
- printMenu();
- }
- }
- void printMenu(){
- if(menuPos<4){
- menuPos=menuPos+4;
- }
- lcd.clear();
- switch(menuPos%4){
- case 0:
- lcd.print(temperatureMssg);
- lcd.setCursor(0,1);
- lcd.print(rpmMssg);
- break;
- case 1:
- lcd.print(rpmMssg);
- lcd.setCursor(0,1);
- lcd.print(speedMssg);
- break;
- case 2:
- lcd.print(speedMssg);
- lcd.setCursor(0,1);
- lcd.print(consumptionMssg);
- break;
- case 3:
- lcd.print(consumptionMssg);
- lcd.setCursor(0,1);
- lcd.print(temperatureMssg);
- break;
- }
- delay(5);
- }
- int read_KeypadBttn(){
- adc_key_in=analogRead(0); //read the value from analog input 0
- //using the concept of voltage divider and analog input resolution 10 bits
- // For V1.1 us this threshold
- if(adc_key_in>1000) {
- return btnNONE;
- }
- else if (adc_key_in < 50){
- return btnRIGHT;
- }
- else if (adc_key_in < 250){
- return btnUP;
- }
- else if (adc_key_in < 450){
- return btnDOWN;
- }
- else if (adc_key_in < 650) {
- return btnLEFT;
- }
- else if (adc_key_in < 850){
- return btnSELECT;
- }
- else{
- return btnNONE;//if the others fail
- }
- }
- void showMessage(String message){
- if(message.length()>16){
- for(int i=0;i<message.length()-16;i++){
- lcd.scrollDisplayLeft();
- delay(500);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement