Pages: [1]   Go Down
Author Topic: Random LED fading not working  (Read 893 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-sad

Here's the code I wrote :
Code:
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 ! smiley
« Last Edit: June 11, 2010, 05:37:57 pm by lunavorax » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are at least on the correct path. I did a quick test and it seems to work ok. Here is my code:
Code:
#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);
  
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-wink
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26180
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley

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

Logged

Greenwood, Indiana
Offline Offline
God Member
*****
Karma: 0
Posts: 508
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 12, 2010, 11:56:18 am by mrmeval » Logged

If it was designed by man it can be repaired by man.

Pages: [1]   Go Up
Jump to: