

Programme simple pour utiliser un afficheur LCD et utilisation des boutons associés

Matériel
- Ecran LCD 16×2 – RB-ITE-161
- Arduino UNO compatible – RB-DFR-189
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#include // Include the LCD library #include // Button #define btnRIGHT 1 #define btnUP 2 #define btnDOWN 4 #define btnLEFT 8 #define btnSELECT 16 #define btnNONE 32 // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Return the first pressed button found byte read_LCD_buttons() { // read the value from the sensor int adc_key_in = analogRead(A0); if (adc_key_in > 1000) return btnNONE; if (adc_key_in < 50) return btnRIGHT; if (adc_key_in < 250) return btnUP; if (adc_key_in < 450) return btnDOWN; if (adc_key_in < 650) return btnLEFT; if (adc_key_in < 850) return btnSELECT; return btnNONE; // when all others fail, return this. } // Return the number of digits in an integer byte countDigits(int num) { byte count=0; while(num) { num = num / 10; count += 1; } return count; } // Display full screen of text (2 rows x 16 characters) void displayScreen(char row1[], char row2[]) { lcd.clear(); lcd.setCursor(0,0); lcd.print(row1); lcd.setCursor(0,1); lcd.print(row2); Serial.println(""); Serial.println(row1); Serial.println(row2); } // Display to wait for select and then do so void waitSelect(bool disp = false) { if(disp) { lcd.setCursor(0,1); lcd.print("< press select >"); Serial.println("< press select >"); } while(read_LCD_buttons() != btnSELECT){}; } // Displays text on screen and an ADC value at chosen position from selected pin. Waits for user input (select button by default) int getValueADC(char row1[], char row2[], byte pos, byte pin, byte endButton) { int value = 0; int lastValue = -1; byte buttons = btnNONE; displayScreen(row1, row2); delay(1500); while(buttons != endButton) { // Check inputs buttons = read_LCD_buttons(); // Update readout from analog port value = analogRead(pin); // Update display if(value != lastValue) { lcd.setCursor(pos, 1); lcd.print(" "); lcd.setCursor(pos, 1); lcd.print(value); Serial.println(""); Serial.println(row1); Serial.print(row2); Serial.print(" "); Serial.println(value); lastValue = value; delay(350); } }; // Return the last measured ADC value return (value); } // Displays text on screen and a changeable value at chosen position. User input can change the value (left/right/up/down) and press select to accept it. int getValueInRange(char row1[], char row2[], byte pos, int valueDefault, int valueMin, int valueMax, int valueStep, int valueStepLarge) { int value = valueDefault; int lastValue = -1; int buttons = btnNONE; displayScreen(row1, row2); delay(1500); while(buttons != btnSELECT) { // Check inputs buttons = read_LCD_buttons(); switch(buttons) { case btnUP: value += valueStep; if(value > valueMax) value = valueMax; break; case btnRIGHT: value += valueStepLarge; if(value > valueMax) value = valueMax; break; case btnDOWN: value -= valueStep; if(value < valueMin) value = valueMin; break; case btnLEFT: value -= valueStepLarge; if(value < valueMin) value = valueMin; break; } // Update display if(value != lastValue) { lcd.setCursor(pos, 1); lcd.print(" "); lcd.setCursor(pos, 1); lcd.print(value); Serial.println(""); Serial.println(row1); Serial.print(row2); Serial.print(" "); Serial.println(value); lastValue = value; delay(350); } }; // Return the last selected value return (value); } // Timing management long display_time_step = 1000; long display_time = 0; int angle = 45; void setup() { // Initialize serial communication Serial.begin(115200); // Initialize LCD screen lcd.begin(16, 2); // Intro screen displayScreen("Val Technologie", "Pipe Bender"); delay(2500); } void loop() { //Demander angle angle = getValueInRange("Definir Angle", "Angle:", 7, angle, 1,180,1,5); // Up/down screen displayScreen("Pipe Bender", "Pliage en cours"); delay(1000); // Display Etiquette Angle lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(0, 1); lcd.print("ANGLE:"); // Display Angle for (int x = 0; x < angle; x++) { lcd.setCursor(13, 1); lcd.print(" "); lcd.setCursor(13, 1); lcd.print(x); delay(50); } // Up/down screen displayScreen("Pipe Bender", "Fin du Pliage"); delay(1000); } |