RGB circuit issue - code or wiring?

Hello all. I have been trying to figure this out for a couple days, but I haven't yet. I want to control an RGB LED (and later RGB strips) using the Digispark ATTiny85 as the brain, a pushbutton to cycle through modes, and a potentiometer to change the values. It seemed like an easy project as there are plenty examples posted across the internet.

Issue: After Digispark boots up, the LED does not light unless pot is moved fully clockwise. Even then it is a very dim blue. The button has no effect on the circuit. I moved the pull-down resistor to make it a pull-up resistor and the LED turns on and stays a bright teal color.

Circuit: Power source is adjustable 5V.
Pull-down resistor on button is 10k (tried pull-up resistor as well)
PWM (0,1,4) resistors are 10k
RGB LED resistors are 220, 470, 330 (respectively)
10k Potentiometer
47uF capacitor (no difference noticed if removed from circuit
MOSFETs: IRL540N (also tried IRLZ44N)
Digispark ATTiny85: original as shown in picture (also tried code on micro USB version)

Code:
I found a code similar to what I want to do. After digging through the tutorial section, I made a few changes. I opted to create different mode 'functions' that the loop calls to if a condition is met. This way, I can change the modes if I want to.

// RGB LED Controller using Digispark ATTiny85 (edited)
// Uses a button to cycle through various predefined color blend states and a Potentiometer to blend them.
// 9 States: Dim White, Blend Green to Blue, Blend Blue to Red, Blend Red to Green, Blend All, Dim Red, Dim Green, Dim Blue,and Dim Purple
// Test Cycle with Pot at  100% = All, Blue, Red, Green, Red, Red, Green, Blue, Blue/Red
// 26-02-2016 Damon Palyka (me@damonp.com)
//
// Define Pins
const int redPin = 0;     // Red LED,   P0
const int grnPin = 1;     // Green LED, P1
const int bluPin = 4;     // Blue LED,  P4
const int potPin = 3;     // Potentiometer, A3 (Pin 3)
const int buttonPin = 5;  // Pushbutton, P5

// Define Variables
int stateNum = 0;         // current State number
int buttonState = 0;      // current state of the button
int lastButtonState = 0;  // previous state of the button
int redVal = 0;           // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int potVal = 0;           // Current Potentiometer Value
int lastPotVal = 0;       // Last Potentiometer Value
int RGBpotVal = 0;        // Pot Value for RGB Blend

void setup()
{
  //Define Inputs
  pinMode(buttonPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);

  modeDefault();   // Make default first when turned on?? Add EEPROM??
}


void loop()  {
  
    buttonState = digitalRead(buttonPin);   // Read Button state  **deleted "{" at beginning
    if (buttonState != lastButtonState) {   // Compare to last state. If current state doesn't match previous state...
      if (buttonState == HIGH) {            // If button is pushed, increment stateNum and blink onboard LED
         stateNum++;                        
        if (stateNum > 8) {                 // Defines Number of possible states  **added "{ " to the end   
           stateNum = 0;
        }                                   // added to close out last if statement
      else {                                // if the current state is LOW then the button went from on to off
                                            // Intentionally blank??
    }
    delay(30);                              // Delay to avoid bounce
      }  
      }
      lastButtonState = buttonState;        // update button state to loop


    int potVal = analogRead(potPin);         // Read Potentiometer Value From A3 (P3)  **deleted "{" at beginning
    //  potVal = (potVal / 4);               // Divide by 4 to normalize to 0-255 Range
    potVal = map(potPin, 0, 1023, 0, 255);   // using map statement vs normalize
    if (potVal != lastPotVal)  {             // Compare to previous pot value
      lastPotVal = potVal;                   // Update potVal to reflect new value
    }
    delay(20);                               // Delay to avoid bounce


  // Begin States section that call to mode functions below

  if (stateNum == 0) {       // Blend RGB
    modeDefault();
  }
  else if (stateNum == 1) {  // Blend Green to Blue
    mode1();
  }
  else if (stateNum == 2) {  // Blend Blue to Red
    mode2();
  }
  else if (stateNum == 3) {  // Blend Red to Green
    mode3();
  }
  else if (stateNum == 4) {  // All LEDs make white, off to brightest
    mode4();
  }
  else if (stateNum == 5) {  // Red Dim
    mode5();
  }
  else if (stateNum == 6) {  //Green Dim
    mode6();
  }
  else if (stateNum == 7) {  //Blue Dim
    mode7();
  }
  else if (stateNum == 8) {  //Purple Dim
    mode8();
  }

  //Send results to LEDs
  analogWrite(redPin, redVal);
  analogWrite(grnPin, grnVal);
  analogWrite(bluPin, bluVal);
}

// Mode functions. Will change them once circuit/code works
// Common Anode LED means inverse values

void modeDefault() {                      // When stateNum = 0,  Blend RGB
  if (potVal < 86) {                      // Lowest third of the potentiometer's range (0-85)
    RGBpotVal = (potVal * 3) ;            // Normalize to 0-255

    redVal = RGBpotVal;                   // Red from full to off
    grnVal = 255 - RGBpotVal;             // Green from off to full
    bluVal = 255;                         // Blue off
  }
  else if (potVal < 171) {                // Middle third of potentiometer's range (86-170)
    RGBpotVal = ( (potVal - 86) * 3);     // Normalize to 0-255

    redVal = 255;                         // Red off
    grnVal = RGBpotVal;                   // Green from full to off
    bluVal = 255 - RGBpotVal;             // Blue from off to full
  }
  else {                                  // Upper third of potentiometer"s range (171-255)
    RGBpotVal = ( (potVal - 171) * 3);    // Normalize to 0-255

    redVal = 255 - RGBpotVal;             // Red from off to full
    grnVal = 255;                         // Green off
    bluVal = RGBpotVal;                   // Blue from full to off
  }
}

void mode1() {  // When stateNum = 1, Blend Green to Blue
  redVal = 255;
  grnVal = potVal;
  bluVal = 255 - potVal;
}

void mode2() {  // When stateNum = 2, Blend Blue to Red
  redVal = 255 - potVal;
  grnVal = 255;
  bluVal = potVal;
}

void mode3()  { // When stateNum = 3, Blend Red to Green
  redVal = potVal;
  grnVal = 255 - potVal;
  bluVal = 255;
}

void mode4() {    // When stateNum = 4, All LEDs turn white, off to brightest
  redVal = 255 - potVal;   
  grnVal = 255 - potVal; 
  bluVal = 255 - potVal;
}

void mode5(){   // When stateNum = 5, Red Dim
  redVal = 255 - potVal;
  grnVal = 255;
  bluVal = 255;
}

void mode6() {  // When stateNum = 6, Green Dim
  redVal = 255;
  grnVal = 255 - potVal;
  bluVal = 255;
}

void mode7()  {  // When stateNum = 7, Blue Dim
  redVal = 255;
  grnVal = 255;
  bluVal = 255 - potVal;
}

void mode8() {  // When stateNum = 8, Purple Dim
    redVal = 255 - potVal;
    grnVal = 255;
    bluVal = 255 - potVal;
  }

rgbLED_pot_button_functions.ino (5.83 KB)

    int potVal = analogRead(potPin);         // Read Potentiometer Value From A3 (P3)  **deleted "{" at beginning
     potVal = map(potPin, 0, 1023, 0, 255);   // using map statement vs normalize

Using the "int" type descriptor you define a new local variable which hides the global variable of the same name. The subroutines won't see the value of this variable.

In the second line you're setting the value of "potVal" to the fixed value of 0 because "potPin" is always 3 and mapping it is the same as dividing by 4 which results in 0.

pylon:
Using the "int" type descriptor you define a new local variable which hides the global variable of the same name. The subroutines won't see the value of this variable.

In the second line you're setting the value of "potVal" to the fixed value of 0 because "potPin" is always 3 and mapping it is the same as dividing by 4 which results in 0.

Thanks for the reply. I'll look into global and local variables. I tried different combinations of int and no int in front of buttonState and potVal, but did not see any difference. I'm going to swap out the Digispark tomorrow. I might've fried one of the pins earlier.

Here is the useful links post that has a lot of great information on many subjects, among them is a post about scope which explains the difference between local and global variables.

Apologies for the absence. Had to take care of some family issues, but I'm back home. Changed the code around to get rid of the local variables (code below). I changed the pull-down resistor to pull-up.

It sort of works. I can cycle through the modes with the button. However, when I turn the pot more than halfway, the RGB LED turns off, and the program resets. Up until then, the LED changes colors (partially) and brightness. I tried it with 2 different Digispark ATTiny85's with the same result.

// RGB LED Controller using Digispark ATTiny85
// Uses a button to cycle through various predefined color blend states and a Potentiometer to blend them.
// 9 States: Dim White, Blend Green to Blue, Blend Blue to Red, Blend Red to Green, Blend All, Dim Red, Dim Green, Dim Blue,and Dim Purple
// Test Cycle with Pot at  100% = All, Blue, Red, Green, Red, Red, Green, Blue, Blue/Red
// 26-02-2016 Damon Palyka (me@damonp.com)
//
// Define Pins
const int redPin = 0;     // Red LED,   P0
const int grnPin = 1;     // Green LED, P1
const int bluPin = 4;     // Blue LED,  P4
const int potPin = 0;     // Potentiometer, P5 (analog 0)
const int buttonPin = 2;  // Pushbutton, P2

// Define Variables
int stateNum = 0;         // current State number
int buttonState = 0;      // current state of the button
int lastButtonState = 0;  // previous state of the button
int redVal = 0;           // Variables to store the values to send to the pins
int grnVal = 0;
int bluVal = 0;
int potVal = 0;           //Current Potentiometer Value
int lastPotVal = 0;       //Last Potentiometer Value
int RGBpotVal = 0;        //Pot Value for RGB Blend

void setup() 
{
  //Define Inputs
  pinMode(buttonPin, INPUT); 
  pinMode(potPin, INPUT);
  pinMode(redPin, OUTPUT); 
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT);

//  digitalWrite(buttonPin, LOW);  // Pushbutton read as LOW until pressed

 }


