Advertisement
pippero

Untitled

Sep 11th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. /*******************************************************
  2. Questo programma testa i pulsanti della LCD Shield
  3. by Luca Pertile 11/03/2013
  4. ********************************************************/
  5. int conta = 0;
  6. int conta1 = 0;
  7.  
  8. #include <LiquidCrystal.h>
  9. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  10. float t_max = 0.0;
  11. float t_min = 0.0;
  12. bool FIRST = true;
  13. int x = 0;
  14. int stato = 0;
  15. int old_stato = 0;
  16. int lcd_key = 0;
  17. int adc_key_in = 0;
  18. int old_lcd_key = 0;
  19. int old_key = 0;
  20. #define btnRIGHT 0 // Definizione dei numeri associati ai pulsanti
  21. #define btnUP 1
  22. #define btnDOWN 2
  23. #define btnLEFT 3
  24. #define btnSELECT 4
  25. #define btnNONE 5
  26. #define Backlight 10
  27.  
  28. // read the buttons
  29. int read_LCD_buttons()
  30. {
  31. adc_key_in = analogRead(0); // Legge il valore del convertitore ADC
  32. // I miei pulsanti danno questi valori: 0, 132, 321, 490, 725 (R,U,D,L,S)
  33. if (adc_key_in > 800) return btnNONE; // "None" per primo per velocizzare
  34. if (adc_key_in < 50) return btnRIGHT;
  35. if (adc_key_in < 200) return btnUP;
  36. if (adc_key_in < 400) return btnDOWN;
  37. if (adc_key_in < 500) return btnLEFT;
  38. if (adc_key_in < 800) return btnSELECT;
  39. return btnNONE; // Se nessun pulsante viene premuto...
  40. }
  41.  
  42. void setup()
  43. {
  44. Serial.begin(9600);
  45. pinMode(Backlight, OUTPUT);
  46. digitalWrite(Backlight, HIGH); // Accende retroilluminazione
  47. lcd.begin(16, 2);
  48.  
  49.  
  50. }
  51.  
  52. void mainloop() {
  53. if (lcd_key == btnSELECT) {
  54. stato = 6;
  55. }
  56. if (FIRST) {
  57. lcd.setCursor(0, 0);
  58. lcd.print("TEMP 1 ");lcd.print(t_min);
  59. lcd.setCursor(0, 1);
  60. lcd.print("fan %");
  61. FIRST = false;
  62. }
  63.  
  64. }
  65.  
  66.  
  67. void mainmenu1() {
  68. if (FIRST) {
  69. lcd.setCursor(0, 0);
  70. lcd.print("menu 1");
  71. FIRST = false;
  72. }
  73. }
  74.  
  75. void mainmenu2() {
  76. if (FIRST) {
  77. lcd.setCursor(0, 0);
  78. lcd.print("menu 2");
  79.  
  80. FIRST = false;
  81. }
  82. }
  83. void mainmenu3() {
  84. if (FIRST) {
  85. lcd.setCursor(0, 0);
  86. lcd.print("menu 3");
  87. lcd.setCursor(0, 1);
  88. lcd.print("DN:next");
  89. FIRST = false;
  90. }
  91.  
  92. }
  93. void mainmenuexit() {
  94. if (FIRST) {
  95. lcd.print("exit menu?");
  96. lcd.setCursor(0, 1);
  97. lcd.print("DN:next S:back");
  98. FIRST = false;
  99. }
  100.  
  101.  
  102. }
  103.  
  104.  
  105. void sub1() {
  106. if (FIRST) {
  107. lcd.print("sub1 x=");
  108. lcd.setCursor(7, 0);
  109. lcd.print(x);
  110. lcd.setCursor(0, 1);
  111.  
  112. FIRST = false;
  113. }
  114. }
  115. void sub2() {
  116.  
  117. //Serial.println(old_key);
  118. // static int conta;
  119. if ((lcd_key == 4) && (old_key == 1)) {
  120. Serial.println(old_key);
  121. old_key = 0;
  122. conta ++;
  123. }
  124. if (lcd_key != 4) {
  125. delay(250);
  126. old_key = 1;
  127. }
  128. if (conta == 0) { //alla 1 pressione select
  129. if (lcd_key == btnUP) { // aumento t min
  130. //Serial.print("conta "); Serial.println(conta );
  131. t_min = t_min + 0.5;
  132. lcd.setCursor(7, 1);
  133. lcd.print(t_min);
  134. }
  135. if (lcd_key == btnDOWN) { // diminuisco t min
  136. // Serial.print("conta "); Serial.println(conta );
  137. t_min = t_min - 0.5;
  138. lcd.setCursor(7, 1);
  139. lcd.print(t_min);
  140. }
  141. }
  142. if (conta == 1) { //alla 2 pressione select
  143. if (lcd_key == btnUP) { // aumento t min
  144. // Serial.print("conta "); Serial.println(conta );
  145. t_max = t_max + 0.5;
  146. lcd.setCursor(7, 0);
  147. lcd.print(t_max);
  148. }
  149. if (lcd_key == btnDOWN) { // diminuisco t min
  150. // Serial.print("conta "); Serial.println(conta );
  151. t_max = t_max - 0.5;
  152. lcd.setCursor(7, 0);
  153. lcd.print(t_max);
  154. }
  155. }
  156. if (conta == 2) {
  157. lcd.setCursor(0, 0);
  158. lcd.print("memorizzo ");
  159. lcd.setCursor(0, 1);
  160. lcd.print("L SAVE R BACK");
  161.  
  162. if (lcd_key == btnRIGHT) {
  163. conta = 0;
  164. stato =0;
  165. Serial.print("indietro ");
  166. }
  167. if (lcd_key == btnLEFT) {
  168. conta = 0;
  169. stato =0;
  170. Serial.print("salva ");
  171. }
  172.  
  173. }
  174. if (FIRST) {
  175. lcd.setCursor(0, 0);
  176. lcd.print("t max");
  177. lcd.setCursor(0, 1);
  178. lcd.print("t min");
  179. FIRST = false;
  180. }
  181.  
  182. if (lcd_key == btnRIGHT) { //ad ogni pressione
  183.  
  184. x ++;
  185. }
  186.  
  187. if (lcd_key == btnLEFT) {
  188. x --;
  189. }
  190.  
  191. }
  192.  
  193.  
  194.  
  195. void loop() {
  196. if (stato != old_stato) {
  197. Serial.print("stato "); Serial.println(stato );
  198. FIRST = true;
  199. lcd.clear();
  200. old_stato = stato;
  201. delay(100);
  202. }
  203. lcd_key = read_LCD_buttons(); // Fa partire la funzione che legge i bottoni
  204. if ((stato >= 0) && (stato <= 5)) {
  205. if (lcd_key != old_lcd_key) { // aumento variabile stato +1
  206.  
  207. if (lcd_key == btnUP) { //ad ogni pressione
  208. if (stato >= 5) {
  209. stato = 0;
  210. }
  211. else {
  212. stato ++;
  213. }
  214. }
  215. if (lcd_key == btnDOWN) {
  216. if (stato <= 0) {
  217. stato = 5;
  218. }
  219. else {
  220. stato --;
  221. }
  222. }
  223. old_lcd_key = lcd_key;
  224. }
  225. }
  226.  
  227. switch (stato) {
  228. case 0:
  229. mainloop();
  230. break;
  231. case 1:
  232. mainmenu1();
  233. break;
  234. case 2:
  235. mainmenu2();
  236. break;
  237. case 3:
  238. mainmenu3();
  239. break;
  240. case 4:
  241. mainmenuexit();
  242. break;
  243. case 5:
  244. sub1();
  245. break;
  246. case 6:
  247. sub2();
  248. break;
  249. }
  250.  
  251.  
  252. switch (lcd_key) // Switch da un valore all'altro in base ai pulsanti
  253. {
  254. case btnRIGHT:
  255. {
  256. // lcd.print("RIGHT ");
  257. break;
  258. }
  259. case btnLEFT:
  260. {
  261. //lcd.print("LEFT ");
  262. break;
  263. }
  264. case btnUP:
  265. {
  266. //stato++;
  267. //lcd.print("UP ");
  268. break;
  269. }
  270. case btnDOWN:
  271. {
  272. //lcd.print("DOWN ");
  273. //stato--;
  274. break;
  275. }
  276. case btnSELECT:
  277. {
  278. //lcd.print("SELECT");
  279. break;
  280. }
  281. case btnNONE:
  282. {
  283. //lcd.print("NONE ");
  284. break;
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement