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)

