Car Controller Dashboard with LCD
If you want to build controller dashboard, you can do it by using just encoder with button!
Components and supplies
1
Solid State Relay Controller 4-Channel + 8 Channel ADC ProXR Lite
1
Rotary Encoder with Push-Button
1
Arduino UNO
Project description
Code
Arduino for Niva - Without Timer Module
arduino
I will go on this way. Timer module takes up space and pins and i realized my stereo has a clock :D
1#include "Timer.h" 2Timer t; 3unsigned long timeout; 4unsigned long timeoutCounter; 5 6#include <Wire.h> 7#include <LiquidCrystal_I2C.h> 8LiquidCrystal_I2C lcd(0x27, 16, 2); 9 10#include <dht11.h> 11#define DHT11PIN 1 12dht11 DHT11; 13 14#include <EEPROM.h> 15int storePwm = 0; 16 17byte encoderPinA = 2; // right (labeled DT on our decoder, yellow wire) 18byte encoderPinB = 3; // left (labeled CLK on our decoder, green wire) 19volatile unsigned int encoderVal = 50; // a counter for the dial 20unsigned int lastReportedPos = 1; // change management 21static boolean rotating = false; // debounce management 22boolean A_set = false; 23boolean B_set = false; 24 25byte clk = 4; 26int encoderValOld; 27 28byte menuCount = 0; 29byte menuCountLim = 6; 30byte menuCountOld; 31 32long buttonTimer = 0; 33long longPressTime = 500; 34boolean buttonActive = false; 35boolean longPressActive = false; 36 37byte state1 = 0; 38bool state2 = false; 39bool state3 = false; 40byte state4 = 0; 41bool state5 = false; 42bool state6 = false; 43bool state7 = false; 44 45byte val1 = 30; 46int pwmVal1; 47 48byte acSetTemp = 23; 49int temp; 50int hum; 51 52byte dPin1 = 6; 53byte dPin2 = 7; 54byte dPin3 = 8; 55byte dPin4 = 9; 56byte dPin5 = 10; 57byte dPin6 = 11; 58byte dPin7 = 12; 59byte dPin8 = 13; 60 61byte pwmPin1 = 5; 62 63int ldrPin = A6; 64int lightsOnLim = 400; 65int lightsOffLim = 600; 66bool lights = false; 67 68bool up = false; 69bool down = false; 70 71int doorPin = A0; 72bool doorState = false; 73 74void setup() { 75 pinMode(encoderPinA, INPUT_PULLUP); 76 pinMode(encoderPinB, INPUT_PULLUP); 77 78 attachInterrupt(0, doEncoderA, CHANGE); 79 attachInterrupt(1, doEncoderB, CHANGE); 80 81 t.every(30000, tempCheck); 82 t.every(5000, lightCheck); 83 t.every(100, doorCheck); 84 85 pinMode(clk, INPUT_PULLUP); 86 pinMode (doorPin, INPUT_PULLUP); 87 88 pinMode (dPin1, OUTPUT); 89 pinMode (dPin2, OUTPUT); 90 pinMode (dPin3, OUTPUT); 91 pinMode (dPin4, OUTPUT); 92 pinMode (dPin5, OUTPUT); 93 pinMode (dPin6, OUTPUT); 94 pinMode (dPin7, OUTPUT); 95 pinMode (dPin8, OUTPUT); 96 97 pinMode(pwmPin1, OUTPUT); 98 99 lcd.begin(); 100 lcd.clear(); 101 lcd.setCursor(3, 0); 102 lcd.print("NIVA 1600"); 103 delay(300); 104 for (int x = 0; x < 16; x++) { 105 106 lcd.setCursor(x, 1); 107 lcd.print(">"); 108 delay(50); 109 110 if (digitalRead(4) == LOW) { 111 break; 112 } 113 } 114 for (int x = 0; x < 16; x++) { 115 lcd.setCursor(x, 1); 116 lcd.print(" "); 117 delay(50); 118 119 if (digitalRead(4) == LOW) { 120 break; 121 } 122 } 123 delay(300); 124 lcd.clear(); 125 lcd.backlight(); 126 timeout = timeoutCounter + 30; 127 tempCheck(); 128 val1 = EEPROM.read(storePwm); 129 encoderVal = val1; 130} 131void loop() { 132 rotating = true; 133 up = false; 134 down = false; 135 136 buttonCheck(); 137 menuScreen(); 138 screenClean(); 139 darkMode(); 140 t.update(); 141 pinControl(); 142 timeoutCounter = (millis() / 1000); 143} 144 145void menuScreen() { 146 if (menuCount == 0) { 147 148 lcd.setCursor(0, 0); 149 lcd.print(temp); 150 lcd.setCursor(2, 0); 151 lcd.print((char) 223); 152 153 lcd.setCursor(3, 0); 154 lcd.print(hum); 155 lcd.setCursor(5, 0); 156 lcd.print("%"); 157 158 lcd.setCursor(7, 0); 159 lcd.print("-"); 160 161 lcd.setCursor(0, 1); 162 163 lcd.print("L"); 164 lcd.setCursor(1, 1); 165 lcd.print(val1); 166 167 val1 = encoderVal; 168 169 if (state5 == true) { 170 lcd.setCursor(4, 1); 171 lcd.print("AC:"); 172 lcd.setCursor(7, 1); 173 lcd.print(acSetTemp); 174 } else { 175 lcd.setCursor(4, 1); 176 lcd.print("AC:"); 177 lcd.setCursor(7, 1); 178 lcd.print("Off"); 179 } 180 181 lcd.setCursor(11, 1); 182 lcd.print("F:"); 183 lcd.setCursor(13, 1); 184 if (state1 == 0) { 185 lcd.print("Oto"); 186 } else if (state1 == 1) { 187 lcd.print("Off"); 188 } else if (state1 == 2) { 189 lcd.print("On "); 190 } 191 192 } 193 if (menuCount == 1) { 194 lcd.setCursor(0, 0); 195 lcd.print("1 Far"); 196 lcd.setCursor(0, 1); 197 if (state1 == 0) { 198 lcd.print("> Otomatik"); 199 } else if (state1 == 1) { 200 lcd.print("> Kapali "); 201 } else if (state1 == 2) { 202 lcd.print("> Acik "); 203 } 204 } 205 if (menuCount == 2) { 206 lcd.setCursor(0, 0); 207 lcd.print("2 Priz / USB"); 208 lcd.setCursor(0, 1); 209 if (state2 == true) { 210 lcd.print("> Acik "); 211 } else { 212 lcd.print("> Kapali"); 213 } 214 } 215 if (menuCount == 3) { 216 lcd.setCursor(0, 0); 217 lcd.print("3 Sis Farlari"); 218 lcd.setCursor(0, 1); 219 if (state3 == true) { 220 lcd.print("> Acik "); 221 } else { 222 lcd.print("> Kapali"); 223 } 224 } 225 if (menuCount == 4) { 226 lcd.setCursor(0, 0); 227 lcd.print("4 Fan"); 228 lcd.setCursor(0, 1); 229 if (state4 == 0) { 230 lcd.print("> Kapali"); 231 } else if (state4 == 1) { 232 lcd.print("> Hafif "); 233 } else if (state4 == 2) { 234 lcd.print("> Yuksek"); 235 } 236 } 237 if (menuCount == 5) { 238 lcd.setCursor(0, 0); 239 lcd.print("5 Klima"); 240 lcd.setCursor(0, 1); 241 if (state5 == true) { 242 lcd.print("> Acik "); 243 } else { 244 lcd.print("> Kapali"); 245 } 246 247 acSetTemp = encoderVal; 248 if (acSetTemp < 16) { 249 encoderVal = 16; 250 } 251 252 if (acSetTemp > 25) { 253 encoderVal = 25; 254 } 255 256 lcd.setCursor(10, 1); 257 lcd.print(acSetTemp); //Dummy val for temp 258 lcd.setCursor(12, 1); 259 lcd.print((char) 223); 260 261 } 262 if (menuCount == 6) { 263 lcd.setCursor(0, 0); 264 lcd.print("6 Yagmur Sensoru"); 265 lcd.setCursor(0, 1); 266 if (state6 == 0) { 267 lcd.print("> Kapali"); 268 } else { 269 lcd.print("> Acik "); 270 } 271 } 272} 273void pinControl() { 274 //---Channel 1 275 if (state1 == 0) { 276 if (lights == true) { 277 digitalWrite(dPin1, HIGH); 278 } else { 279 digitalWrite(dPin1, LOW); 280 } 281 } else if (state1 == 1) { 282 digitalWrite(dPin1, LOW); 283 } else if (state1 == 2) { 284 digitalWrite(dPin1, HIGH); 285 } 286 //---Channel 2 287 if (state2 == true) { 288 digitalWrite(dPin2, HIGH); 289 } else { 290 digitalWrite(dPin2, LOW); 291 } 292 //---Channel 3 293 if (state3 == true) { 294 digitalWrite(dPin3, HIGH); 295 } else { 296 digitalWrite(dPin3, LOW); 297 } 298 //---Channel 4 299 if (state4 == 0) { 300 digitalWrite(dPin4, LOW); 301 digitalWrite(dPin5, LOW); 302 } else if (state4 == 1) { 303 digitalWrite(dPin4, HIGH); 304 digitalWrite(dPin5, LOW); 305 } else if (state4 == 2) { 306 digitalWrite(dPin5, HIGH); 307 digitalWrite(dPin4, LOW); 308 } 309 //---Channel 5 310 if (state5 == true && acSetTemp < temp) { 311 digitalWrite(dPin6, HIGH); 312 } else { 313 digitalWrite(dPin6, LOW); 314 } 315 316 //---Channel 6 317 if (state6 == true) { 318 //digitalWrite(dPin7, HIGH); 319 } else { 320 //digitalWrite(dPin7, LOW); 321 } 322 323 pwmVal1 = map(val1, 0, 100, 0, 255); 324 analogWrite(pwmPin1, pwmVal1); 325} 326 327void lightCheck() { 328 if (analogRead(ldrPin) < lightsOnLim) { 329 lights = true; 330 } else if (analogRead(ldrPin) > lightsOffLim) { 331 lights = false; 332 } 333} 334 335void doorCheck() { 336 if (digitalRead(doorPin) == HIGH) { 337 if (val1 < 70) { 338 encoderVal++; 339 } 340 doorState = false; 341 } else { 342 byte oldVal = EEPROM.read(storePwm); 343 if (oldVal < val1) { 344 encoderVal--; 345 } 346 doorState = true; 347 } 348} 349 350void darkMode() { 351 if (timeoutCounter > timeout) { 352 lcd.noBacklight(); 353 menuCount = 0; 354 } else { 355 lcd.backlight(); 356 } 357} 358void screenClean() { 359 if (menuCount != menuCountOld) { 360 lcd.clear(); 361 menuCountOld = menuCount; 362 } 363 if (encoderValOld != encoderVal) { 364 lcd.clear(); 365 encoderValOld = encoderVal; 366 } 367} 368void stateCheck() { 369 if (longPressActive == true) { 370 371 if (menuCount == 0) { 372 EEPROM.write(storePwm, val1); 373 delay(10); 374 lcd.noBacklight(); 375 delay(50); 376 lcd.backlight(); 377 } 378 379 if (menuCount == 1) { 380 if (state1 == 0) { 381 state1 = 1; 382 } else if (state1 == 1) { 383 state1 = 2; 384 } else if (state1 == 2) { 385 state1 = 0; 386 } 387 } 388 389 if (menuCount == 2) { 390 state2 = !state2; 391 } 392 393 if (menuCount == 3) { 394 state3 = !state3; 395 } 396 397 if (menuCount == 4) { 398 if (state4 == 0) { 399 state4 = 1; 400 } else if (state4 == 1) { 401 state4 = 2; 402 } else if (state4 == 2) { 403 state4 = 0; 404 } 405 } 406 407 if (menuCount == 5) { 408 state5 = !state5; 409 } 410 if (menuCount == 6) { 411 state6 = !state6; 412 } 413 414 } 415} 416void buttonCheck() { 417 if (digitalRead(clk) == LOW) { 418 if (buttonActive == false) { 419 buttonActive = true; 420 buttonTimer = millis(); 421 } 422 if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { 423 longPressActive = true; 424 //Uzun basma 425 stateCheck(); 426 } 427 } else { 428 if (buttonActive == true) { 429 if (longPressActive == true) { 430 longPressActive = false; 431 } else { 432 433 //kısa basma 434 435 timeout = timeoutCounter + 20; 436 if (menuCount < menuCountLim) { 437 menuCount++; 438 439 } else { 440 menuCount = 0; 441 encoderVal = val1; 442 } 443 444 } 445 buttonActive = false; 446 } 447 } 448} 449void tempCheck() { 450 451 int chk = DHT11.read(DHT11PIN); 452 453 temp = DHT11.temperature; 454 hum = DHT11.humidity; 455 456} 457void doEncoderA() { 458 // debounce 459 if ( rotating ) delay (1); // wait a little until the bouncing is done 460 // Test transition, did things really change? 461 if ( digitalRead(encoderPinA) != A_set ) { // debounce once more 462 A_set = !A_set; 463 // adjust counter + if A leads B 464 if ( A_set && !B_set ) 465 466 if (encoderVal < 100) { 467 encoderVal += 1; 468 } 469 470 timeout = timeoutCounter + 20; 471 up = true; 472 down = false; 473 rotating = false; // no more debouncing until loop() hits again 474 } 475} 476void doEncoderB() { 477 if ( rotating ) delay (1); 478 if ( digitalRead(encoderPinB) != B_set ) { 479 B_set = !B_set; 480 // adjust counter – 1 if B leads A 481 if ( B_set && !A_set ) 482 483 if (encoderVal > 0) { 484 encoderVal -= 1; 485 } 486 487 timeout = timeoutCounter + 20; 488 down = true; 489 up = false; 490 rotating = false; 491 } 492}
Arduino for Niva - Without Timer Module
arduino
I will go on this way. Timer module takes up space and pins and i realized my stereo has a clock :D
1#include "Timer.h" 2Timer t; 3unsigned long timeout; 4unsigned long timeoutCounter; 5 6#include <Wire.h> 7#include <LiquidCrystal_I2C.h> 8LiquidCrystal_I2C lcd(0x27, 16, 2); 9 10#include <dht11.h> 11#define DHT11PIN 1 12dht11 DHT11; 13 14#include <EEPROM.h> 15int storePwm = 0; 16 17byte encoderPinA = 2; // right (labeled DT on our decoder, yellow wire) 18byte encoderPinB = 3; // left (labeled CLK on our decoder, green wire) 19volatile unsigned int encoderVal = 50; // a counter for the dial 20unsigned int lastReportedPos = 1; // change management 21static boolean rotating = false; // debounce management 22boolean A_set = false; 23boolean B_set = false; 24 25byte clk = 4; 26int encoderValOld; 27 28byte menuCount = 0; 29byte menuCountLim = 6; 30byte menuCountOld; 31 32long buttonTimer = 0; 33long longPressTime = 500; 34boolean buttonActive = false; 35boolean longPressActive = false; 36 37byte state1 = 0; 38bool state2 = false; 39bool state3 = false; 40byte state4 = 0; 41bool state5 = false; 42bool state6 = false; 43bool state7 = false; 44 45byte val1 = 30; 46int pwmVal1; 47 48byte acSetTemp = 23; 49int temp; 50int hum; 51 52byte dPin1 = 6; 53byte dPin2 = 7; 54byte dPin3 = 8; 55byte dPin4 = 9; 56byte dPin5 = 10; 57byte dPin6 = 11; 58byte dPin7 = 12; 59byte dPin8 = 13; 60 61byte pwmPin1 = 5; 62 63int ldrPin = A6; 64int lightsOnLim = 400; 65int lightsOffLim = 600; 66bool lights = false; 67 68bool up = false; 69bool down = false; 70 71int doorPin = A0; 72bool doorState = false; 73 74void setup() { 75 pinMode(encoderPinA, INPUT_PULLUP); 76 pinMode(encoderPinB, INPUT_PULLUP); 77 78 attachInterrupt(0, doEncoderA, CHANGE); 79 attachInterrupt(1, doEncoderB, CHANGE); 80 81 t.every(30000, tempCheck); 82 t.every(5000, lightCheck); 83 t.every(100, doorCheck); 84 85 pinMode(clk, INPUT_PULLUP); 86 pinMode (doorPin, INPUT_PULLUP); 87 88 pinMode (dPin1, OUTPUT); 89 pinMode (dPin2, OUTPUT); 90 pinMode (dPin3, OUTPUT); 91 pinMode (dPin4, OUTPUT); 92 pinMode (dPin5, OUTPUT); 93 pinMode (dPin6, OUTPUT); 94 pinMode (dPin7, OUTPUT); 95 pinMode (dPin8, OUTPUT); 96 97 pinMode(pwmPin1, OUTPUT); 98 99 lcd.begin(); 100 lcd.clear(); 101 lcd.setCursor(3, 0); 102 lcd.print("NIVA 1600"); 103 delay(300); 104 for (int x = 0; x < 16; x++) { 105 106 lcd.setCursor(x, 1); 107 lcd.print(">"); 108 delay(50); 109 110 if (digitalRead(4) == LOW) { 111 break; 112 } 113 } 114 for (int x = 0; x < 16; x++) { 115 lcd.setCursor(x, 1); 116 lcd.print(" "); 117 delay(50); 118 119 if (digitalRead(4) == LOW) { 120 break; 121 } 122 } 123 delay(300); 124 lcd.clear(); 125 lcd.backlight(); 126 timeout = timeoutCounter + 30; 127 tempCheck(); 128 val1 = EEPROM.read(storePwm); 129 encoderVal = val1; 130} 131void loop() { 132 rotating = true; 133 up = false; 134 down = false; 135 136 buttonCheck(); 137 menuScreen(); 138 screenClean(); 139 darkMode(); 140 t.update(); 141 pinControl(); 142 timeoutCounter = (millis() / 1000); 143} 144 145void menuScreen() { 146 if (menuCount == 0) { 147 148 lcd.setCursor(0, 0); 149 lcd.print(temp); 150 lcd.setCursor(2, 0); 151 lcd.print((char) 223); 152 153 lcd.setCursor(3, 0); 154 lcd.print(hum); 155 lcd.setCursor(5, 0); 156 lcd.print("%"); 157 158 lcd.setCursor(7, 0); 159 lcd.print("-"); 160 161 lcd.setCursor(0, 1); 162 163 lcd.print("L"); 164 lcd.setCursor(1, 1); 165 lcd.print(val1); 166 167 val1 = encoderVal; 168 169 if (state5 == true) { 170 lcd.setCursor(4, 1); 171 lcd.print("AC:"); 172 lcd.setCursor(7, 1); 173 lcd.print(acSetTemp); 174 } else { 175 lcd.setCursor(4, 1); 176 lcd.print("AC:"); 177 lcd.setCursor(7, 1); 178 lcd.print("Off"); 179 } 180 181 lcd.setCursor(11, 1); 182 lcd.print("F:"); 183 lcd.setCursor(13, 1); 184 if (state1 == 0) { 185 lcd.print("Oto"); 186 } else if (state1 == 1) { 187 lcd.print("Off"); 188 } else if (state1 == 2) { 189 lcd.print("On "); 190 } 191 192 } 193 if (menuCount == 1) { 194 lcd.setCursor(0, 0); 195 lcd.print("1 Far"); 196 lcd.setCursor(0, 1); 197 if (state1 == 0) { 198 lcd.print("> Otomatik"); 199 } else if (state1 == 1) { 200 lcd.print("> Kapali "); 201 } else if (state1 == 2) { 202 lcd.print("> Acik "); 203 } 204 } 205 if (menuCount == 2) { 206 lcd.setCursor(0, 0); 207 lcd.print("2 Priz / USB"); 208 lcd.setCursor(0, 1); 209 if (state2 == true) { 210 lcd.print("> Acik "); 211 } else { 212 lcd.print("> Kapali"); 213 } 214 } 215 if (menuCount == 3) { 216 lcd.setCursor(0, 0); 217 lcd.print("3 Sis Farlari"); 218 lcd.setCursor(0, 1); 219 if (state3 == true) { 220 lcd.print("> Acik "); 221 } else { 222 lcd.print("> Kapali"); 223 } 224 } 225 if (menuCount == 4) { 226 lcd.setCursor(0, 0); 227 lcd.print("4 Fan"); 228 lcd.setCursor(0, 1); 229 if (state4 == 0) { 230 lcd.print("> Kapali"); 231 } else if (state4 == 1) { 232 lcd.print("> Hafif "); 233 } else if (state4 == 2) { 234 lcd.print("> Yuksek"); 235 } 236 } 237 if (menuCount == 5) { 238 lcd.setCursor(0, 0); 239 lcd.print("5 Klima"); 240 lcd.setCursor(0, 1); 241 if (state5 == true) { 242 lcd.print("> Acik "); 243 } else { 244 lcd.print("> Kapali"); 245 } 246 247 acSetTemp = encoderVal; 248 if (acSetTemp < 16) { 249 encoderVal = 16; 250 } 251 252 if (acSetTemp > 25) { 253 encoderVal = 25; 254 } 255 256 lcd.setCursor(10, 1); 257 lcd.print(acSetTemp); //Dummy val for temp 258 lcd.setCursor(12, 1); 259 lcd.print((char) 223); 260 261 } 262 if (menuCount == 6) { 263 lcd.setCursor(0, 0); 264 lcd.print("6 Yagmur Sensoru"); 265 lcd.setCursor(0, 1); 266 if (state6 == 0) { 267 lcd.print("> Kapali"); 268 } else { 269 lcd.print("> Acik "); 270 } 271 } 272} 273void pinControl() { 274 //---Channel 1 275 if (state1 == 0) { 276 if (lights == true) { 277 digitalWrite(dPin1, HIGH); 278 } else { 279 digitalWrite(dPin1, LOW); 280 } 281 } else if (state1 == 1) { 282 digitalWrite(dPin1, LOW); 283 } else if (state1 == 2) { 284 digitalWrite(dPin1, HIGH); 285 } 286 //---Channel 2 287 if (state2 == true) { 288 digitalWrite(dPin2, HIGH); 289 } else { 290 digitalWrite(dPin2, LOW); 291 } 292 //---Channel 3 293 if (state3 == true) { 294 digitalWrite(dPin3, HIGH); 295 } else { 296 digitalWrite(dPin3, LOW); 297 } 298 //---Channel 4 299 if (state4 == 0) { 300 digitalWrite(dPin4, LOW); 301 digitalWrite(dPin5, LOW); 302 } else if (state4 == 1) { 303 digitalWrite(dPin4, HIGH); 304 digitalWrite(dPin5, LOW); 305 } else if (state4 == 2) { 306 digitalWrite(dPin5, HIGH); 307 digitalWrite(dPin4, LOW); 308 } 309 //---Channel 5 310 if (state5 == true && acSetTemp < temp) { 311 digitalWrite(dPin6, HIGH); 312 } else { 313 digitalWrite(dPin6, LOW); 314 } 315 316 //---Channel 6 317 if (state6 == true) { 318 //digitalWrite(dPin7, HIGH); 319 } else { 320 //digitalWrite(dPin7, LOW); 321 } 322 323 pwmVal1 = map(val1, 0, 100, 0, 255); 324 analogWrite(pwmPin1, pwmVal1); 325} 326 327void lightCheck() { 328 if (analogRead(ldrPin) < lightsOnLim) { 329 lights = true; 330 } else if (analogRead(ldrPin) > lightsOffLim) { 331 lights = false; 332 } 333} 334 335void doorCheck() { 336 if (digitalRead(doorPin) == HIGH) { 337 if (val1 < 70) { 338 encoderVal++; 339 } 340 doorState = false; 341 } else { 342 byte oldVal = EEPROM.read(storePwm); 343 if (oldVal < val1) { 344 encoderVal--; 345 } 346 doorState = true; 347 } 348} 349 350void darkMode() { 351 if (timeoutCounter > timeout) { 352 lcd.noBacklight(); 353 menuCount = 0; 354 } else { 355 lcd.backlight(); 356 } 357} 358void screenClean() { 359 if (menuCount != menuCountOld) { 360 lcd.clear(); 361 menuCountOld = menuCount; 362 } 363 if (encoderValOld != encoderVal) { 364 lcd.clear(); 365 encoderValOld = encoderVal; 366 } 367} 368void stateCheck() { 369 if (longPressActive == true) { 370 371 if (menuCount == 0) { 372 EEPROM.write(storePwm, val1); 373 delay(10); 374 lcd.noBacklight(); 375 delay(50); 376 lcd.backlight(); 377 } 378 379 if (menuCount == 1) { 380 if (state1 == 0) { 381 state1 = 1; 382 } else if (state1 == 1) { 383 state1 = 2; 384 } else if (state1 == 2) { 385 state1 = 0; 386 } 387 } 388 389 if (menuCount == 2) { 390 state2 = !state2; 391 } 392 393 if (menuCount == 3) { 394 state3 = !state3; 395 } 396 397 if (menuCount == 4) { 398 if (state4 == 0) { 399 state4 = 1; 400 } else if (state4 == 1) { 401 state4 = 2; 402 } else if (state4 == 2) { 403 state4 = 0; 404 } 405 } 406 407 if (menuCount == 5) { 408 state5 = !state5; 409 } 410 if (menuCount == 6) { 411 state6 = !state6; 412 } 413 414 } 415} 416void buttonCheck() { 417 if (digitalRead(clk) == LOW) { 418 if (buttonActive == false) { 419 buttonActive = true; 420 buttonTimer = millis(); 421 } 422 if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { 423 longPressActive = true; 424 //Uzun basma 425 stateCheck(); 426 } 427 } else { 428 if (buttonActive == true) { 429 if (longPressActive == true) { 430 longPressActive = false; 431 } else { 432 433 //kısa basma 434 435 timeout = timeoutCounter + 20; 436 if (menuCount < menuCountLim) { 437 menuCount++; 438 439 } else { 440 menuCount = 0; 441 encoderVal = val1; 442 } 443 444 } 445 buttonActive = false; 446 } 447 } 448} 449void tempCheck() { 450 451 int chk = DHT11.read(DHT11PIN); 452 453 temp = DHT11.temperature; 454 hum = DHT11.humidity; 455 456} 457void doEncoderA() { 458 // debounce 459 if ( rotating ) delay (1); // wait a little until the bouncing is done 460 // Test transition, did things really change? 461 if ( digitalRead(encoderPinA) != A_set ) { // debounce once more 462 A_set = !A_set; 463 // adjust counter + if A leads B 464 if ( A_set && !B_set ) 465 466 if (encoderVal < 100) { 467 encoderVal += 1; 468 } 469 470 timeout = timeoutCounter + 20; 471 up = true; 472 down = false; 473 rotating = false; // no more debouncing until loop() hits again 474 } 475} 476void doEncoderB() { 477 if ( rotating ) delay (1); 478 if ( digitalRead(encoderPinB) != B_set ) { 479 B_set = !B_set; 480 // adjust counter – 1 if B leads A 481 if ( B_set && !A_set ) 482 483 if (encoderVal > 0) { 484 encoderVal -= 1; 485 } 486 487 timeout = timeoutCounter + 20; 488 down = true; 489 up = false; 490 rotating = false; 491 } 492}
Arduino for Niva
arduino
1#include "Timer.h" 2Timer t; 3unsigned long timeout; 4unsigned long timeoutCounter; 5 6unsigned long wiperTimeout = 30; 7unsigned long wiperCounter; 8unsigned long wiperInterval = 30; 9byte onTime = 1; 10 11#include <Wire.h> 12#include <LiquidCrystal_I2C.h> 13LiquidCrystal_I2C lcd(0x27, 16, 2); 14 15#include <dht11.h> 16#define DHT11PIN 1 17dht11 DHT11; 18 19#include <EEPROM.h> 20int storePwm = 0; 21 22byte encoderPinA = 2; // right (labeled DT on our decoder, yellow wire) 23byte encoderPinB = 3; // left (labeled CLK on our decoder, green wire) 24volatile unsigned int encoderVal = 50; // a counter for the dial 25unsigned int lastReportedPos = 1; // change management 26static boolean rotating = false; // debounce management 27boolean A_set = false; 28boolean B_set = false; 29 30byte clk = 4; 31int encoderValOld; 32 33byte menuCount = 0; 34byte menuCountLim = 5; 35byte menuCountOld; 36 37long buttonTimer = 0; 38long longPressTime = 500; 39boolean buttonActive = false; 40boolean longPressActive = false; 41 42byte state1 = 0; 43bool state2 = false; 44bool state3 = false; 45byte state4 = 0; 46bool state5 = false; 47bool state6 = false; 48bool state7 = false; 49 50byte val1 = 30; 51int pwmVal1; 52 53byte acSetTemp = 23; 54int temp; 55int hum; 56 57byte dPin1 = 6; 58byte dPin2 = 7; 59byte dPin3 = 8; 60byte dPin4 = 9; 61byte dPin5 = 10; 62byte dPin6 = 11; 63byte dPin7 = 12; 64byte dPin8 = 13; 65 66byte pwmPin1 = 5; 67 68int ldrPin = A6; 69int lightsOnLim = 400; 70int lightsOffLim = 600; 71bool lights = false; 72 73int doorPin = A0; 74bool doorState = false; 75 76void setup() { 77 pinMode(encoderPinA, INPUT_PULLUP); 78 pinMode(encoderPinB, INPUT_PULLUP); 79 80 attachInterrupt(0, doEncoderA, CHANGE); 81 attachInterrupt(1, doEncoderB, CHANGE); 82 83 t.every(30000, tempCheck); 84 t.every(3000, lightCheck); 85 t.every(100, doorCheck); 86 87 pinMode(clk, INPUT_PULLUP); 88 pinMode (doorPin, INPUT_PULLUP); 89 90 pinMode (dPin1, OUTPUT); 91 pinMode (dPin2, OUTPUT); 92 pinMode (dPin3, OUTPUT); 93 pinMode (dPin4, OUTPUT); 94 pinMode (dPin5, OUTPUT); 95 pinMode (dPin6, OUTPUT); 96 pinMode (dPin7, OUTPUT); 97 pinMode (dPin8, OUTPUT); 98 99 digitalWrite (dPin1, HIGH); 100 digitalWrite (dPin2, HIGH); 101 digitalWrite (dPin3, HIGH); 102 digitalWrite (dPin4, HIGH); 103 digitalWrite (dPin5, HIGH); 104 digitalWrite (dPin6, HIGH); 105 digitalWrite (dPin7, HIGH); 106 digitalWrite (dPin8, HIGH); 107 108 pinMode(pwmPin1, OUTPUT); 109 digitalWrite(pwmPin1, 200); 110 111 lcd.begin(); 112 lcd.clear(); 113 lcd.setCursor(3, 0); 114 lcd.print("NIVA 1600"); 115 delay(300); 116 for (int x = 0; x < 16; x++) { 117 118 lcd.setCursor(x, 1); 119 lcd.print(">"); 120 delay(30); 121 122 if (digitalRead(4) == LOW) { 123 break; 124 } 125 } 126 for (int x = 0; x < 16; x++) { 127 lcd.setCursor(x, 1); 128 lcd.print(" "); 129 delay(30); 130 131 if (digitalRead(4) == LOW) { 132 break; 133 } 134 } 135 delay(300); 136 lcd.clear(); 137 lcd.backlight(); 138 timeout = timeoutCounter + 20; 139 tempCheck(); 140 val1 = EEPROM.read(storePwm); 141 encoderVal = val1; 142} 143void loop() { 144 rotating = true; 145 buttonCheck(); 146 menuScreen(); 147 screenClean(); 148 darkMode(); 149 t.update(); 150 pinControl(); 151 timeoutCounter = (millis() / 1000); 152 wiperCounter = (millis() / 1000); 153} 154void menuScreen() { 155 if (menuCount == 0) { //Ana ekran 156 157 lcd.setCursor(0, 0); 158 lcd.print(temp); 159 lcd.setCursor(2, 0); 160 lcd.print((char) 223); 161 162 lcd.setCursor(3, 0); 163 lcd.print(hum); 164 lcd.setCursor(5, 0); 165 lcd.print("%"); 166 167 lcd.setCursor(7, 0); 168 if (doorState == false) { 169 lcd.print("Kapi Acik"); 170 } else { 171 lcd.print(" "); 172 } 173 174 lcd.setCursor(0, 1); 175 176 lcd.print("L"); 177 lcd.setCursor(1, 1); 178 lcd.print(val1); 179 180 val1 = encoderVal; 181 182 if (state5 == true) { 183 lcd.setCursor(4, 1); 184 lcd.print("AC:"); 185 lcd.setCursor(7, 1); 186 lcd.print(acSetTemp); 187 } else { 188 lcd.setCursor(4, 1); 189 lcd.print("AC:"); 190 lcd.setCursor(7, 1); 191 lcd.print("Off"); 192 } 193 194 lcd.setCursor(11, 1); 195 lcd.print("F:"); 196 lcd.setCursor(13, 1); 197 if (state1 == 0) { 198 lcd.print("Oto"); 199 } else if (state1 == 1) { 200 lcd.print("Off"); 201 } else if (state1 == 2) { 202 lcd.print("On "); 203 } 204 205 } 206 if (menuCount == 1) { //Far 207 lcd.setCursor(0, 0); 208 lcd.print("1 Far"); 209 lcd.setCursor(0, 1); 210 if (state1 == 0) { 211 lcd.print("> Otomatik"); 212 } else if (state1 == 1) { 213 lcd.print("> Kapali "); 214 } else if (state1 == 2) { 215 lcd.print("> Acik "); 216 } 217 } 218 if (menuCount == 2) { //Silgeç 219 lcd.setCursor(0, 0); 220 lcd.print("Arka Silecek"); 221 lcd.setCursor(0, 1); 222 if (state2 == true) { 223 lcd.print("> Acik "); 224 } else { 225 lcd.print("> Kapali"); 226 } 227 } 228 if (menuCount == 3) { // Sis farları 229 lcd.setCursor(0, 0); 230 lcd.print("3 Sis Farlari"); 231 lcd.setCursor(0, 1); 232 if (state3 == true) { 233 lcd.print("> Acik "); 234 } else { 235 lcd.print("> Kapali"); 236 } 237 } 238 if (menuCount == 4) { // Koltuk ısıtma 239 lcd.setCursor(0, 0); 240 lcd.print("4 Koltuk Isitma"); 241 lcd.setCursor(0, 1); 242 if (state4 == 0) { 243 lcd.print("> Kapali"); 244 } else { 245 lcd.print("> Acik "); 246 } 247 } 248 if (menuCount == 5) { // Klima 249 lcd.setCursor(0, 0); 250 lcd.print("5 Klima"); 251 lcd.setCursor(0, 1); 252 if (state5 == true) { 253 lcd.print("> Acik "); 254 } else { 255 lcd.print("> Kapali"); 256 } 257 258 acSetTemp = encoderVal; 259 if (acSetTemp < 16) { 260 encoderVal = 16; 261 } 262 263 if (acSetTemp > 25) { 264 encoderVal = 25; 265 } 266 267 lcd.setCursor(10, 1); 268 lcd.print(acSetTemp); //Dummy val for temp 269 lcd.setCursor(12, 1); 270 lcd.print((char) 223); 271 272 } 273} 274void pinControl() { 275 //---Channel 1 276 if (state1 == 0) { 277 if (lights == true) { 278 digitalWrite(dPin1, LOW); 279 } else { 280 digitalWrite(dPin1, HIGH); 281 } 282 } else if (state1 == 1) { 283 digitalWrite(dPin1, HIGH); 284 } else if (state1 == 2) { 285 digitalWrite(dPin1, LOW); 286 } 287 //---Channel 2 288 if (state2 == true) { 289 if (wiperTimeout < wiperCounter) { 290 digitalWrite(dPin2, LOW); 291 } 292 if (wiperTimeout + onTime < wiperCounter) { 293 digitalWrite(dPin2, HIGH); 294 wiperTimeout = wiperCounter + wiperInterval; 295 } 296 } 297 //---Channel 3 298 if (state3 == true) { 299 digitalWrite(dPin3, LOW); 300 } else { 301 digitalWrite(dPin3, HIGH); 302 } 303 //---Channel 4 304 if (state4 == 0) { 305 digitalWrite(dPin4, HIGH); 306 } else { 307 digitalWrite(dPin4, HIGH); 308 } 309 //---Channel 5 310 if (state5 == true && acSetTemp < temp) 311 { 312 digitalWrite(dPin5, LOW); 313 } else { 314 digitalWrite(dPin5, HIGH); 315 } 316 //---Channel 6 317 if (state6 == true) { 318 //digitalWrite(dPin6, HIGH); 319 } else { 320 //digitalWrite(dPin6, LOW); 321 } 322 323 pwmVal1 = map(val1, 0, 100, 0, 255); 324 analogWrite(pwmPin1, pwmVal1); 325} 326void lightCheck() { 327 if (analogRead(ldrPin) < lightsOnLim) { 328 lights = true; 329 pwmVal1 = 20; 330 } else if (analogRead(ldrPin) > lightsOffLim) { 331 lights = false; 332 } 333} 334void doorCheck() { 335 if (digitalRead(doorPin) == LOW) { 336 337 doorState = false; 338 } else { 339 340 doorState = true; 341 } 342} 343void darkMode() { 344 if (timeoutCounter > timeout) { 345 lcd.noBacklight(); 346 menuCount = 0; 347 } else { 348 lcd.backlight(); 349 } 350} 351void screenClean() { 352 if (menuCount != menuCountOld) { 353 lcd.clear(); 354 menuCountOld = menuCount; 355 } 356 if (encoderValOld != encoderVal) { 357 lcd.clear(); 358 encoderValOld = encoderVal; 359 } 360} 361void stateCheck() { 362 if (longPressActive == true) { 363 if (menuCount == 0) { 364 EEPROM.write(storePwm, val1); 365 delay(10); 366 lcd.noBacklight(); 367 delay(50); 368 lcd.backlight(); 369 } 370 if (menuCount == 1) { 371 if (state1 == 0) { 372 state1 = 1; 373 } else if (state1 == 1) { 374 state1 = 2; 375 } else if (state1 == 2) { 376 state1 = 0; 377 } 378 } 379 if (menuCount == 2) { 380 state2 = !state2; 381 } 382 if (menuCount == 3) { 383 state3 = !state3; 384 } 385 if (menuCount == 4) { 386 387 state4 = !state4; 388 } 389 if (menuCount == 5) { 390 state5 = !state5; 391 } 392 if (menuCount == 6) { 393 state6 = !state6; 394 } 395 } 396} 397void buttonCheck() { 398 if (digitalRead(clk) == LOW) { 399 if (buttonActive == false) { 400 buttonActive = true; 401 buttonTimer = millis(); 402 } 403 if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { 404 longPressActive = true; 405 //Uzun basma 406 stateCheck(); 407 } 408 } else { 409 if (buttonActive == true) { 410 if (longPressActive == true) { 411 longPressActive = false; 412 } else { 413 414 //kısa basma 415 timeout = timeoutCounter + 20; 416 417 if (menuCount < menuCountLim) { 418 menuCount++; 419 420 } else { 421 menuCount = 0; 422 encoderVal = val1; 423 } 424 425 } 426 buttonActive = false; 427 } 428 } 429} 430void tempCheck() { 431 432 int chk = DHT11.read(DHT11PIN); 433 temp = DHT11.temperature; 434 hum = DHT11.humidity; 435 436} 437void doEncoderA() { 438 // debounce 439 if ( rotating ) delay (1); // wait a little until the bouncing is done 440 // Test transition, did things really change? 441 if ( digitalRead(encoderPinA) != A_set ) { // debounce once more 442 A_set = !A_set; 443 // adjust counter + if A leads B 444 if ( A_set && !B_set ) 445 446 if (encoderVal < 100) { 447 encoderVal += 1; 448 } 449 timeout = timeoutCounter + 20; 450 rotating = false; // no more debouncing until loop() hits again 451 } 452} 453void doEncoderB() { 454 if ( rotating ) delay (1); 455 if ( digitalRead(encoderPinB) != B_set ) { 456 B_set = !B_set; 457 // adjust counter – 1 if B leads A 458 if ( B_set && !A_set ) 459 460 if (encoderVal > 0) { 461 encoderVal -= 1; 462 } 463 464 timeout = timeoutCounter + 20; 465 rotating = false; 466 } 467}
Comments
Only logged in users can leave comments