Utilizing all PWM outputs on Uno with two switches

I am not super proficient with my Arduino but I am learning as I go. I have an UNO with two momentary on push button switches tied to A0 and A1. I have a few LED strings that are triggered off the output of pin 3, 5, 6, 9, 10 and 11. I am builiding a bar that has under cabinet lights that utilize pin 11 and A0 to toggle it on and off. It works perfectly.

Now I have a sign that uses A1 to toggle it on and off and there are 3 sets of RGB LED’s that I am using, but due to limitations on PWM outputs of the Arduino I am using:

String 1 - Green (Pin 3)
String 2 - Blue (Pin 5)
String 3 - Red (Pin 6)
Green (Pin 9)
Blue (Pin 10)

The String 1 and 2 just need to turn off and on, super simple. String 3 needs to have analogWrite so I can PWM those pins to achieve a color as close to orange as possible. Now I am using A1 to toggle the state of these pins from off to on, however it doesn’t seem to work. This is the closest I can get the code to work and it just toggles String 3 on and off, no PWM control. However as the code stands the LED’s on String 3 turn on when the analogWrite is 0…so I’m not sure what I am doing wrong. Any help or tips would be greatly appreciated! I am guessing my problem is running through the if/else statement while trying to set the PWM?

// Button inputs:
const int  underbuttonPin = A0;    // the pin that the pushbutton is attached to
const int  signbuttonPin = A1;    // the pin that the pushbutton is attached to

// LED outputs:
const int GREENledPinWheat =  3;      // the number of the green LED pin
const int BLUEledPinSun =  5;      // the number of the blue LED pin
const int REDledPinRun =  6;      // the number of the red LED pin
const int GREENledPinRun =  9;      // the number of the green LED pin
const int BLUEledPinRun =  10;      // the number of the blue LED pin
const int underledPin = 11;       // the pin that the white LED is attached to

// Variables that will change:
int underbuttonPushCounter = 0;   // counter for the number of button presses
int underbuttonState = 0;         // current state of the button
int lastunderbuttonState = 0;     // previous state of the button

int signbuttonPushCounter = 0;   // counter for the number of button presses
int signbuttonState = 0;         // current state of the button
int lastsignbuttonState = 0;     // previous state of the button

void setup() {
  // initialize the button pins as inputs:
  pinMode(underbuttonPin, INPUT);
  pinMode(signbuttonPin, INPUT);
  
  // initialize the LED's as an outputs:
  pinMode(GREENledPinWheat, OUTPUT);
  pinMode(BLUEledPinSun, OUTPUT);
  pinMode(REDledPinRun, OUTPUT);
  pinMode(GREENledPinRun, OUTPUT);
  pinMode(BLUEledPinRun, OUTPUT);
  pinMode(underledPin, OUTPUT);
  
  // initialize serial communication:
  //Serial.begin(9600);
}

