Controlling multipul LED's with PWM

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);
  }
  

}

just to add I've also tried with different structures, such as this

void Lights() {  //diffrent lights on for certain modes, with lights dimmed for unselected options
  previousLEDMillis += LEDInterval;
  
    switch (airflow){
      case 1:
       analogWrite(headbLED, 255);
       analogWrite(headfbLED, 25);
       analogWrite(feetbLED, 25);
       analogWrite(demistbLED, 25);
       analogWrite(defbLED, 25);
      break;
      case 2:
       analogWrite(headbLED, 25);
       analogWrite(headfbLED, 255);
       analogWrite(feetbLED, 25);
       analogWrite(demistbLED, 25);
       analogWrite(defbLED, 25);
      break;
      case 3:
       analogWrite(headbLED, 25);
       analogWrite(headfbLED, 25);
       analogWrite(feetbLED, 255);
       analogWrite(demistbLED, 25);
       analogWrite(defbLED, 25);
      break;
      case 4:
       analogWrite(headbLED, 25);
       analogWrite(headfbLED, 25);
       analogWrite(feetbLED, 25);
       analogWrite(demistbLED, 255);
       analogWrite(defbLED, 25);
      break;
      case 5:
       analogWrite(headbLED, 25);
       analogWrite(headfbLED, 25);
       analogWrite(feetbLED, 25);
       analogWrite(demistbLED, 25);
       analogWrite(defbLED, 255);
      break;
      default:
       analogWrite(headbLED, 25);
       analogWrite(headfbLED, 25);
       analogWrite(feetbLED, 25);
       analogWrite(demistbLED, 25);
       analogWrite(defbLED, 25);
      break;
    }

  
  

}

Use some Serial.print statements to see where you're stuck. I haven't analyzed your use of millis, but I'll bet that once you are in lights(), the test in loop() is always true.

What is this line for?
previousLEDMillis += LEDInterval;

This line is used to add the Millis time to the next check so it has something to compare to accurately in the next loop() check

I'll add some print lines in different spots to see if it's getting stuck

so after some checking and using the serial monitor, it seems after it writes PWM at any point, I stop getting anything out of the digital reads, am I running into a limit hardware wise? I'm using a mega 2560(robot dyn mini)

You save millis() and then throw the result away.

  delay(150);

delay does not belong in a program using millis() for timing.

Try:

void Input() {   // Reads inputs of buttons and blend door pot

 static bool prev_head;
 static   bool prev_headf;
 static   bool prev_Feet;
 static   bool prev_demist;
 static   bool prev_def;
 static   bool prev_recirc;

  previousInputMillis += buttonInterval;

  mixval = map(analogRead(mixpot), 0, 1023, 120, 50);
  mixservo.write(mixval);

  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;
    }
  } 
 prev_head = head;
 prev_headf = headf;
 prev_Feet = Feet;
 prev_demist = demist;
 prev_def = def;
 prev_recirc = recirc;

}

lots of cut&paste, redundant code.

consider

#define MyHW
#ifdef MyHW
const byte mixpot     = A0;

const byte headbPIN   = A1;
const byte headfbPIN  = A2;
const byte feetbPIN   = A3;
const byte demistbPIN = A4;
const byte defbPIN    = A5;

const byte headbLED   = 13;
const byte headfbLED  = 12;
const byte feetbLED   = 11;
const byte demistbLED = 10;
const byte defbLED    = 9;

const byte pinMixServo    = 8;
const byte pinDefServo    = 7;
const byte pinHeadServo   = 6;
const byte pinFeetServo   = 5;
const byte pinRecircServo = 4;

#else
const byte mixpot     = A0;

const byte headbPIN   = 22;
const byte headfbPIN  = 23;
const byte feetbPIN   = 24;
const byte demistbPIN = 25;
const byte defbPIN    = 26;

const byte headbLED   = 2;
const byte headfbLED  = 3;
const byte feetbLED   = 4;
const byte demistbLED = 5;
const byte defbLED    = 6;

const byte pinMixServo    = 9;
const byte pinDefServo    = 10;
const byte pinHeadServo   = 11;
const byte pinFeetServo   = 12;
const byte pinRecircServo = 13;
#endif

// -----------------------------------------------------------------------------
#include <Servo.h>

struct Desc {
    byte    pinBut;

    byte    servoDef;
    byte    servoHead;
    byte    servoFeet;

    byte    ledHeadB;
    byte    ledHeadFb;
    byte    ledFeetB;
    byte    ledDemistB;
    byte    ledDefB;

    const char *label;

    byte    butState;
};

