Go Down

Topic: Random LED fading not working (Read 1 time) previous topic - next topic

LunaVorax

Jun 12, 2010, 12:36 am Last Edit: Jun 12, 2010, 12:37 am by lunavorax Reason: 1
Hello everyone !

My new project was a simple fading LED (as in Exemples > Analog > Fading) but with a random value chosen each time for fading in and out delay.
(And another "sleeping" delay.)

I've written what I though to be a matching code for my idea but now instead of a random fading LED I got a random blinking LED.
The LED turns randomly on and off at full brigthness without fading. :-?

After a while of code tweaking I still don't know where the problem is and the issue still remain the same. :(

Here's the code I wrote :
Code: [Select]

int ledPin = 9;

void setup() {}

void loop() {
 int x = 0;
 x = random(30, 300);
 
 int y = 0;
 y = random(30, 300);

 for(int fadeValue = 0 ; fadeValue <= 255; fadeValue++) {
   analogWrite(ledPin, fadeValue);
   delay(x);
 }

 for(int fadeValue = 255 ; fadeValue >= 0; fadeValue--) {
   analogWrite(ledPin, fadeValue);
   delay(y);
 }
 
 //Sleeping
 int z = 0;
 z = random(30, 300);
 delay(z);
}


Thank you very much in advance for your answers ! :)

artjumble

You are at least on the correct path. I did a quick test and it seems to work ok. Here is my code:
Code: [Select]

#define LED1 3

void setup()
{
 pinMode(LED1, OUTPUT);
}

void loop()
{
 int fadeUpDelay = random(10, 60);
 int fadeDownDelay = random(10, 60);
 
 for(int c=0; c < 255; c++)
 {
   analogWrite(LED1, c);
   delay(fadeUpDelay);
 }
 
 for(int c=255; c > 0; c--)
 {
   analogWrite(LED1, c);
   delay(fadeDownDelay);
 }
 
 delay(10);
 
}


LunaVorax

Hi artjumbie !

Since your code doesn't seem to be very different from mine, I did not understood what was fixed from my code to your code (I still see no difference).
However, the fact that you used a #define instead of a regular int catched my eye so even if I didn't knew what was really special about it.

Surprisingly, your code worked. But it appears that it wasn't because your code was better, but because you used pin 3 to run the code.
I have been running my code for an LED connected to pin 2 all the time and it wasn't working. Your code also don't work when LED1 is changed to 2 (and when the LED is also connected to pin 2 of course).

Now my code work when the LED is connected to another pin.

So the error was only a question of pins... Now I wonder why pin 2 doesn't seems to work like others.

Anyway, thanks for your help artjumbie, you made my day. ;)

AWOL

Quote
Now I wonder why pin 2 doesn't seems to work like others


Because PWM (which is what "analogWrite" does) is onl available on certain pins, depending on which processor you are using..
Check the reference:
http://arduino.cc/en/Reference/AnalogWrite
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

LunaVorax

Oh darn ! So it means I can't do it for 8 LEDs at the same time but only 6 :/
Oh well, I'll see how it goes.

Thank you again for this information !

artjumble

Quote
Surprisingly, your code worked. But it appears that it wasn't because your code was better, but because you used pin 3 to run the code.
I have been running my code for an LED connected to pin 2 all the time and it wasn't working. Your code also don't work when LED1 is changed to 2 (and when the LED is also connected to pin 2 of course).


I had suspected it might be the pin # you were using, but I already had an LED setup to test with so I figured I would write the code.  :)

Quote
Oh darn ! So it means I can't do it for 8 LEDs at the same time but only 6 :/

I believe there are two basic ways around this limitation. Charlieplexing and Multiplexing


mrmeval

#6
Jun 12, 2010, 06:55 pm Last Edit: Jun 12, 2010, 06:56 pm by mrmeval Reason: 1
HEF4794B will allow you to control 8 outputs with PWM. It's a higher current version of the 74HC595

You wire the outputs to 8 LEDs, the inputs are from ordinary digital pins on the arduino, then wire a pwm pin to the enable output pin.

This will dim all of the LED's. You can write code to set the PWM to a value and then turn on an LED at that value then turn it off and go to the next one.  

You can also tie the enable output pin high and fake a PWM in your code by rapidly turning each LED on and off.
If it was designed by man it can be repaired by man.

Go Up