Advertisement
pippero

Untitled

Sep 14th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 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.  
  39. if ( lcd_key !=5 && Ultimo_lcd_key==0 ) {
  40. Ultimo_lcd_key =1;
  41.  
  42. Serial.println(stato);
  43. if ( lcd_key == btnUP) { //tasto up
  44. stato=stato +1 ;
  45. }
  46. if (lcd_key == btnDOWN) {
  47. stato =stato-1;
  48. }
  49. }
  50. else if ( lcd_key ==5 ) {
  51. Ultimo_lcd_key =0;
  52.  
  53. }
  54.  
  55.  
  56. lcd.setCursor(0, 0);
  57. lcd.print("stato "); lcd.print(stato );
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement