So I need some help finding the issue with my code with this. I am working on a climate control system for an older vehicle, I have the servo control working along with the blend door and all that jazz, and got the buttons working to switch between the different modes. The problem arose when I got some backlit switches as a way to indicate which mode its in. basically if you push the head button, its lit to full brightness while all the other buttons are significantly dimmer so you can still see what they are at night, but know that's not the mode the climate control is in. Right now if I comment out my light control, it switches between the 5 modes with no problem(I have the serial monitor on to read without hooking up to the whole climate control system but it works just fine with the servos). But the problem arises when I add the lights in. when I hit a button, it switches to the mode and changes the lights accordingly, but then locks there, and I cant change between modes again. I'm somewhat a novice so any help I can get is greatly appreciated!
#include <Servo.h>
Servo mixservo;
Servo defservo;
Servo headservo;
Servo feetservo;
Servo recircservo;
const int mixpot = A0; //Pin Mapping
const int headb = 22;
const int headfb = 23;
const int feetb = 24;
const int demistb = 25;
const int defb = 26;
const int recircb = 27;
const int headbLED = 2;
const int headfbLED = 3;
const int feetbLED = 4;
const int demistbLED = 5;
const int defbLED = 6;
int mixval = 0; //variables
int mixmath = 0;
int airflow = 6;
bool head = false;
bool headf = false;
bool Feet = false;
bool demist = false;
bool def = false;
bool recirc = false;
bool intake = false;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousInputMillis = 0; // time when button press last checked
unsigned long previousLEDMillis = 0; // time when button press last checked
const int buttonInterval = 50; // number of millisecs between button readings
const int LEDInterval = 100;
void setup() {
Serial.begin(9600);
mixservo.attach(9);
defservo.attach(10);
headservo.attach(11);
feetservo.attach(12);
recircservo.attach(13);
pinMode(headb, INPUT_PULLUP);
pinMode(headfb, INPUT_PULLUP);
pinMode(feetb, INPUT_PULLUP);
pinMode(demistb, INPUT_PULLUP);
pinMode(defb, INPUT_PULLUP);
pinMode(recircb, INPUT_PULLUP);
pinMode(headbLED, OUTPUT);
pinMode(headfbLED, OUTPUT);
pinMode(feetbLED, OUTPUT);
pinMode(demistbLED, OUTPUT);
pinMode(defbLED, OUTPUT);
}
void loop() {
currentMillis = millis(); // capture the latest value of millis()
if (millis() - previousInputMillis >= buttonInterval) {
Input();
}
if (millis() - previousLEDMillis >= LEDInterval) {
Lights();
}
Serial.println(airflow);
}
void Input() { // Reads inputs of buttons and blend door pot
previousInputMillis += buttonInterval;
mixval = map(analogRead(mixpot), 0, 1023, 120, 50);
mixservo.write(mixval);
bool prev_head = head;
bool prev_headf = headf;
bool prev_Feet = Feet;
bool prev_demist = demist;
bool prev_def = def;
bool prev_recirc = recirc;
head = digitalRead(headb) == LOW;
headf = digitalRead(headfb) == LOW;
Feet = digitalRead(feetb) == LOW;
demist = digitalRead(demistb) == LOW;
def = digitalRead(defb) == LOW;
recirc = digitalRead(recircb ) == LOW;
if (prev_head != head) {
if (head) {
HEAD();
}
}
if (prev_headf != headf) {
if (headf) {
HEADF();
}
}
if (prev_Feet != Feet) {
if (Feet) {
FEET();
}
}
if (prev_demist != demist) {
if (demist) {
DEMIST();
}
}
if (prev_def != def) {
if (def) {
DEF();
}
}
if (prev_recirc != recirc) { //latching for recirc door, servo control goes in here
if (recirc and intake == false) {
intake = true;
}
else if (recirc and intake == true) {
intake = false;
}
}
}
void HEAD() {
airflow = 1;
defservo.write(150); //CLOSE
delay(150);
headservo.write(150); //OPEN
feetservo.write(20); //CLOSE
}
void HEADF() {
airflow = 2;
defservo.write(150); //CLOSE
delay(150);
headservo.write(150); //OPEN
feetservo.write(150); //OPEN
}
void FEET() {
airflow = 3;
defservo.write(150); //CLOSE
delay(150);
headservo.write(20); //CLOSE
feetservo.write(150); //OPEN
}
void DEMIST() {
airflow = 4;
headservo.write(20); //CLOSE
delay(150);
defservo.write(60); //OPEN
feetservo.write(150); //OPEN
}
void DEF() {
airflow = 5;
headservo.write(20); //CLOSE
delay(150);
defservo.write(60); //OPEN
feetservo.write(20); //CLOSE
}
void Lights() { //diffrent lights on for certain modes, with lights dimmed for unselected options
previousLEDMillis += LEDInterval;
if (airflow == 1) {
analogWrite(headbLED, 255);
analogWrite(headfbLED, 25);
analogWrite(feetbLED, 25);
analogWrite(demistbLED, 25);
analogWrite(defbLED, 25);
} else if (airflow == 2) {
analogWrite(headbLED, 25);
analogWrite(headfbLED, 255);
analogWrite(feetbLED, 25);
analogWrite(demistbLED, 25);
analogWrite(defbLED, 25);
} else if (airflow == 3) {
analogWrite(headbLED, 25);
analogWrite(headfbLED, 25);
analogWrite(feetbLED, 255);
analogWrite(demistbLED, 25);
analogWrite(defbLED, 25);
} else if (airflow == 4) {
analogWrite(headbLED, 25);
analogWrite(headfbLED, 25);
analogWrite(feetbLED, 25);
analogWrite(demistbLED, 255);
analogWrite(defbLED, 25);
} else if (airflow == 5) {
analogWrite(headbLED, 25);
analogWrite(headfbLED, 25);
analogWrite(feetbLED, 25);
analogWrite(demistbLED, 25);
analogWrite(defbLED, 255);
} else if (airflow > 5) {
analogWrite(headbLED, 25);
analogWrite(headfbLED, 25);
analogWrite(feetbLED, 25);
analogWrite(demistbLED, 25);
analogWrite(defbLED, 25);
}
}