void loop() {
 buttonState = digitalRead(buttonPin); //Read Button state
 if (buttonState != lastButtonState) { //Compare to last state. If current state doesn't match previous state...
    if (buttonState == HIGH) {  //If button is pushed, increment stateNum and blink onboard LED
      stateNum++;
      if (stateNum > 8) { 
        stateNum = 0; //Defines Number of possible states  **added "{ " to beginning
      }                                                 // added to close out last if statement
    }
  lastButtonState = buttonState; } //update button state to loop
 delay(30); //Delay to avoid bounce
  

potVal = analogRead(potPin); //Read Potentiometer Value From A0 (P5)
potVal = (potVal / 4); //Divide by 4 to normalize to 0-255 Range

if (potVal != lastPotVal) { //Compare to previous pot value
  lastPotVal = potVal;  //Update potVal to reflect new value
    } 
 delay(20); //Delay to avoid bounce

//Begin States section
  
 if (stateNum == 0) { //Blend RGB 
 
  if (potVal < 86) {  // Lowest third of the potentiometer's range (0-85)
                    
    RGBpotVal = (potVal * 3) ;  // Normalize to 0-255

    redVal = RGBpotVal;         // Red from full to off
    grnVal = 255 - RGBpotVal;   // Green from off to full
    bluVal = 255;               // Blue off
  }
  else if (potVal < 171) {  // Middle third of potentiometer's range (86-170)
  
    RGBpotVal = ( (potVal - 86) * 3); // Normalize to 0-255

    redVal = 255;                     // Red off
    grnVal = RGBpotVal;               // Green from full to off
    bluVal = 255 - RGBpotVal;         // Blue from off to full
  }
  else {   // Upper third of potentiometer"s range (171-255)
  
    RGBpotVal = ( (potVal - 171) * 3); // Normalize to 0-255

    redVal = 255 - RGBpotVal;          // Red from off to full
    grnVal = 255;                      // Green off
    bluVal = RGBpotVal;                // Blue from full to off
  }
 }
else if (stateNum == 1) {  //Blend Green to Blue
 
    redVal = 255;             
    grnVal = potVal; 
    bluVal = 255 - potVal;        
  }
else if (stateNum == 2) {  //Blend Blue to Red
 
    redVal = 255 - potVal;        
    grnVal = 255;             
    bluVal = potVal; 
  } 
else if (stateNum == 3) {  //Blend Red to Green
   
    redVal = potVal; 
    grnVal = 255 - potVal;        
    bluVal = 255;   
 }       
 else if (stateNum == 4) {  //All LEDs make white, off to brightest
   
     redVal = 255 - potVal;
     grnVal = 255 - potVal; //Common Anode LED means inverse values 
     bluVal = 255 - potVal;        
  }
  else if (stateNum == 5) {  //Red Dim
   
    redVal = 255 - potVal; 
    grnVal = 255;        
    bluVal = 255;   
 }  
   else if (stateNum == 6) {  //Green Dim
   
    redVal = 255; 
    grnVal = 255 - potVal;        
    bluVal = 255;   
 }
   else if (stateNum == 7) {  //Blue Dim
 
    redVal = 255; 
    grnVal = 255;        
    bluVal = 255 - potVal;   
 }  
   else if (stateNum == 8) {  //Purple Dim
 
    redVal = 255 - potVal; 
    grnVal = 255;        
    bluVal = 255 - potVal;   
 }
 
//Send results to LEDs
 analogWrite(redPin, redVal);
 analogWrite(grnPin, grnVal); 
 analogWrite(bluPin, bluVal);
}

Double-check your wiring. You may have the pot shorting out the power.

That looks like pretty neat code. I see you are mixing colors. Look into using HSV instead of RGB because it mixes the colors and brightness in a way that makes sense to the eye. (Of course you convert to RGB for output to the LED.)

MorganS:
Double-check your wiring. You may have the pot shorting out the power.

That looks like pretty neat code. I see you are mixing colors. Look into using HSV instead of RGB because it mixes the colors and brightness in a way that makes sense to the eye. (Of course you convert to RGB for output to the LED.)

Thanks. I'll look into HSV. I double-checked the wiring. I swapped pots, but got the same results. The resistance from the 5v pin to the middle pin on the pot measures around 6700 ohms when the LED turns off and the program resets. So from 0-6700 ohms I see the color adjust. However, anything over 6700 ohms (going counterclockwise) causes the LED to turn off (and the Digispark to reset??). Used some Ohm's Law so see about a 3V output at 6700 ohms.

.....aaannnddd this was the point in my reply when I remembered reading about the 3.3V limit on A0 as soon as I saw the calculation for the 3V voltage drop. I swapped the pot to pin 3 (A3) and it works.

Going to try the HSV before I scale this up to an RGB LED strip and MOSFETs. Thanks everyone!