Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************
- Questo programma testa i pulsanti della LCD Shield
- by Luca Pertile 11/03/2013
- ********************************************************/
- int conta = 0;
- int conta1 = 0;
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- float t_max = 0.0;
- float t_min = 0.0;
- bool FIRST = true;
- int x = 0;
- int stato = 0;
- int old_stato = 0;
- int lcd_key = 0;
- int adc_key_in = 0;
- int old_lcd_key = 0;
- int old_key = 0;
- #define btnRIGHT 0 // Definizione dei numeri associati ai pulsanti
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- #define Backlight 10
- // read the buttons
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // Legge il valore del convertitore ADC
- // I miei pulsanti danno questi valori: 0, 132, 321, 490, 725 (R,U,D,L,S)
- if (adc_key_in > 800) return btnNONE; // "None" per primo per velocizzare
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 200) return btnUP;
- if (adc_key_in < 400) return btnDOWN;
- if (adc_key_in < 500) return btnLEFT;
- if (adc_key_in < 800) return btnSELECT;
- return btnNONE; // Se nessun pulsante viene premuto...
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(Backlight, OUTPUT);
- digitalWrite(Backlight, HIGH); // Accende retroilluminazione
- lcd.begin(16, 2);
- }
- void mainloop() {
- if (lcd_key == btnSELECT) {
- stato = 6;
- }
- if (FIRST) {
- lcd.setCursor(0, 0);
- lcd.print("TEMP 1 ");lcd.print(t_min);
- lcd.setCursor(0, 1);
- lcd.print("fan %");
- FIRST = false;
- }
- }
- void mainmenu1() {
- if (FIRST) {
- lcd.setCursor(0, 0);
- lcd.print("menu 1");
- FIRST = false;
- }
- }
- void mainmenu2() {
- if (FIRST) {
- lcd.setCursor(0, 0);
- lcd.print("menu 2");
- FIRST = false;
- }
- }
- void mainmenu3() {
- if (FIRST) {
- lcd.setCursor(0, 0);
- lcd.print("menu 3");
- lcd.setCursor(0, 1);
- lcd.print("DN:next");
- FIRST = false;
- }
- }
- void mainmenuexit() {
- if (FIRST) {
- lcd.print("exit menu?");
- lcd.setCursor(0, 1);
- lcd.print("DN:next S:back");
- FIRST = false;
- }
- }
- void sub1() {
- if (FIRST) {
- lcd.print("sub1 x=");
- lcd.setCursor(7, 0);
- lcd.print(x);
- lcd.setCursor(0, 1);
- FIRST = false;
- }
- }
- void sub2() {
- //Serial.println(old_key);
- // static int conta;
- if ((lcd_key == 4) && (old_key == 1)) {
- Serial.println(old_key);
- old_key = 0;
- conta ++;
- }
- if (lcd_key != 4) {
- delay(250);
- old_key = 1;
- }
- if (conta == 0) { //alla 1 pressione select
- if (lcd_key == btnUP) { // aumento t min
- //Serial.print("conta "); Serial.println(conta );
- t_min = t_min + 0.5;
- lcd.setCursor(7, 1);
- lcd.print(t_min);
- }
- if (lcd_key == btnDOWN) { // diminuisco t min
- // Serial.print("conta "); Serial.println(conta );
- t_min = t_min - 0.5;
- lcd.setCursor(7, 1);
- lcd.print(t_min);
- }
- }
- if (conta == 1) { //alla 2 pressione select
- if (lcd_key == btnUP) { // aumento t min
- // Serial.print("conta "); Serial.println(conta );
- t_max = t_max + 0.5;
- lcd.setCursor(7, 0);
- lcd.print(t_max);
- }
- if (lcd_key == btnDOWN) { // diminuisco t min
- // Serial.print("conta "); Serial.println(conta );
- t_max = t_max - 0.5;
- lcd.setCursor(7, 0);
- lcd.print(t_max);
- }
- }
- if (conta == 2) {
- lcd.setCursor(0, 0);
- lcd.print("memorizzo ");
- lcd.setCursor(0, 1);
- lcd.print("L SAVE R BACK");
- if (lcd_key == btnRIGHT) {
- conta = 0;
- stato =0;
- Serial.print("indietro ");
- }
- if (lcd_key == btnLEFT) {
- conta = 0;
- stato =0;
- Serial.print("salva ");
- }
- }
- if (FIRST) {
- lcd.setCursor(0, 0);
- lcd.print("t max");
- lcd.setCursor(0, 1);
- lcd.print("t min");
- FIRST = false;
- }
- if (lcd_key == btnRIGHT) { //ad ogni pressione
- x ++;
- }
- if (lcd_key == btnLEFT) {
- x --;
- }
- }
- void loop() {
- if (stato != old_stato) {
- Serial.print("stato "); Serial.println(stato );
- FIRST = true;
- lcd.clear();
- old_stato = stato;
- delay(100);
- }
- lcd_key = read_LCD_buttons(); // Fa partire la funzione che legge i bottoni
- if ((stato >= 0) && (stato <= 5)) {
- if (lcd_key != old_lcd_key) { // aumento variabile stato +1
- if (lcd_key == btnUP) { //ad ogni pressione
- if (stato >= 5) {
- stato = 0;
- }
- else {
- stato ++;
- }
- }
- if (lcd_key == btnDOWN) {
- if (stato <= 0) {
- stato = 5;
- }
- else {
- stato --;
- }
- }
- old_lcd_key = lcd_key;
- }
- }
- switch (stato) {
- case 0:
- mainloop();
- break;
- case 1:
- mainmenu1();
- break;
- case 2:
- mainmenu2();
- break;
- case 3:
- mainmenu3();
- break;
- case 4:
- mainmenuexit();
- break;
- case 5:
- sub1();
- break;
- case 6:
- sub2();
- break;
- }
- switch (lcd_key) // Switch da un valore all'altro in base ai pulsanti
- {
- case btnRIGHT:
- {
- // lcd.print("RIGHT ");
- break;
- }
- case btnLEFT:
- {
- //lcd.print("LEFT ");
- break;
- }
- case btnUP:
- {
- //stato++;
- //lcd.print("UP ");
- break;
- }
- case btnDOWN:
- {
- //lcd.print("DOWN ");
- //stato--;
- break;
- }
- case btnSELECT:
- {
- //lcd.print("SELECT");
- break;
- }
- case btnNONE:
- {
- //lcd.print("NONE ");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement