Problem with fading LEDS

Hi guys,

I just got my arduino in the mail today and am trying to make a simple circuit that fades from one led to the other using a potentiometer.

I'm confused because one led is fading, while one just turns on and off. Heres my code:

int potPin = 0;
int redPin = 4;
int greenPin = 5;
int val = 0;
int rval, gval;
int sensorMax = 1023, sensorMin = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  val = analogRead(potPin);
  rval = map(val, sensorMin, sensorMax, 0, 255);
  gval = map(val, sensorMin, sensorMax, 255, 0);
  analogWrite(redPin,rval);
  analogWrite(greenPin,gval);
  delay (50);
}

What I get as a result is green will fade up and down correctly, but the red led will jump to full brightness if its past half way, or jump to off. Any ideas?

Thanks!

Pin 4 is not a PWM pin.

http://arduino.cc/en/Reference/AnalogWrite

Thank you! This is all still very new to me. No clue what PWM is, looking it up now.

Thanks again!!

Sorry, PWM is pulse-width modulation (fixed frequency, variable duty cycle square wave), which is what an "analogWrite" sets up, but it's only available on certain pins.

I have another basic question and don't want to waste a thread.

How do you go about switching projects? Ie, if your old program is on the arduino, it starts running right away as soon as its plugged in. I would imagine this could cause problems if I change pins and such before loading the new code. Is there a way to clear the arduino or stop it from running the program while you prepare to upload the new code?

Thanks!

This is not usually a problem but here are some things you can do:

  • don't power it until you have rewired for the new project and are ready to upload the new code. If you start the upload immediately after applying power to the Arduino the old code will not run.

  • or if you really think that accidently running the new code with the old wiring will be a problem you could burn the new sketch after you have disconnected the old wiring and before you connect up the new.

Have fun!

Thanks mem!

Hi Phil. H , mem and AWOL

I am also having problem with my fading LED. I would like to fade many LED at the same time. My code works perfectly with a 4 Led and a resistance of 100ohms.

But when I plug A LED strip ribon of 34 LED, (3LED/resistance of 131ohms) it doesn’t work :-[ The global intensity of that isn’t very high : 170mA.

However I’am sure I can make some fade in/out with even more LED.

Does anyone has an idea for this issue? :-/
Do I need to install another electronic piece to my circuit?

Here is my code:

Thanks
Hard-oui-no

// Fading LED

int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
//int ledpin6 = 6 // light connected to digital pin 6
void setup()
{
// nothing for setup
}

void loop()
{
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30);
if ( value == 254 ) {

analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(5000);

} // waits for 30 mili seconds to see the dimming effect
}
for(value = 255; value >=0; value-=1) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30);
// ledpin6 2e caisson
void loop()
{
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin6, value); // sets the value (range from 0 to 255)
delay(30);
if ( value == 254 ) {

analogWrite(ledpin6, value); // sets the value (range from 0 to 255)
delay(5000);

} // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=1) // fade out (from max to min)
{
analogWrite(ledpin6, value);
delay(30);
}
}

I hope you're not trying to drive the whole lot directly from the Arduino.

Please tell me you're not - the Arduino / AVR is limited to 40mA per pin.