Simple Fading Code

ok, iv only had my arduino for about 2 months now and iv made around 10, 15 little arduino projects that i find on the net, all i have to do is copy and past the code, and follow the schematics.

BUT

i want to start learning to write my own, and built a thing for my bed, with about 6 blue LED's

iv used the code that makes it fade in and out, and boy dose it look awsome in my bedroom, but i would like to have a little more controll over this with out having to plug it into my computer to change the speed of the fading in and out.

so i tryed to merge the Fading source code ;

with the AnalogInput source code;

and heres what i got

 int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 9;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor

 void setup()  { 
   // nothing happens in setup 
 } 

 void loop()  { 
   // fade in from min to max in increments of 5 points:
   for(int fadeValue = 0; fadeValue <= 255; fadeValue +=analogRead(sensorPin); ) { 
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);         
     // wait for 30 milliseconds to see the dimming effect    
     delay(30);                            
   } 

   // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; analogRead(sensorPin);) { 
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);         
     // wait for 30 milliseconds to see the dimming effect    
     delay(30);                            
   } 
 }

can some one please tell me what iv fucked up ?

I know its a very simple arduino project, but yer.
i hope some one can please help

first of all you need to put the analog value from the pot in the delay function and not the loop.
Now the pot will give you a control range from 0-1023, and this value will go to the delay.
So it needs to look like this :
delay(analogRead(sensorPin));

you can use the map function to set a different range of values for the delay, just go to the reference page and read about it.

Thank you for the quick responce.
but im still new and what you have explained made little scence to me.

can highlight in my code what you have changed ?
and maybe state why ?

sorry, but i really am grateful

i re read what you said, and i now get what you ment.

  // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);         
     // wait for 30 milliseconds to see the dimming effect    
    [glow](delay(analogRead(sensorPin));  [/glow]                          
   } 
 }

but i still get an error; "In function 'void loop()':
error: expected `)' before ';' toke"

(delay(analogRead(sensorPin));
^
This is unnecessary and unmatched.

Ok so here is what you need to change:

 int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 9;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor

 void setup()  {
   // nothing happens in setup
 }

 void loop()  {
   // This part need to stay as it was in the original code
   for(int fadeValue = 0; fadeValue <= 255; fadeValue +=5 ) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
     delay(analogRead(sensorPin));
   }

   // fade out from max to min in increments of 5 points:
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
     // sets the value (range from 0 to 255):
     analogWrite(ledPin, fadeValue);
     // sets the delay with the pot value
      delay(analogRead(sensorPin));
   }
 }

you may want to use the map function because if you set the pot to it's max it will take more than a minute to fade in and a minute to fade out.
and this should look like this

int x = analogRead(sensorPin);
x = map(x, 0, 1023, 0, 500); 
delay(x);

If you want to learn about the map function go here:http://arduino.cc/en/Reference/Map

Thank you so much |systemx !

:slight_smile:

you ever need a flash animation, flash banner, or anything to do with flash just let me know :slight_smile:

Happy to help man

|systemx ; check it out :slight_smile: How To - LED Glow/neon sticks - PART 3 UPDATED!! - YouTube

LOL :smiley: man great job looks nice.
You can try later do this with RGB LEDs, it's going to look sick.