Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 |
I have an old rusty(was rusty) Lada Niva. After building every single part, I did not want to use big soviet buttons. After building new front console, I started this controller project.
I will add circuit design and comments soon...
Update: It started to work...
acrylic housing for lcd
Mounting tests...
#include "Timer.h"
Timer t;
unsigned long timeout;
unsigned long timeoutCounter;
unsigned long wiperTimeout = 30;
unsigned long wiperCounter;
unsigned long wiperInterval = 30;
byte onTime = 1;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <dht11.h>
#define DHT11PIN 1
dht11 DHT11;
#include <EEPROM.h>
int storePwm = 0;
byte encoderPinA = 2; // right (labeled DT on our decoder, yellow wire)
byte encoderPinB = 3; // left (labeled CLK on our decoder, green wire)
volatile unsigned int encoderVal = 50; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;
byte clk = 4;
int encoderValOld;
byte menuCount = 0;
byte menuCountLim = 5;
byte menuCountOld;
long buttonTimer = 0;
long longPressTime = 500;
boolean buttonActive = false;
boolean longPressActive = false;
byte state1 = 0;
bool state2 = false;
bool state3 = false;
byte state4 = 0;
bool state5 = false;
bool state6 = false;
bool state7 = false;
byte val1 = 30;
int pwmVal1;
byte acSetTemp = 23;
int temp;
int hum;
byte dPin1 = 6;
byte dPin2 = 7;
byte dPin3 = 8;
byte dPin4 = 9;
byte dPin5 = 10;
byte dPin6 = 11;
byte dPin7 = 12;
byte dPin8 = 13;
byte pwmPin1 = 5;
int ldrPin = A6;
int lightsOnLim = 400;
int lightsOffLim = 600;
bool lights = false;
int doorPin = A0;
bool doorState = false;
void setup() {
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE);
attachInterrupt(1, doEncoderB, CHANGE);
t.every(30000, tempCheck);
t.every(3000, lightCheck);
t.every(100, doorCheck);
pinMode(clk, INPUT_PULLUP);
pinMode (doorPin, INPUT_PULLUP);
pinMode (dPin1, OUTPUT);
pinMode (dPin2, OUTPUT);
pinMode (dPin3, OUTPUT);
pinMode (dPin4, OUTPUT);
pinMode (dPin5, OUTPUT);
pinMode (dPin6, OUTPUT);
pinMode (dPin7, OUTPUT);
pinMode (dPin8, OUTPUT);
digitalWrite (dPin1, HIGH);
digitalWrite (dPin2, HIGH);
digitalWrite (dPin3, HIGH);
digitalWrite (dPin4, HIGH);
digitalWrite (dPin5, HIGH);
digitalWrite (dPin6, HIGH);
digitalWrite (dPin7, HIGH);
digitalWrite (dPin8, HIGH);
pinMode(pwmPin1, OUTPUT);
digitalWrite(pwmPin1, 200);
lcd.begin();
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("NIVA 1600");
delay(300);
for (int x = 0; x < 16; x++) {
lcd.setCursor(x, 1);
lcd.print(">");
delay(30);
if (digitalRead(4) == LOW) {
break;
}
}
for (int x = 0; x < 16; x++) {
lcd.setCursor(x, 1);
lcd.print(" ");
delay(30);
if (digitalRead(4) == LOW) {
break;
}
}
delay(300);
lcd.clear();
lcd.backlight();
timeout = timeoutCounter + 20;
tempCheck();
val1 = EEPROM.read(storePwm);
encoderVal = val1;
}
void loop() {
rotating = true;
buttonCheck();
menuScreen();
screenClean();
darkMode();
t.update();
pinControl();
timeoutCounter = (millis() / 1000);
wiperCounter = (millis() / 1000);
}
void menuScreen() {
if (menuCount == 0) { //Ana ekran
lcd.setCursor(0, 0);
lcd.print(temp);
lcd.setCursor(2, 0);
lcd.print((char) 223);
lcd.setCursor(3, 0);
lcd.print(hum);
lcd.setCursor(5, 0);
lcd.print("%");
lcd.setCursor(7, 0);
if (doorState == false) {
lcd.print("Kapi Acik");
} else {
lcd.print(" ");
}
lcd.setCursor(0, 1);
lcd.print("L");
lcd.setCursor(1, 1);
lcd.print(val1);
val1 = encoderVal;
if (state5 == true) {
lcd.setCursor(4, 1);
lcd.print("AC:");
lcd.setCursor(7, 1);
lcd.print(acSetTemp);
} else {
lcd.setCursor(4, 1);
lcd.print("AC:");
lcd.setCursor(7, 1);
lcd.print("Off");
}
lcd.setCursor(11, 1);
lcd.print("F:");
lcd.setCursor(13, 1);
if (state1 == 0) {
lcd.print("Oto");
} else if (state1 == 1) {
lcd.print("Off");
} else if (state1 == 2) {
lcd.print("On ");
}
}
if (menuCount == 1) { //Far
lcd.setCursor(0, 0);
lcd.print("1 Far");
lcd.setCursor(0, 1);
if (state1 == 0) {
lcd.print("> Otomatik");
} else if (state1 == 1) {
lcd.print("> Kapali ");
} else if (state1 == 2) {
lcd.print("> Acik ");
}
}
if (menuCount == 2) { //Silgeç
lcd.setCursor(0, 0);
lcd.print("Arka Silecek");
lcd.setCursor(0, 1);
if (state2 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
}
if (menuCount == 3) { // Sis farları
lcd.setCursor(0, 0);
lcd.print("3 Sis Farlari");
lcd.setCursor(0, 1);
if (state3 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
}
if (menuCount == 4) { // Koltuk ısıtma
lcd.setCursor(0, 0);
lcd.print("4 Koltuk Isitma");
lcd.setCursor(0, 1);
if (state4 == 0) {
lcd.print("> Kapali");
} else {
lcd.print("> Acik ");
}
}
if (menuCount == 5) { // Klima
lcd.setCursor(0, 0);
lcd.print("5 Klima");
lcd.setCursor(0, 1);
if (state5 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
acSetTemp = encoderVal;
if (acSetTemp < 16) {
encoderVal = 16;
}
if (acSetTemp > 25) {
encoderVal = 25;
}
lcd.setCursor(10, 1);
lcd.print(acSetTemp); //Dummy val for temp
lcd.setCursor(12, 1);
lcd.print((char) 223);
}
}
void pinControl() {
//---Channel 1
if (state1 == 0) {
if (lights == true) {
digitalWrite(dPin1, LOW);
} else {
digitalWrite(dPin1, HIGH);
}
} else if (state1 == 1) {
digitalWrite(dPin1, HIGH);
} else if (state1 == 2) {
digitalWrite(dPin1, LOW);
}
//---Channel 2
if (state2 == true) {
if (wiperTimeout < wiperCounter) {
digitalWrite(dPin2, LOW);
}
if (wiperTimeout + onTime < wiperCounter) {
digitalWrite(dPin2, HIGH);
wiperTimeout = wiperCounter + wiperInterval;
}
}
//---Channel 3
if (state3 == true) {
digitalWrite(dPin3, LOW);
} else {
digitalWrite(dPin3, HIGH);
}
//---Channel 4
if (state4 == 0) {
digitalWrite(dPin4, HIGH);
} else {
digitalWrite(dPin4, HIGH);
}
//---Channel 5
if (state5 == true && acSetTemp < temp)
{
digitalWrite(dPin5, LOW);
} else {
digitalWrite(dPin5, HIGH);
}
//---Channel 6
if (state6 == true) {
//digitalWrite(dPin6, HIGH);
} else {
//digitalWrite(dPin6, LOW);
}
pwmVal1 = map(val1, 0, 100, 0, 255);
analogWrite(pwmPin1, pwmVal1);
}
void lightCheck() {
if (analogRead(ldrPin) < lightsOnLim) {
lights = true;
pwmVal1 = 20;
} else if (analogRead(ldrPin) > lightsOffLim) {
lights = false;
}
}
void doorCheck() {
if (digitalRead(doorPin) == LOW) {
doorState = false;
} else {
doorState = true;
}
}
void darkMode() {
if (timeoutCounter > timeout) {
lcd.noBacklight();
menuCount = 0;
} else {
lcd.backlight();
}
}
void screenClean() {
if (menuCount != menuCountOld) {
lcd.clear();
menuCountOld = menuCount;
}
if (encoderValOld != encoderVal) {
lcd.clear();
encoderValOld = encoderVal;
}
}
void stateCheck() {
if (longPressActive == true) {
if (menuCount == 0) {
EEPROM.write(storePwm, val1);
delay(10);
lcd.noBacklight();
delay(50);
lcd.backlight();
}
if (menuCount == 1) {
if (state1 == 0) {
state1 = 1;
} else if (state1 == 1) {
state1 = 2;
} else if (state1 == 2) {
state1 = 0;
}
}
if (menuCount == 2) {
state2 = !state2;
}
if (menuCount == 3) {
state3 = !state3;
}
if (menuCount == 4) {
state4 = !state4;
}
if (menuCount == 5) {
state5 = !state5;
}
if (menuCount == 6) {
state6 = !state6;
}
}
}
void buttonCheck() {
if (digitalRead(clk) == LOW) {
if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
longPressActive = true;
//Uzun basma
stateCheck();
}
} else {
if (buttonActive == true) {
if (longPressActive == true) {
longPressActive = false;
} else {
//kısa basma
timeout = timeoutCounter + 20;
if (menuCount < menuCountLim) {
menuCount++;
} else {
menuCount = 0;
encoderVal = val1;
}
}
buttonActive = false;
}
}
}
void tempCheck() {
int chk = DHT11.read(DHT11PIN);
temp = DHT11.temperature;
hum = DHT11.humidity;
}
void doEncoderA() {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
if (encoderVal < 100) {
encoderVal += 1;
}
timeout = timeoutCounter + 20;
rotating = false; // no more debouncing until loop() hits again
}
}
void doEncoderB() {
if ( rotating ) delay (1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
if (encoderVal > 0) {
encoderVal -= 1;
}
timeout = timeoutCounter + 20;
rotating = false;
}
}
Arduino for Niva - Without Timer Module
ArduinoI will go on this way. Timer module takes up space and pins and i realized my stereo has a clock :D
#include "Timer.h"
Timer t;
unsigned long timeout;
unsigned long timeoutCounter;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <dht11.h>
#define DHT11PIN 1
dht11 DHT11;
#include <EEPROM.h>
int storePwm = 0;
byte encoderPinA = 2; // right (labeled DT on our decoder, yellow wire)
byte encoderPinB = 3; // left (labeled CLK on our decoder, green wire)
volatile unsigned int encoderVal = 50; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;
byte clk = 4;
int encoderValOld;
byte menuCount = 0;
byte menuCountLim = 6;
byte menuCountOld;
long buttonTimer = 0;
long longPressTime = 500;
boolean buttonActive = false;
boolean longPressActive = false;
byte state1 = 0;
bool state2 = false;
bool state3 = false;
byte state4 = 0;
bool state5 = false;
bool state6 = false;
bool state7 = false;
byte val1 = 30;
int pwmVal1;
byte acSetTemp = 23;
int temp;
int hum;
byte dPin1 = 6;
byte dPin2 = 7;
byte dPin3 = 8;
byte dPin4 = 9;
byte dPin5 = 10;
byte dPin6 = 11;
byte dPin7 = 12;
byte dPin8 = 13;
byte pwmPin1 = 5;
int ldrPin = A6;
int lightsOnLim = 400;
int lightsOffLim = 600;
bool lights = false;
bool up = false;
bool down = false;
int doorPin = A0;
bool doorState = false;
void setup() {
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE);
attachInterrupt(1, doEncoderB, CHANGE);
t.every(30000, tempCheck);
t.every(5000, lightCheck);
t.every(100, doorCheck);
pinMode(clk, INPUT_PULLUP);
pinMode (doorPin, INPUT_PULLUP);
pinMode (dPin1, OUTPUT);
pinMode (dPin2, OUTPUT);
pinMode (dPin3, OUTPUT);
pinMode (dPin4, OUTPUT);
pinMode (dPin5, OUTPUT);
pinMode (dPin6, OUTPUT);
pinMode (dPin7, OUTPUT);
pinMode (dPin8, OUTPUT);
pinMode(pwmPin1, OUTPUT);
lcd.begin();
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("NIVA 1600");
delay(300);
for (int x = 0; x < 16; x++) {
lcd.setCursor(x, 1);
lcd.print(">");
delay(50);
if (digitalRead(4) == LOW) {
break;
}
}
for (int x = 0; x < 16; x++) {
lcd.setCursor(x, 1);
lcd.print(" ");
delay(50);
if (digitalRead(4) == LOW) {
break;
}
}
delay(300);
lcd.clear();
lcd.backlight();
timeout = timeoutCounter + 30;
tempCheck();
val1 = EEPROM.read(storePwm);
encoderVal = val1;
}
void loop() {
rotating = true;
up = false;
down = false;
buttonCheck();
menuScreen();
screenClean();
darkMode();
t.update();
pinControl();
timeoutCounter = (millis() / 1000);
}
void menuScreen() {
if (menuCount == 0) {
lcd.setCursor(0, 0);
lcd.print(temp);
lcd.setCursor(2, 0);
lcd.print((char) 223);
lcd.setCursor(3, 0);
lcd.print(hum);
lcd.setCursor(5, 0);
lcd.print("%");
lcd.setCursor(7, 0);
lcd.print("-");
lcd.setCursor(0, 1);
lcd.print("L");
lcd.setCursor(1, 1);
lcd.print(val1);
val1 = encoderVal;
if (state5 == true) {
lcd.setCursor(4, 1);
lcd.print("AC:");
lcd.setCursor(7, 1);
lcd.print(acSetTemp);
} else {
lcd.setCursor(4, 1);
lcd.print("AC:");
lcd.setCursor(7, 1);
lcd.print("Off");
}
lcd.setCursor(11, 1);
lcd.print("F:");
lcd.setCursor(13, 1);
if (state1 == 0) {
lcd.print("Oto");
} else if (state1 == 1) {
lcd.print("Off");
} else if (state1 == 2) {
lcd.print("On ");
}
}
if (menuCount == 1) {
lcd.setCursor(0, 0);
lcd.print("1 Far");
lcd.setCursor(0, 1);
if (state1 == 0) {
lcd.print("> Otomatik");
} else if (state1 == 1) {
lcd.print("> Kapali ");
} else if (state1 == 2) {
lcd.print("> Acik ");
}
}
if (menuCount == 2) {
lcd.setCursor(0, 0);
lcd.print("2 Priz / USB");
lcd.setCursor(0, 1);
if (state2 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
}
if (menuCount == 3) {
lcd.setCursor(0, 0);
lcd.print("3 Sis Farlari");
lcd.setCursor(0, 1);
if (state3 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
}
if (menuCount == 4) {
lcd.setCursor(0, 0);
lcd.print("4 Fan");
lcd.setCursor(0, 1);
if (state4 == 0) {
lcd.print("> Kapali");
} else if (state4 == 1) {
lcd.print("> Hafif ");
} else if (state4 == 2) {
lcd.print("> Yuksek");
}
}
if (menuCount == 5) {
lcd.setCursor(0, 0);
lcd.print("5 Klima");
lcd.setCursor(0, 1);
if (state5 == true) {
lcd.print("> Acik ");
} else {
lcd.print("> Kapali");
}
acSetTemp = encoderVal;
if (acSetTemp < 16) {
encoderVal = 16;
}
if (acSetTemp > 25) {
encoderVal = 25;
}
lcd.setCursor(10, 1);
lcd.print(acSetTemp); //Dummy val for temp
lcd.setCursor(12, 1);
lcd.print((char) 223);
}
if (menuCount == 6) {
lcd.setCursor(0, 0);
lcd.print("6 Yagmur Sensoru");
lcd.setCursor(0, 1);
if (state6 == 0) {
lcd.print("> Kapali");
} else {
lcd.print("> Acik ");
}
}
}
void pinControl() {
//---Channel 1
if (state1 == 0) {
if (lights == true) {
digitalWrite(dPin1, HIGH);
} else {
digitalWrite(dPin1, LOW);
}
} else if (state1 == 1) {
digitalWrite(dPin1, LOW);
} else if (state1 == 2) {
digitalWrite(dPin1, HIGH);
}
//---Channel 2
if (state2 == true) {
digitalWrite(dPin2, HIGH);
} else {
digitalWrite(dPin2, LOW);
}
//---Channel 3
if (state3 == true) {
digitalWrite(dPin3, HIGH);
} else {
digitalWrite(dPin3, LOW);
}
//---Channel 4
if (state4 == 0) {
digitalWrite(dPin4, LOW);
digitalWrite(dPin5, LOW);
} else if (state4 == 1) {
digitalWrite(dPin4, HIGH);
digitalWrite(dPin5, LOW);
} else if (state4 == 2) {
digitalWrite(dPin5, HIGH);
digitalWrite(dPin4, LOW);
}
//---Channel 5
if (state5 == true && acSetTemp < temp) {
digitalWrite(dPin6, HIGH);
} else {
digitalWrite(dPin6, LOW);
}
//---Channel 6
if (state6 == true) {
//digitalWrite(dPin7, HIGH);
} else {
//digitalWrite(dPin7, LOW);
}
pwmVal1 = map(val1, 0, 100, 0, 255);
analogWrite(pwmPin1, pwmVal1);
}
void lightCheck() {
if (analogRead(ldrPin) < lightsOnLim) {
lights = true;
} else if (analogRead(ldrPin) > lightsOffLim) {
lights = false;
}
}
void doorCheck() {
if (digitalRead(doorPin) == HIGH) {
if (val1 < 70) {
encoderVal++;
}
doorState = false;
} else {
byte oldVal = EEPROM.read(storePwm);
if (oldVal < val1) {
encoderVal--;
}
doorState = true;
}
}
void darkMode() {
if (timeoutCounter > timeout) {
lcd.noBacklight();
menuCount = 0;
} else {
lcd.backlight();
}
}
void screenClean() {
if (menuCount != menuCountOld) {
lcd.clear();
menuCountOld = menuCount;
}
if (encoderValOld != encoderVal) {
lcd.clear();
encoderValOld = encoderVal;
}
}
void stateCheck() {
if (longPressActive == true) {
if (menuCount == 0) {
EEPROM.write(storePwm, val1);
delay(10);
lcd.noBacklight();
delay(50);
lcd.backlight();
}
if (menuCount == 1) {
if (state1 == 0) {
state1 = 1;
} else if (state1 == 1) {
state1 = 2;
} else if (state1 == 2) {
state1 = 0;
}
}
if (menuCount == 2) {
state2 = !state2;
}
if (menuCount == 3) {
state3 = !state3;
}
if (menuCount == 4) {
if (state4 == 0) {
state4 = 1;
} else if (state4 == 1) {
state4 = 2;
} else if (state4 == 2) {
state4 = 0;
}
}
if (menuCount == 5) {
state5 = !state5;
}
if (menuCount == 6) {
state6 = !state6;
}
}
}
void buttonCheck() {
if (digitalRead(clk) == LOW) {
if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
longPressActive = true;
//Uzun basma
stateCheck();
}
} else {
if (buttonActive == true) {
if (longPressActive == true) {
longPressActive = false;
} else {
//kısa basma
timeout = timeoutCounter + 20;
if (menuCount < menuCountLim) {
menuCount++;
} else {
menuCount = 0;
encoderVal = val1;
}
}
buttonActive = false;
}
}
}
void tempCheck() {
int chk = DHT11.read(DHT11PIN);
temp = DHT11.temperature;
hum = DHT11.humidity;
}
void doEncoderA() {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
if (encoderVal < 100) {
encoderVal += 1;
}
timeout = timeoutCounter + 20;
up = true;
down = false;
rotating = false; // no more debouncing until loop() hits again
}
}
void doEncoderB() {
if ( rotating ) delay (1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
if (encoderVal > 0) {
encoderVal -= 1;
}
timeout = timeoutCounter + 20;
down = true;
up = false;
rotating = false;
}
}
Comments
Please log in or sign up to comment.