void loop() {
  // read the undercabinet pushbutton input pin:
  underbuttonState = digitalRead(underbuttonPin);

  // compare the underbuttonState to its previous state
  if (underbuttonState != lastunderbuttonState) {
    // if the state has changed, increment the counter
    if (underbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      underbuttonPushCounter++;
      Serial.println("on");
      Serial.print("number of left button pushes:  ");
      Serial.println(underbuttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastunderbuttonState = underbuttonState;


  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (underbuttonPushCounter % 2 == 0) {
    digitalWrite(underledPin, LOW);
  } else {
    digitalWrite(underledPin, HIGH);
  }

  // read the sign pushbutton input pin:
  signbuttonState = digitalRead(signbuttonPin);

  // compare the signbuttonState to its previous state
  if (signbuttonState != lastsignbuttonState) {
    // if the state has changed, increment the counter
    if (signbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      signbuttonPushCounter++;
      Serial.println("on");
      Serial.print("number of right button pushes:  ");
      Serial.println(signbuttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastsignbuttonState = signbuttonState;

  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (signbuttonPushCounter % 2 == 0) {
      digitalWrite(GREENledPinWheat, LOW);
      digitalWrite(BLUEledPinSun, LOW);
      analogWrite(REDledPinRun, 0);
      analogWrite(GREENledPinRun, 0);
      analogWrite(BLUEledPinRun, 0);
      // Serial.println("SIGN OFF");
  } else {
      digitalWrite(GREENledPinWheat, HIGH);
      digitalWrite(BLUEledPinSun, HIGH);
      analogWrite(REDledPinRun, 0);
      analogWrite(GREENledPinRun, 0);
      analogWrite(BLUEledPinRun, 0);
      // Serial.println("SIGN ON");
  }

}

It's hard to visualise your circuit from your description. Can you post a schematic please? Hand drawn will be fine. Also links to the leds and led strings you are using. The forum sticky post will show you how to post links correctly. Thanks for using code tags.

I am not sure about the brand or specs of the LED strips as they were bought from aliexpress a few years ago. Likewise with the mosfet driver, from dealextreme, can handle up to 3a as the white led strip pulls too much for the ds2003 to handle. Each channel is tested with a 12v power supply and all work. Also ran the sample fader code and I could choose each channel (pin 3,5,6,9,10) and could control each wired led color on each strip. So it’s not a hardware issue…its my lack of coding skills.

See attached for the schematic I hope it makes more sense.

Not sure why you have used that DS2003 chip.
You can only drive a dozen or so LEDs total with this chip because of thermal limitations.
The first page of the datasheet can be misleading there.

Is that "undercabinet'" one a mosfet switch or a LED driver.
A picture would help.
Leo..

I’ve used the DS2003 on previous projects driving small LED strips without a problem. With the sign each LED string is under 12" long, the white however is two 6’ long strips which required the use of the Catalex single mos switch (something I have a few of and had ordered from DX many years ago). Here’s a photo of the setup in it’s case.

That’s a low-side mosfet switch with driver chip.
Ok to switch a LED strip <3Amp (<=150 white LEDs full power).

12" long is not a LED strp specification.
What’s important is the number of LEDs.
You might have to limit the PWM values to stop that DS chip from getting too hot.
Leo…

Wawa:
That’s a low-side mosfet switch with driver chip.
Ok to switch a LED strip <3Amp (<=150 white LEDs full power).

12" long is not a LED strp specification.
What’s important is the number of LEDs.
You might have to limit the PWM values to stop that DS chip from getting too hot.
Leo…

I appreciate your concern I have already determined current load for each channel being driven. The DS2003 will be able to handle it, as with the other projects I have used it for.

Any critique on the code I am having issue with?

I am guessing my problem is running through the if/else statement while trying to set the PWM?

No, you don’t appeared to be using the PWM signals at all. An analgWrite of HIGH, LOW or zero does not set the PWM going but is just the same as a digitalWrite.

Grumpy_Mike:
No, you don’t appeared to be using the PWM signals at all. An analgWrite of HIGH, LOW or zero does not set the PWM going but is just the same as a digitalWrite.

Correct. The way the code stands I can turn all the wired in LED's off and on without a problem. When I change the code to read analogWritew with a value between 0 and 255 it affects all the other LED's brightness for some reason. I'm suspecting it is due to my code with the if/else statement, as I put the "// Serial.println("SIGN OFF");" and "// Serial.println("SIGN ON");" and watched the serial monitor and it just filled it with the status...I'm sure that is causing PWM control issues? Instead I am stuck on how to create a state that will change and then set the PWM of the pins. When the state changes then it changes the PWM again.

When I change the code to read analogWritew with a value between 0 and 255 it affects all the other LED's brightness for some reason.

So we need to see that code, the one that goes wrong.
However, I must say this does not look like a software issue, it sounds more hardware related. So we need to see your schematic an a photograph of your build so we can check the two match.

I'm suspecting it is due to my code with the if/else statement, as I put the "// Serial.println("SIGN OFF");" and "// Serial.println("SIGN ON");" and watched the serial monitor and it just filled it with the status...I'm sure that is causing PWM control issues?

That can absolutely not happen.

Mike I posted the schematic up above. The code I am having issues with is 95% the same as what is posted, I have edited it and reposted it here:

// Button inputs:
const int  underbuttonPin = A0;    // the pin that the pushbutton is attached to
const int  signbuttonPin = A1;    // the pin that the pushbutton is attached to

// LED outputs:
const int GREENledPinWheat =  3;      // the number of the green LED pin
const int BLUEledPinSun =  5;      // the number of the blue LED pin
const int REDledPinRun =  6;      // the number of the red LED pin
const int GREENledPinRun =  9;      // the number of the green LED pin
const int BLUEledPinRun =  10;      // the number of the blue LED pin
const int underledPin = 11;       // the pin that the white LED is attached to

// Variables that will change:
int underbuttonPushCounter = 0;   // counter for the number of button presses
int underbuttonState = 0;         // current state of the button
int lastunderbuttonState = 0;     // previous state of the button

int signbuttonPushCounter = 0;   // counter for the number of button presses
int signbuttonState = 0;         // current state of the button
int lastsignbuttonState = 0;     // previous state of the button

void setup() {
  // initialize the button pins as inputs:
  pinMode(underbuttonPin, INPUT);
  pinMode(signbuttonPin, INPUT);
  
  // initialize the LED's as an outputs:
  pinMode(GREENledPinWheat, OUTPUT);
  pinMode(BLUEledPinSun, OUTPUT);
  pinMode(REDledPinRun, OUTPUT);
  pinMode(GREENledPinRun, OUTPUT);
  pinMode(BLUEledPinRun, OUTPUT);
  pinMode(underledPin, OUTPUT);
  
  // initialize serial communication:
  //Serial.begin(9600);
}

void loop() {
  // read the undercabinet pushbutton input pin:
  underbuttonState = digitalRead(underbuttonPin);

  // compare the underbuttonState to its previous state
  if (underbuttonState != lastunderbuttonState) {
    // if the state has changed, increment the counter
    if (underbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      underbuttonPushCounter++;
      Serial.println("on");
      Serial.print("number of left button pushes:  ");
      Serial.println(underbuttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastunderbuttonState = underbuttonState;


  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (underbuttonPushCounter % 2 == 0) {
    digitalWrite(underledPin, LOW);
  } else {
    digitalWrite(underledPin, HIGH);
  }

  // read the sign pushbutton input pin:
  signbuttonState = digitalRead(signbuttonPin);

  // compare the signbuttonState to its previous state
  if (signbuttonState != lastsignbuttonState) {
    // if the state has changed, increment the counter
    if (signbuttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      signbuttonPushCounter++;
      Serial.println("on");
      Serial.print("number of right button pushes:  ");
      Serial.println(signbuttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastsignbuttonState = signbuttonState;

  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (signbuttonPushCounter % 2 == 0) {
      digitalWrite(GREENledPinWheat, LOW);
      digitalWrite(BLUEledPinSun, LOW);
      analogWrite(REDledPinRun, 0);
      analogWrite(GREENledPinRun, 0);
      analogWrite(BLUEledPinRun, 0);
      Serial.println("SIGN OFF");
  } else {
      digitalWrite(GREENledPinWheat, HIGH);
      digitalWrite(BLUEledPinSun, HIGH);
      analogWrite(REDledPinRun, 100);
      analogWrite(GREENledPinRun, 200);
      analogWrite(BLUEledPinRun, 50);
      Serial.println("SIGN ON");
  }

}

The code is based off a pushbutton toggle state code that I snipped up and tried to make work. It works great for turning on and off a device (like the white LED’s), however it doesn’t work for the analogWrite. The Serial.printIn does flood the serial monitor window the way the code stands which is why I am assuming it is causing an issue with the analogWrite?

I have run the example Fade code and changed the LED pin to each 3, 5, 6, 9, 10 and 11 without issue.

/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 The analogWrite() function uses PWM, so if
 you want to change the pin you're using, be
 sure to use another PWM capable pin. On most
 Arduino, the PWM pins are identified with 
 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

 This example code is in the public domain.
 */

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

. The code I am having issues with is 95% the same as what is posted,

95% is not good enough if you are unable to post all your code then there is not much I can do.

Mike I posted the schematic up above.

But you did not post a photograph.

I ask for these things not to be awkward but for me to try and understand your system, in a hope that I can spot what is wrong.

If you feel you can not cooperate fully then I will leave you to the others to try and help you.
Good luck with your project.

Wow.

Grumpy_Mike:
95% is not good enough if you are unable to post all your code then there is not much I can do.

I posted the final non-working code in Post 11... I was trying a few iterations of the code to try and make it work, I figured there was something fundamentally wrong with how I was approaching it and what I had posted in the very first post would have revealed that. I wasn't sitting around waiting for someone to rewrite the code for me, as I was editing it and changing it to try and find a reason why it wasn't working.

Grumpy_Mike:
But you did not post a photograph.

I ask for these things not to be awkward but for me to try and understand your system, in a hope that I can spot what is wrong.

If you feel you can not cooperate fully then I will leave you to the others to try and help you.
Good luck with your project.

Sorry I am new to this forum, this actual post is my 7th here... I attached a schematic photo in Post #2 after PaulRB asked for it, PaulRB then made it easy to see, thanks Paul. I posted a schematic when asked and responded to every post, if that's not full cooperation I am not sure what you expect. Either way I figured it out and I'm onto the next project.

Also for the record I had the DS2003 power the RGB LED's for a brief 24 hour burn in and it was no warmer than the ambient air temperature. I had previous determined the current for each channel and the DS2003 could easily handle it per pin as well as the package as a whole.