Desc desc [] = {
    { headbPIN,   150, 150,  20,  255,  25,  25,  25,  25,  "HEAD" },
    { headfbPIN,  150, 150, 150,   25, 255,  25,  25,  25,  "HEADF" },
    { feetbPIN,   150,  20, 150,   25,  25, 255,  25,  25,  "FEET" },
    { demistbPIN,  20,  60, 150,   25,  25,  25, 255,  25,  "DEMIST" },
    { defbPIN,     20,  60,  20,   25,  25,  25,  25, 255,  "DEF" },
};
#define N_DESC (sizeof (desc)/sizeof (Desc))

Servo mixservo;
Servo defservo;
Servo headservo;
Servo feetservo;
Servo recircservo;

byte pinLeds    [] = { headbLED, headfbLED, feetbLED, demistbLED, defbLED };

// -----------------------------------------------------------------------------
void
update (
    Desc *p)
{
    Serial.println (p->label);

    defservo.write  (p->servoDef);
    delay (150);
    headservo.write (p->servoHead);
    feetservo.write (p->servoFeet);

    analogWrite (headbLED,   p->ledHeadB);
    analogWrite (headfbLED,  p->ledHeadFb);
    analogWrite (feetbLED,   p->ledFeetB);
    analogWrite (demistbLED, p->ledDemistB);
    analogWrite (defbLED,    p->ledDefB);
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    Desc *p = desc;
    for (unsigned n = 0; n < N_DESC; n++, p++)  {
        byte but = digitalRead (p->pinBut);

        if (p->butState != but)  {
            p->butState = but;
            delay (10);             // debounce

            if (LOW == but)
                update (p);
        }
    }

    int mixval = map (analogRead (mixpot), 0, 1023, 120, 50);
    mixservo.write (mixval);
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    mixservo.attach    (pinMixServo);
    defservo.attach    (pinDefServo);
    headservo.attach   (pinHeadServo);
    feetservo.attach   (pinFeetServo);
    recircservo.attach (pinRecircServo);

    for (unsigned n = 0; n < sizeof(pinLeds); n++)
        pinMode (pinLeds [n], OUTPUT);

    Desc *p = desc;
    for (unsigned n = 0; n < N_DESC; n++, p++)  {
        pinMode (p->pinBut, INPUT_PULLUP);
        p->butState = digitalRead (p->pinBut);
    }
}

I appreciate the guidance I got! The main issue was I was running all the pins off all the buttons and LED's through a since 220 Ohm resistor, when I took that out of line it works perfectly fine, so when I transfer from per board to PCB ill need to keep them on separated lines for the LED's.
gcjr, your code looks immaculate and probably far more efficient then mine, but like I said I'm a novice, a lot of your code is far above me on what it even does so it will take me some time to try and implement but I like how much cleaner it is!

Here is my code, there a couple more things I want to fix but I mostly have the results from the 8 Buttons I wanted. mainly I cant get the PWM pins on 44-46 to work for me so I'm doing my own manual PWM to dim the PWR led.

#include <Servo.h>

Servo mixservo;
Servo defservo;
Servo headservo;
Servo feetservo;
Servo recircservo;
const int mixpot = A0;      //Pin Mapping
const int ACb = 20;
const int PWRB = 21;
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 l1LED = 2;
const int l2LED = 3;
const int l3LED = 4;
const int l4LED = 5;
const int l5LED = 6;
const int l6LED = 7;
const int l7LED = 8;
const int l8LED = 19;


int mixval = 0;       //variables
int airflow = 5;
int LEDON = 255;
int LEDDIM = 2;
bool PWR = false;
bool PWRC = false;
bool head = false;
bool headf = false;
bool Feet = false;
bool demist = false;
bool def = false;
bool recirc = false;
bool AC = false;
bool intake = true;
bool ACc = true;

long unsigned int LEDPrevious = 0;

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(l1LED, OUTPUT);
  pinMode(l2LED, OUTPUT);
  pinMode(l3LED, OUTPUT);
  pinMode(l4LED, OUTPUT);
  pinMode(l5LED, OUTPUT);
  pinMode(l6LED, OUTPUT);
  pinMode(l7LED, OUTPUT);
  pinMode(l8LED, OUTPUT);
}

