Pages: [1]   Go Down
Author Topic: Change Led Fade Speed With Potentiometer  (Read 836 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am so happy smiley 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:
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);                            
  }
}
 
« Last Edit: April 28, 2010, 10:04:07 pm by Jeremy1998 » Logged

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
{
  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);                            
  }
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Pages: [1]   Go Up
Jump to: