Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(8,9,4,5,6,7);
  3. //This program will show a simple menu of car data: rpm, speed, temperature and consumption.
  4. int lcd_key=0;
  5. int adc_key_in=0, menuPos=0,menuPosc;
  6. String initMssg="Intentando conectar con el vehiculo...";
  7. String temperatureMssg="Ext. temp: ",rpmMssg="r.p.m.:", speedMssg="speed:",consumptionMssg="consumption:", noFunctionMssg="este boton no tiene funcion asignada,",okMssg="conectado con coche";
  8. #define btnRIGHT 0
  9. #define btnUP 1
  10. #define btnDOWN 2
  11. #define btnLEFT 3
  12. #define btnSELECT 4
  13. #define btnNONE 5
  14. #define BUTTON_ADC_PIN A0 // A0 is the button ADC input
  15. #define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
  16. boolean firstTime=true;
  17. void setup(){
  18. pinMode(BUTTON_ADC_PIN,INPUT); //ensure A0 is an input
  19. digitalWrite(BUTTON_ADC_PIN,LOW); //ensure pullup is off on A0
  20. //lcd backlight control
  21. digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
  22. pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
  23. lcd.begin(16,2);
  24. lcd.setCursor(0,0);
  25.  
  26. lcd.print(initMssg);
  27. showMessage(initMssg);
  28. lcd.clear();
  29. Serial.begin(9600);
  30. }
  31. void loop(){
  32. if(firstTime){
  33. lcd.clear();
  34. lcd.print(okMssg);
  35. showMessage(okMssg);
  36. firstTime=false;
  37. menuPosc=menuPos;
  38. }
  39. switch(read_KeypadBttn()){
  40. case btnUP:
  41. menuPos++;
  42. Serial.println("boton up pulsado");
  43. delay(170);
  44. break;
  45. case btnDOWN:
  46. delay(170);
  47. menuPos--;
  48. Serial.println("boton down pulsado");
  49. break;
  50. }
  51. if(menuPosc!=menuPos){
  52. printMenu();
  53. }
  54. }
  55.  
  56. void printMenu(){
  57.  
  58. if(menuPos<4){
  59. menuPos=menuPos+4;
  60. }
  61. lcd.clear();
  62. switch(menuPos%4){
  63. case 0:
  64. lcd.print(temperatureMssg);
  65. lcd.setCursor(0,1);
  66.  
  67. lcd.print(rpmMssg);
  68. break;
  69. case 1:
  70. lcd.print(rpmMssg);
  71. lcd.setCursor(0,1);
  72.  
  73. lcd.print(speedMssg);
  74. break;
  75. case 2:
  76. lcd.print(speedMssg);
  77. lcd.setCursor(0,1);
  78.  
  79. lcd.print(consumptionMssg);
  80. break;
  81. case 3:
  82. lcd.print(consumptionMssg);
  83. lcd.setCursor(0,1);
  84. lcd.print(temperatureMssg);
  85. break;
  86.  
  87. }
  88. delay(5);
  89. }
  90.  
  91. int read_KeypadBttn(){
  92. adc_key_in=analogRead(0); //read the value from analog input 0
  93. //using the concept of voltage divider and analog input resolution 10 bits
  94. // For V1.1 us this threshold
  95. if(adc_key_in>1000) {
  96. return btnNONE;
  97. }
  98. else if (adc_key_in < 50){
  99. return btnRIGHT;
  100. }
  101. else if (adc_key_in < 250){
  102. return btnUP;
  103. }
  104. else if (adc_key_in < 450){
  105. return btnDOWN;
  106. }
  107. else if (adc_key_in < 650) {
  108. return btnLEFT;
  109. }
  110. else if (adc_key_in < 850){
  111. return btnSELECT;
  112. }
  113. else{
  114. return btnNONE;//if the others fail
  115. }
  116.  
  117. }
  118. void showMessage(String message){
  119. if(message.length()>16){
  120. for(int i=0;i<message.length()-16;i++){
  121. lcd.scrollDisplayLeft();
  122. delay(500);
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement