Go Down

Topic: Change Led Fade Speed With Potentiometer (Read 957 times) previous topic - next topic

Jeremy1998

Apr 29, 2010, 05:01 am Last Edit: Apr 29, 2010, 05:04 am by Jeremy1998 Reason: 1
I am so happy :) because this is the first semi-complex project that I did all on my own! Hope you like it!

OK, what this does is fade the led on, wait half a second, fade the led on, wait half a sceond, restart, ect....

You turn the potentiometer to vary the fading speed. This is just another part in my upcoming project. I am also trying to improve this code, and I will replace it, or post it in a reply.

Code: [Select]
int ledPin = 11;    // LED connected to digital pin 11
int potPin = 0;
int val;

void setup()  {
}

void loop()  {
{
val = analogRead(potPin);
val = map(val, 0, 1023, 1, 5);
}
 delay(500);
 for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=val) {
 analogWrite(ledPin, fadeValue);        
 delay(5);    
 }
 delay(500);
 for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=val) {
 analogWrite(ledPin, fadeValue);            
 delay(5);                            
 }
}
 

jon_s

For smoother fading you could make the potentiometer control the delay time while keeping the increment at 1 unit. N.B., I've reversed the map function so that this should (i think) work just like your version:

Code: [Select]

{
 val = analogRead(potPin);
 val = map(val, 0, 1023, 5, 1);
}
 delay(500);
 for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
 analogWrite(ledPin, fadeValue);        
 delay(val);    
 }
 delay(500);
 for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
 analogWrite(ledPin, fadeValue);
 delay(val);                            
 }

Jeremy1998

thank you tons... that makes it much smoother... i will be replacing the code...

Jeremy1998

new code. special thanks to jon_s for makign the update.

Code: [Select]
int ledPin = 11;    
int potPin = 0;
int val;

void setup()  {
}

void loop()  {
{
 val = analogRead(potPin);
 val = map(val, 0, 1023, 5, 1);
 }
 delay(750);
 for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
 analogWrite(ledPin, fadeValue);        
 delay(val);    
 }
 delay(750);
 for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
 analogWrite(ledPin, fadeValue);
 delay(val);                            
 }
}


mroe updates to come to this code, so follow this thread

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy