Advertisement
pippero

Untitled

Sep 14th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  3. #define btnRIGHT 0 // Definizione dei numeri associati ai pulsanti
  4. #define btnUP 1
  5. #define btnDOWN 2
  6. #define btnLEFT 3
  7. #define btnSELECT 4
  8. #define btnNONE 5
  9. #define Backlight 10
  10. int stato = 0;
  11. int adc_key_in = 0;
  12. int Ultimo_lcd_key = 0;
  13. int lcd_key = 0;
  14. int read_LCD_buttons()
  15. {
  16. adc_key_in = analogRead(0); // Legge il valore del convertitore ADC
  17. // I miei pulsanti danno questi valori: 0, 132, 321, 490, 725 (R,U,D,L,S)
  18. if (adc_key_in > 800) return btnNONE; // "None" per primo per velocizzare
  19. if (adc_key_in < 50) return btnRIGHT;
  20. if (adc_key_in < 200) return btnUP;
  21. if (adc_key_in < 400) return btnDOWN;
  22. if (adc_key_in < 500) return btnLEFT;
  23. if (adc_key_in < 800) return btnSELECT;
  24. return btnNONE; // Se nessun pulsante viene premuto...
  25. }
  26.  
  27.  
  28.  
  29. void setup() {
  30. Serial.begin(9600);
  31. pinMode(Backlight, OUTPUT);
  32. digitalWrite(Backlight, HIGH); // Accende retroilluminazione
  33. lcd.begin(16, 2);
  34. }
  35.  
  36. void loop() {
  37. lcd_key = read_LCD_buttons(); // Fa partire la funzione che legge i bottoni
  38. if ( lcd_key != Ultimo_lcd_key ) {
  39.  
  40.  
  41. Serial.println(stato);
  42. if ( lcd_key == btnUP) { //tasto up
  43. stato ++ ;
  44. }
  45. if (lcd_key == btnDOWN) {
  46. stato --;
  47. }
  48. Ultimo_lcd_key = lcd_key;
  49. }
  50. lcd.setCursor(0, 0);
  51. lcd.print("stato "); lcd.print(stato );
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement