i am trying to fade te led when i push te button but i dont wanna use delay function . thats mean when i push each time the led fade for some second or 2
void setup() {
pinMode(5, OUTPUT);
pinMode(8,INPUT_PULLUP);
Serial.begin(9600);
int i = 0;
}
void loop() {
if(digitalRead(8) == LOW)
for (int i = 0; i < 255; i++) {
analogWrite(5, i);
delay(100);
}
for (int i = 255; i > 0; i--) {
analogWrite(5, i);
delay(100);
}
if (digitalRead(8) == HIGH)
return;{
}
}
Check out my tutorial on How to write Timers and Delays in Arduino
and use a repeating timer to 'count' down the Analog write values.
call timer.stop() at the end when value ==0
Also check out my tutorial on Multi-tasking in Arduino as it is simpler to break your code into tasks.
One for the button checking, and one for the fade
Also check out my tutorial on Debouncing Switches in Arduino
how can i use when the button is pressed it fade from low to high and then from high to off because the delay i can use in de loop but only when de button is pressed
int btnState = 0;
bool trigger = LOW;
void setup ()
{
pinMode (5, OUTPUT);
pinMode (8, INPUT_PULLUP);
}
void loop ()
{
if ( digitalRead(8) == LOW)
{
if (trigger)
{
for (int i = 0; i < 255; i++)
{
analogWrite(5, i);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}for (int i = 255; i > 0; i--)
{
analogWrite(5, i);
// wait for 30 milliseconds to see the dimming effect
delay(30);
{
}
}
trigger = !trigger;
}
btnState = digitalRead(8);
}