void loop() {
  Serial.println(airflow);
  bool prev_PWR = PWR;
  PWR = digitalRead(PWRB) == LOW;

  if (prev_PWR != PWR) {
    if (PWR and PWRC == false) {
      digitalWrite(l8LED, HIGH);
      PWRC = true;
      Serial.println("ON");
    } else if (PWR and PWRC == true) {
      PWRC = false;
      Serial.println("OFF");
    }
  }
  if (PWRC == true) {

      mixval = map(analogRead(mixpot), 0, 1023, 40, 150);
      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;
      bool prev_AC = AC;

      head = digitalRead(headb) == LOW;
      headf = digitalRead(headfb) == LOW;
      Feet = digitalRead(feetb) == LOW;
      demist = digitalRead(demistb) == LOW;
      def = digitalRead(defb) == LOW;
      recirc = digitalRead(recircb) == LOW;
      AC = digitalRead(ACb) == 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) {
          RECIRC();
        }
      }
      if (prev_AC != AC) { //latching for AC
        if (AC) {
          AIRCON();
        }
     }
     if (LEDPrevious >= 10){
        Lights();
        LEDPrevious = 0;
    }else if (LEDPrevious < 10){
        LEDPrevious = LEDPrevious + 1;
    }
  }
  else {
    digitalWrite(l8LED, HIGH);
    delay(1);
    digitalWrite(l8LED, LOW);
    delay(5);
    analogWrite(l1LED, LOW);
    analogWrite(l2LED, LOW);
    analogWrite(l3LED, LOW);
    analogWrite(l4LED, LOW);
    analogWrite(l5LED, LOW);
    analogWrite(l6LED, LOW);
    analogWrite(l7LED, LOW);
  }
  
}

void HEAD() {
  airflow = 1;
  defservo.write(150); //CLOSE
  headservo.write(150); //OPEN
  feetservo.write(20); //CLOSE
}
void HEADF() {
  airflow = 2;
  defservo.write(150); //CLOSE
  headservo.write(150); //OPEN
  feetservo.write(150); //OPEN
}
void FEET() {
  airflow = 3;
  defservo.write(150); //CLOSE
  headservo.write(20); //CLOSE
  feetservo.write(150); //OPEN
}
void DEMIST() {
  airflow = 4;
  headservo.write(20); //CLOSE
  defservo.write(60); //OPEN
  feetservo.write(150); //OPEN
}
void DEF() {
  airflow = 5;
  headservo.write(20); //CLOSE
  defservo.write(60); //OPEN
  feetservo.write(20); //CLOSE
}
void RECIRC() {
  if (recirc and intake == false) {
    intake = true;
    recircservo.write(50);
  } else if (recirc and intake == true) {
    intake = false;
    recircservo.write(150);
  }
}
void AIRCON() {
  if (AC and ACc == false) {
    ACc = true;
  } else if (AC and ACc == true) {
    ACc = false;
  }
}
void Lights() {
  if (airflow == 1) {
    analogWrite(l1LED, LEDON);
    analogWrite(l2LED, LEDDIM);
    analogWrite(l3LED, LEDDIM);
    analogWrite(l4LED, LEDDIM);
    analogWrite(l5LED, LEDDIM);
  } else if (airflow == 2) {
    analogWrite(l1LED, LEDDIM);
    analogWrite(l2LED, LEDON);
    analogWrite(l3LED, LEDDIM);
    analogWrite(l4LED, LEDDIM);
    analogWrite(l5LED, LEDDIM);
  } else if (airflow == 3) {
    analogWrite(l1LED, LEDDIM);
    analogWrite(l2LED, LEDDIM);
    analogWrite(l3LED, LEDON);
    analogWrite(l4LED, LEDDIM);
    analogWrite(l5LED, LEDDIM);
  } else if (airflow == 4) {
    analogWrite(l1LED, LEDDIM);
    analogWrite(l2LED, LEDDIM);
    analogWrite(l3LED, LEDDIM);
    analogWrite(l4LED, LEDON);
    analogWrite(l5LED, LEDDIM);
  } else if (airflow == 5) {
    analogWrite(l1LED, LEDDIM);
    analogWrite(l2LED, LEDDIM);
    analogWrite(l3LED, LEDDIM);
    analogWrite(l4LED, LEDDIM);
    analogWrite(l5LED, LEDON);
  } else if (airflow > 5) {
    analogWrite(l1LED, LEDDIM);
    analogWrite(l2LED, LEDDIM);
    analogWrite(l3LED, LEDDIM);
    analogWrite(l4LED, LEDDIM);
    analogWrite(l5LED, LEDDIM);
  }

  if (ACc == true){
      analogWrite(l7LED, LEDON);
  } else if (ACc == false){
      analogWrite(l7LED, LEDDIM);
  }
  if (intake == true){
      analogWrite(l6LED, LEDON);
  } else if (intake == false){
      analogWrite(l6LED, LEDDIM);
  }
  


}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.