Hello all!
I am new to arduino, programming, and electronics in general and was looking for a bit of advice. This is also my first forum post so I hope that I am doing this correctly. I am putting together a 6 channel led dimmer and am having trouble getting the channels to read and update correctly. I have all six channels working and if I press each button individually, they each perform the required task. However, when I press 2 buttons (or more) at once, the first LED acts appropriately but the second does not respond until I release the first. Here is my setup:
Arduino Uno
6 momentary switches (with pull down resistors)
6 LEDs (with resistors)
The momentary switches are connected to pins 2,4,7,8,12, and 13
The LEDs are connected to pins 3, 5, 6, 9, 10, and 11
Here is my code (I am sure there are a lot of improvements that can be made):
// Initialize Button and LED variables
const int channel_A_button = 2;
const int channel_B_button = 4;
const int channel_C_button = 7;
const int channel_D_button = 8;
const int channel_E_button = 12;
const int channel_F_button = 13;
const int channel_A_LED = 3;
const int channel_B_LED = 5;
const int channel_C_LED = 6;
const int channel_D_LED = 9;
const int channel_E_LED = 10;
const int channel_F_LED = 11;
// Initialize button state variables for state tracking
int A_buttonstate = 0;
int B_buttonstate = 0;
int C_buttonstate = 0;
int D_buttonstate = 0;
int E_buttonstate = 0;
int F_buttonstate = 0;
int A_lastbuttonstate = 0;
int B_lastbuttonstate = 0;
int C_lastbuttonstate = 0;
int D_lastbuttonstate = 0;
int E_lastbuttonstate = 0;
int F_lastbuttonstate = 0;
// Initialize PWM variables
int fade = 5;
int brightness = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);
}
void loop() {
A_buttonstate = digitalRead(channel_A_button);
B_buttonstate = digitalRead(channel_B_button);
C_buttonstate = digitalRead(channel_C_button);
D_buttonstate = digitalRead(channel_D_button);
E_buttonstate = digitalRead(channel_E_button);
F_buttonstate = digitalRead(channel_F_button);
if (A_buttonstate != A_lastbuttonstate) { //Verify previous button state
if (A_buttonstate == HIGH) { //If different state is detected, Fade LED in or out depending on state
while(brightness >= 0) {
analogWrite(channel_A_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_A_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
A_lastbuttonstate = A_buttonstate; // Set new button state for check on next run
if (B_buttonstate != B_lastbuttonstate) {
if (B_buttonstate == HIGH) {
while(brightness >= 0) {
analogWrite(channel_B_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_B_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
B_lastbuttonstate = B_buttonstate;
if (C_buttonstate != C_lastbuttonstate) {
if (C_buttonstate == HIGH) {
while(brightness >= 0) {
analogWrite(channel_C_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_C_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
C_lastbuttonstate = C_buttonstate;
if (D_buttonstate != D_lastbuttonstate) {
if (D_buttonstate == HIGH) {
while(brightness >= 0) {
analogWrite(channel_D_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_D_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
D_lastbuttonstate = D_buttonstate;
if (E_buttonstate != E_lastbuttonstate) {
if (E_buttonstate == HIGH) {
while(brightness >= 0) {
analogWrite(channel_E_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_E_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
E_lastbuttonstate = E_buttonstate;
if (F_buttonstate != F_lastbuttonstate) {
if (F_buttonstate == HIGH) {
while(brightness >= 0) {
analogWrite(channel_F_LED, brightness);
brightness = brightness - fade;
delay(10);
}
}
else {
while(brightness <= 255) {
analogWrite(channel_F_LED, brightness);
brightness = brightness + fade;
delay(10);
}
}
}
F_lastbuttonstate = F_buttonstate;
}
I appreciate any insights!
Jonathan