/*
Arduino | coding-help
Arduino leonardo pro micro
_nick.tzfs — 7/11/24 at 6:29 PM
I am making a project with a arduino leonardo pro micro a rotary encoder
3 tip120 transistors and 4 rgb leds. I used chat gpt to help me with
the circuit but whenever i plug it in the led remains white and cant
change it with code.
Can somebody help me with the wiring and the code because i want this
project for my gf and and has to be ready by Monday?
I want the rotation of the encoder to contol the brightness of the leds
and the button of the encoder to change between 4-5 different colors
when pressed. I am willing to pay a short amount if somebody needs it to help me
AnonEngineering — 7/11/24 at 11:34 PM
share the circuit diagram, sounds like all three transistors are on all
the time, giving you white
_nick.tzfs — 7/12/24 at 5:36 AM
Yes i found out tha i connected the colector and emmiter reversly
because i forgot i putted it on positive side. Can you help with the code.
Encoder tutorial
https://arduinogetstarted.com/tutorials/arduino-rotary-encoder
HSV code
https://forum.arduino.cc/t/color-changing-rgb-led-rainbow-spectrum/8561
Pro Micro PWM pins: 3, 5, 6, 9, 10
*/
const int CLK_PIN = 2;
const int DT_PIN = 3;
const int BTN_PIN = 4;
const int NUM_COLORS = 8;
const int RGB_PINS[] = {11, 10, 9};
const int HSV_COLORS[NUM_COLORS][3] = {
// Hue, Saturation, Value
{ 0, 0, 100}, // White
{ 0, 100, 100}, // Red
{ 60, 100, 100}, // Yellow
{120, 100, 100}, // Green
{180, 100, 100}, // Cyan
{240, 100, 100}, // Blue
{300, 100, 100}, // Magenta
{360, 100, 100} // Red
};
// ISR variables need to be volatile
volatile int brightness = 100;
volatile unsigned long last_time; // for debouncing
int colorIndex = 0;
int prev_bright = 0;
int oldBtnState = HIGH;
int checkButton() {
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) { // button was pressed
colorIndex++;
if (colorIndex == NUM_COLORS) colorIndex = 0;
Serial.print("Color: ");
Serial.println(colorIndex);
} else { // button was released
// nothing to do
}
delay(20); // for debounce
}
return colorIndex;
}
long HSBtoRGB(float _hue, float _sat, float _brightness) {
float red = 0.0;
float green = 0.0;
float blue = 0.0;
if (_sat == 0.0) {
red = _brightness;
green = _brightness;
blue = _brightness;
} else {
if (_hue == 360.0) {
_hue = 0;
}
int slice = _hue / 60.0;
float hue_frac = (_hue / 60.0) - slice;
float aa = _brightness * (1.0 - _sat);
float bb = _brightness * (1.0 - _sat * hue_frac);
float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
switch (slice) {
case 0:
red = _brightness;
green = cc;
blue = aa;
break;
case 1:
red = bb;
green = _brightness;
blue = aa;
break;
case 2:
red = aa;
green = _brightness;
blue = cc;
break;
case 3:
red = aa;
green = bb;
blue = _brightness;
break;
case 4:
red = cc;
green = aa;
blue = _brightness;
break;
case 5:
red = _brightness;
green = aa;
blue = bb;
break;
default:
red = 0.0;
green = 0.0;
blue = 0.0;
break;
}
}
long ired = red * 255.0;
long igreen = green * 255.0;
long iblue = blue * 255.0;
return long((ired << 16) | (igreen << 8) | (iblue));
}
void ISR_encoderChange() {
if ((millis() - last_time) < 50) // debounce time is 50ms
return;
if (digitalRead(DT_PIN) == HIGH) {
// the encoder is rotating in counter-clockwise direction => decrease the counter
brightness -= 10;
if (brightness <= 0) brightness = 0;
} else {
// the encoder is rotating in clockwise direction => increase the counter
brightness += 10;
if (brightness >= 100) brightness = 100;
}
last_time = millis();
}
void setColor (unsigned char red, unsigned char green, unsigned char blue) {
analogWrite(RGB_PINS[0], 255 - red); // "255 - r/g/b" for common anode
analogWrite(RGB_PINS[1], 255 - green);
analogWrite(RGB_PINS[2], 255 - blue);
}
void setup() {
Serial.begin(115200);
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
for (int ledPin = 0; ledPin < 3; ledPin++) {
pinMode(RGB_PINS[ledPin], OUTPUT);
}
attachInterrupt(digitalPinToInterrupt(CLK_PIN), ISR_encoderChange, RISING);
Serial.print("Color: ");
Serial.println(colorIndex);
}
void loop() {
int colorIdx = checkButton();
float hue = float (HSV_COLORS[colorIdx][0]);
float saturation = HSV_COLORS[colorIdx][1] / 100.0;
float value = brightness / 100.0;
long color = HSBtoRGB(hue, saturation, value);
// Get the red, blue and green parts from generated color
int red = color >> 16 & 255;
int green = color >> 8 & 255;
int blue = color & 255;
setColor(red, green, blue);
if (prev_bright != brightness) {
Serial.print("Brightness: ");
Serial.println(brightness);
prev_bright = brightness;
}
}