Hello!
I'm currently working on a lighting project and I'm trying to get the programming figured out before I order parts and sink money into this. As the title states, I'm trying to use an arduino to control the brightness of LEDs using PWM. I got the LEDs to turn on, but they just stay on and pushing the button in the simulation i set up in Wokwi doesn't do anything. I have a serial output for every setting but it's just spamming 1 in the console continously. Any help would be greatly appreciated!
That confusion is one major problem. Another is that you never read the button with a digitalRead(brightnessButton) A third is that pin4 is left floating.
Maybe look at the "How to Wire and Program a Button" stuff from here:
And for counting, try to understand the State Change Detection example from here:
The state change detection protects from repeatedly triggering the if() statement for as long as the button is active, instead, acting only when the button changes from being pressed to not-being-pressed & vice versa.
Work through the examples and learn how to understand what the code is doing, and then maybe you can work through your code and make sure it does what you want.
you may also disappointed to find that the brightness doesn't change uniformly acoss the range of value. The Led won't be half as bright at 127 as it was as 255, that the brightness will change dramatically near the end of the range
Noted! Thank you for the heads up! I'm looking to use either the Adafruit nOOds or some 2mm COB LEDs. I probably won't be running them too bright, but this is good information to have!
I felt like it was an obvious rookie mistake, but thank you for telling me the obvious. Programming isn't my strong suit, and I'm doing this to learn some basic arduino stuff.
I'll take a look at the examples you listed and I'll keep you guys updated! Thank you!
I managed to find some time and took a look at both!!
I got a good understanding down and it works great, but I'm trying to figure out how to do different levels of brightness with button pressed.
if (buttonPushCoutner = 1) {
analogWrite(backLED, 50);
} else if (buttonPushCounter = 2){
analogWrite(backLED, 120);
} else if (buttonPushCounter = 3){
analogWrite(backLED, 200);
} else {
buttonPushCounter = 0;
}
I'd throw this in the end of the void loop, but it's not working.
I'm going to look at other code and see what other people have done, but most people seem to just toggle between two options. I'm gonna take a look at this in the morning.
Im assuming "=" is math related
(I.e, 2+2 = 4?)
While "==" is true/false
Let me test it before i hit the hay
Edit: Got it to where it actually recognizes that the initial button count is 0, which is great!
I had to re-write the code again since my laptop shut down and I didn't write the changes anywhere. I think I'm getting close, I just need to make a few minor tweaks.
int brightnessButton = 4; //button pin?
byte brightnessCount; //Track button presses
int brightnessValue; //PWM set
int lastButtonState = 0;
int buttonState = 0;
int backLED = 2;
int frontLED = 3;
void setup() {
pinMode(brightnessButton, INPUT);
pinMode(backLED,OUTPUT);
pinMode(frontLED, OUTPUT);
analogWrite(backLED, brightnessValue);
analogWrite(frontLED, brightnessValue);
brightnessCount = 0;
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(brightnessButton);
if (buttonState != lastButtonState) { //if the button State does not match up with the previous state..
if (buttonState == HIGH){ //If the button State reads high, then do the following
brightnessCount++; //increment the counter by 1
Serial.println("on");
} else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState; //Then set the two to equal themselves
if (brightnessCount == 0) {
analogWrite(backLED, 10);
analogWrite(frontLED, 10);
Serial.print("0");
delay(50);
}
else if (brightnessCount == 1) {
analogWrite(backLED, 100);
analogWrite(frontLED, 100);
Serial.print("1");
delay(50);
}
else if (brightnessCount == 2) {
analogWrite(backLED, 150);
analogWrite(frontLED, 150);
Serial.print("2");
delay(50);
}
else if (brightnessCount == 3) {
analogWrite(backLED, 255);
analogWrite(frontLED, 255);
Serial.print("3");
delay(50);
}
else if (brightnessCount >= 4)
{
brightnessCount = 0;
delay(50);
}
}
EditEdit: IT WORKS
I need to push it twice to cycle to the next option, but IT WORKS
Added to the console to print "on" every time it detected a button press in a new line
So every "on" or "off" is a button press
That didn't match with what I read in the code, and doesn't match what I experience when I build and run your project.
I noticed that you print numbers to show the flow, and have a delay after each which I assume is to reduce spam.
Your original code advanced with each button press. So I do not know why you need twice to cycle. And this is the kind of behaviour that I cannot leave until I understand. More so because my experience is different.
Here's you code with a little trick to do printing only when necessary, it is otherwise your logic undamaged. I tried and sincerely hope. You will note the removal of the scattered printing, no great loss as that part is fairly easy to see working.
int brightnessButton = 4; //button pin?
byte brightnessCount; //Track button presses
int brightnessValue; //PWM set
int lastButtonState = 0;
int buttonState = 0;
int backLED = 2;
int frontLED = 3;
void setup() {
pinMode(brightnessButton, INPUT);
pinMode(backLED, OUTPUT);
pinMode(frontLED, OUTPUT);
analogWrite(backLED, brightnessValue);
analogWrite(frontLED, brightnessValue);
brightnessCount = 0;
Serial.begin(9600);
}
void loop() {
// print changes to brightnessCount
static int printedBrightness = -1; // but not for long
if (printedBrightness != brightnessCount) {
Serial.print("brightness count = ");
Serial.println(brightnessCount);
printedBrightness = brightnessCount;
}
buttonState = digitalRead(brightnessButton);
if (buttonState != lastButtonState) { //if the button State does not match up with the previous state..
if (buttonState == HIGH) { //If the button State reads high, then do the following
brightnessCount++; //increment the counter by 1
Serial.print(" <press> ");
} else {
// Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState; //Then set the two to equal themselves
if (brightnessCount == 0) {
analogWrite(backLED, 10);
analogWrite(frontLED, 10);
}
else if (brightnessCount == 1) {
analogWrite(backLED, 100);
analogWrite(frontLED, 100);
}
else if (brightnessCount == 2) {
analogWrite(backLED, 150);
analogWrite(frontLED, 150);
}
else if (brightnessCount == 3) {
analogWrite(backLED, 255);
analogWrite(frontLED, 255);
}
else if (brightnessCount >= 4)
{
brightnessCount = 0;
}
}
I'll do it again carefully, it is not without the realm of possibility that I automatically corrected something as I worked with your code.
It might be due to Wowki, as when I tested it last night it was running slow. I think wowki was either acting up or not working right, though I don't really know
But, I think I've got the code and wiring down. Thank ya'll for the help!