Go Down

Topic: Issue with the PWM Fade code. (Read 444 times) previous topic - next topic

Zakari

Dec 04, 2012, 06:36 pm Last Edit: Dec 04, 2012, 06:39 pm by Zakari Reason: 1
Hello, Ive been working on a project to get some RGBs to respond to an ultrasonic sensor, and following advice to change from a for/delay system to a millis based one, I have decided to go back to basics. The code I wrote to fade the led with millis didnt work, so I tried to do it as the tutorial says using delay, jsut to try and track down where is going wrong.
The tutorial example works fine ofcourse, and my slightly modified version to work with a comman anode RGB also works fine(I have the brightness led attached to the anode, and the red cathode attached to ROne, tied LOW. This is so I can fade all 3 colour with 1 command rather than trying to balance colours and brightness at the same time.)  But I dont want the LED to fade right down to nothing, and back up, I only wish it to dim slighlty.

Code: [Select]
int led = 3;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by
int ROne = 11;

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(ROne, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  digitalWrite(ROne, LOW);
  // set the brightness of pin 9:
  analogWrite(led, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 200 || brightness == 255){                             //NOTE the modification here
    fadeAmount = -fadeAmount ;
  }     
 
  delay(50);



So I adjusted the range the brightness has to change by, so that it stays mostly on. But the result is unexpected, instead of dimming a little and fading back up, it dims fully then jumps back to full. It doesnt matter which way I adjust the range, it does the same thing enless i is set to 0 and 255.

So:
1.  Does this make sense to anyone?
2. It is possible to only partially fade a LED right? So am I doing it wrong?
3. Can anyone suggest a good way to do it without using a For loop?

Cheers guys!

Grumpy_Mike

Quote
Does this make sense

No.
You use
Code: [Select]
// reverse the direction of the fading at the ends of the fade:
  if (brightness == 200 || brightness == 255){                             //NOTE the modification here
    fadeAmount = -fadeAmount ;
  }     

To change the direction of the fade at the top end but you have nothing to change the direction at the bottom end.

Quote
Can anyone suggest a good way to do it without using a For loop?

Yes use the millis() timer.

Quote
The code I wrote to fade the led with millis didnt work,

So that means you didn't write it correctly. It is this code that you should be posting and asking help with.

Go Up