First of all, i hope that the subject title isn't too vague. I'm new to programming, so it's hard for me to desribe the problem!
This program is designed to control 3 LED circuits via PWM. Using 4 buttons (this will later be changed to a 2-axis joystick), a user can switch between presets (left - right), and brightness of the LED's (up, down).
Currently, the hardware used for the project has a single button which controls the preset, and 2 buttons to control the brightness trim amount.
All buttons return LOW when pressed.
When i run the program, nothing lights up, and pressing a button doesn't change anything.
On the hardware side, i am positive that everything is wired up correctly, since previous, more simple code has been successfull. The problems started when i implemented the brightness trim code into each preset, using a while loop. Previously, i had the brightness trim section as a seperate if statement. This meant that a brightness change only would occur when a preset was changed - and the code is supposed to be able to change brightness while inside a preset.
I am using an Arduino Uno.
The code:
//Declare LED variables
int yled = 10;
int bled = 9;
int wled = 11;
int b_ch = 15; // amount of brightness change when brightness is trimmed
int p_up = 2; // preset up button
int b_up = 3; // Brightness up button
int b_down = 4; // brightness down button
// preset counter variables
int preset = 0; // counter for the number of the button presses
int presetUpState = 0; // current state of the button
int presetUpLBS = 1; // previous state of the button
int presetDownState = 0; // Current state of the button, currently NOT being used
int presetDownLBS = 0; // last state of the button, currently NOT being used
// brightness trim variables
int b_trim = 0; // holder for the brightness trim amount
//Brightness counter variables
int trimUpState = 0; // current state of button
int trimUpLBS = 1; // previous state of button
int trimDownState = 0; // Current state of button
int trimDownLBS = 1; // Previous state of button
// Declare I/O
void setup(){
pinMode(yled, OUTPUT);
pinMode(bled, OUTPUT);
pinMode(wled, OUTPUT);
pinMode(p_up, INPUT);
pinMode(b_up, INPUT);
pinMode(b_down, INPUT);
}
void loop() {
presetUpState = digitalRead(p_up); //reads the state of the preset button
if(presetUpState != presetUpLBS){ //if the preset button is not equal to the previous value (1), do function
if(presetUpState == LOW){ //checks if the preset button is pressed
preset++; //adds 1 to the preset counter integer
if(preset == 1){ //if the preset counter equals 1
while(digitalRead(p_up) == HIGH){ //While the preset button reads high (not pressed), do loop:
if(digitalRead(p_up) == LOW){ //reads the preset button, if preset button returns low (pressed), breaks loop
break;
}
trimUpState = digitalRead(b_up); //reads the brightness trim up button
if(trimUpState !=trimUpLBS){ //if the button doesn't equal the previous value (pressed), continue function
if(trimUpState == LOW){ //if button is pressed, do said action
b_trim = b_trim + b_ch;
}
trimUpLBS = trimUpState; //Makes the last button state the current state, to check for another press.
}
trimDownState = digitalRead(b_down); //same as previous function
if(trimDownState !=trimDownLBS){
if(trimDownState == LOW){
b_trim = b_trim - b_ch;
}
trimDownLBS = trimDownState;
}
analogWrite(yled, 0); //writes the values to the LED pins
analogWrite(bled, 0);
analogWrite(wled, 150 + b_trim);
}
}
else if(preset == 2){ //same procedure as last preset change
while(digitalRead(p_up) == HIGH){ //While the preset button reads high (not pressed), do loop:
if(digitalRead(p_up) == LOW){ //reads the preset button, if preset button returns low (pressed), breaks loop
break;
}
trimUpState = digitalRead(b_up);
if(trimUpState !=trimUpLBS){
if(trimUpState == LOW){
b_trim = b_trim + b_ch;
}
trimUpLBS = trimUpState;
}
trimDownState = digitalRead(b_down);
if(trimDownState !=trimDownLBS){
if(trimDownState == LOW){
b_trim = b_trim - b_ch;
}
trimDownLBS = trimDownState;
}
analogWrite(yled, 0);
analogWrite(bled, 150 + b_trim);
analogWrite(wled, 0);
}
}
else if(preset == 3){
while(digitalRead(p_up) == HIGH){ //While the preset button reads high (not pressed), do loop:
if(digitalRead(p_up) == LOW){ //reads the preset button, if preset button returns low (pressed), breaks loop
break;
}
trimUpState = digitalRead(b_up);
if(trimUpState !=trimUpLBS){
if(trimUpState == LOW){
b_trim = b_trim + b_ch;
}
trimUpLBS = trimUpState;
}
trimDownState = digitalRead(b_down);
if(trimDownState !=trimDownLBS){
if(trimDownState == LOW){
b_trim = b_trim - b_ch;
}
trimDownLBS = trimDownState;
}
analogWrite(yled, 150 + b_trim);
analogWrite(bled, 0);
analogWrite(wled, 0);
}
}
else if(preset == 4){ //if the preset counter gets to 4, make the preset counter 0
preset = 0;
}
}
}
presetUpLBS = presetUpState; //make LBS (last button state) of the preset button the current state, so function can check for a new press
}
I appreciate any help or input you got!
Some info about the project:
Currently, i'm waiting for some High powered LEDs, 3x 10W PWM led drivers and other stuff to arrive, which i'm going to install in an old desk lamp. This, together with an arduino is used to control the brightness of the 3 different channels, so its possible to generate different light settings. bright white/bluish light - a "lab" mode, Yellowish light - "Reading" mode, Combined white/yellow light - "standard" mode, and so on.