I have difficulty in a program, because I adjust the pwm of a led through two buttons, but I can not make it flash, adjusting this time through two other buttons, could someone help me?
We cannot see your program.
I have difficulty in a program, because I adjust the pwm of a led through two buttons, but I can not make it flash, adjusting this time through two other buttons, could someone help me?
As I said on your other identical topic (oops), I pointed out that we can't see your code
For now I'm not putting the buttons, but so the led does not go up either, after I put the programming part of millis
int ledPin = 5;
int incPin = 4;
int decPin = 8;
int bPin = 7;
int cPin = 2;
int val=0;
int incButton = LOW;
int decButton = LOW;
int estadoLed = LOW;
unsigned long previousMillis = 0;
const long intervalo = 1000;
int currentMillis =0;
void setup()
{
pinMode (cPin, INPUT);
pinMode(bPin, INPUT);
pinMode(incPin, INPUT);
pinMode(decPin, INPUT);
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
Serial.begin(9600);
}
void loop()
{
unsigned long currentMIllis = millis();
if (currentMillis - previousMillis >= intervalo){
previousMillis = currentMillis;
if (estadoLed == LOW){
incButton = digitalRead(incPin);
decButton = digitalRead(decPin);
if (incButton == HIGH)
{
if(val==255)val=255;
else val++;
}
if(decButton == HIGH)
{
if(val==0)val=0;
else val=val-1;
}
Serial.println(val);
delay(120);
analogWrite(ledPin, val);
}
estadoLed = val ;
} else { estadoLed = LOW; }
analogWrite(ledPin, estadoLed);}
Topics in 2 different sections merged
Please do not cross post in multiple sections of the forum. It wastes time and means that answers may be split into more than one locations
sorry, but i'm new here
if (val == 255) val = 255;
else val++;
Is this style of coding actually involve some kind of mistake?
Since if your range is 0-255, this is the same as:
if (val < 255)
val++;
yes, the same thing, but i am new here, and how can i use blink in this program? and can i control this blink with 2 other buttons?
Your code won't be very responsive, only checking the buttons once a second.
Why not run two separate phases, say 10Hz to check the buttons, and a second phase to blink the LEDs at a rate dependent upon the input from your second set of buttons?
Please remember to use code tags when posting code.
Just explain in plain words how you want to blink.
The whole code is quite cryptic, for instance what for you call analogWrite(ledPin, estadoLed) on each loop cycle? It just do what you asked from it to do - PWM light with estadoLed value!
as well? like the flashing value, would you put the pwm?
Do you want this way, for instance:
- Light 500ms with given PWM value
- Turn off for 500ms
And so on? In the same time to be able to change value of brightness at any time with these two buttons?
That would be a clear demand how your program should behave and clear path how to code properly.
yes, in the first moment it would be like you said, but if i add more two buttons for change the delay?
It would be fine, you would change duration value then with these additional buttons.
yes, but what would the code look like (1. Light 500ms with given PWM value
2. Turn off for 500ms)
See reply #9.
Have one function that looks a lot like the code in loop, but just checking all the button pins, and setting the brightness and blink rate (global variables). This function runs at about 10Hz.
Another function runs at whatever rate your buttons have set, and blinks the LED.
Both functions are called from loop.
Your code may look something as follows.
- The PWM value you need to set only once, when changed.
- Each 500ms is changed ON/OFF state
- Debouncing buttons are set for 100ms
- Limitation should be added in order not be able to press both buttons at once
Code is wrote as is, without any testing, which is on you further to play with as you want.
#define ledPin 5
#define incPin 4
#define decPin 8
#define bPin 7
#define cPin 2
int val = 255;
byte incButton = LOW;
byte decButton = LOW;
bool light = false;
bool pressed = false;
unsigned long interval = 500;
unsigned long debouncing = 50;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long debounceMillis = 0;
void setup()
{
pinMode (cPin, INPUT);
pinMode (bPin, INPUT);
pinMode (incPin, INPUT);
pinMode (decPin, INPUT);
pinMode (ledPin, OUTPUT);
analogWrite (ledPin, val);
Serial.begin(9600);
}
void loop()
{
unsigned long currentMIllis = millis();
// Debounce code for buttons
if (currentMillis - debounceMillis >= 100) {
debounceMillis = currentMillis;
incButton = digitalRead (incPin);
decButton = digitalRead (decPin);
if (incButton == HIGH && val < 255)
{
val++;
pressed = true;
}
if (decButton == HIGH && val > 0)
{
val--;
pressed = true;
}
if (pressed)
{
pressed = false;
Serial.print("New PWM value: ");
Serial.println(val);
if (light)
analogWrite(ledPin, val);
else
analogWrite(ledPin, 0);
}
} // debouncing buttons code end
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
light = ! light;
if (light)
analogWrite(ledPin, val);
else
analogWrite(ledPin, 0);
if (light)
Serial.println("ON");
else
Serial.println("OFF");
}
}
Edit: Declaration for pins where as well replaced from original OPs code
Or instead of doing this
if (light)
analogWrite(ledPin, val);
else
analogWrite(ledPin, 0);
if (light)
Serial.println("ON");
else
Serial.println("OFF");
}
}
you can do what grown-ups do, and factor your code
if (light) {
analogWrite(ledPin, val);
Serial.println (F("On"));
}
else {
analogWrite(ledPin, 0);
Serial.println (F("OFF"));
}
Also, your pin numbers should be "const byte", not "int"
Edit: I now see @noob_pi has redacted the thread, and has now used integer literals. Whatever.
Gentle reminder to all in this thread that I would rather be doing other things.
Play nice please.
Bob.