Pages: [1] 2   Go Down
Author Topic: problem fading and blinking leds simultaneously with millis  (Read 1481 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have LED1 fading on and off with millis, and LED2 blinking with millis. There are not any delays, but when LED2 blinks ON, LED1 gets slightly dimmer, and when LED2 blinks back off, LED1 gets brighter. Is this something in my code? I suppose it could be a hardware issue, but I think it might be this code (which I borrowed from someone online).
heres my code:
Code:
#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 6000  // Fade up and down every 2 seconds

int ledPin = 5;
int pwmPin = 10;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
}

void loop()
{
  unsigned long time = millis();

  // Blink the LED
  if (time % BLINK_RATE > BLINK_RATE/2)
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);

  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just tried your code on my Uno and I can't reproduce what you're seeing.  How do you have these wired up?  Are you sharing the resistor for both LEDs (the only way I was able to get what you're describing ...)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh! i actually didnt use any resistors. If I add resistors, do you think that will solve it? I'll try that and see what happens. Thanks!
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 210
Posts: 13031
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I add resistors, do you think that will solve it?

Doesn't matter.  If you continue to leave off current limiting resistors you will permanently damage your board.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hence my original question of how he had things wired up ...  Code is fine, but hardware is questionable ...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The resistor was indeed the problem(it doesnt say "newbie" by my name for nothing). Sorry bout the hardware issue on the software forum.
This code works.
Now I'm trying to alter this code to blink sequentially through 11 LEDs while fading, instead of blinking just one LED.
My attempt gave me no blinking LEDs.
Code:
#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 5000  // Fade up and down every 2 seconds

 int x = 0;

int pwmPin = 10;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}

void loop()
{

  unsigned long time = millis();

  // Blink the LED
 
    if (time % BLINK_RATE > BLINK_RATE/2)
      digitalWrite(x, HIGH);
    else
      digitalWrite(x, LOW);
  x = x+1;
  if (x = 14)
  x = 0;
  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}

thanks again!
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  x = x+1;
  if (x = 14)
  x = 0;
thanks again!
You are setting the value of x to 14.  I believe you meant to compare it which means:
Code:
if (x == 14)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks KirAsh4!
 Now I'm getting all the blinking LED's blinking together instead of sequentially. Should I put it in a "for" statement? If so, where would I put it?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

UPDATE:
I tried using a for statement but got the same problem (all LEDs besides the fading one are blinking together instead of sequentially.
heres that code:
Code:
#define BLINK_RATE 1000 // On/off blink every 1 second
#define FADE_RATE 5000  // Fade up and down every 2 seconds

 int x = 0;

int pwmPin = 10;

void setup()
{

  pinMode(pwmPin, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
}

void loop()
{

  unsigned long time = millis();

  // Blink the LED
 for (x = 0; x <= 13; x++){
    if (time % BLINK_RATE > BLINK_RATE/2)
      digitalWrite(x, HIGH);
    else
      digitalWrite(x, LOW);
 
  if (x == 14)
  x = 0;
 }
  // Synthesize a sawtooth wave: a value from 0 to FADE_RATE/2 and back down
  long val = labs((FADE_RATE / 2) - (time % FADE_RATE));
  val = 255 * val / (FADE_RATE / 2); // convert to range 0..255..0
  analogWrite(pwmPin, val);
}
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, they're not blinking together.  However, because you're turning them on and 1ms second you're turning them off again, it appears as if they're all blinking together.  Trust me, they're not.  One simple way of testing that is to add a delay(50) before your if statement.

And by doing that, you will also discover another problem, which I will leave for you to find.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks. I'm betting you are referring to the delay vs. millis problem, eh? Thanks again for your help!
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope.  Has to do with your fading LED ...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what i mean is, if I add delay the fade gets all chopped up, so I need to work out a millis timer instead of delay. correct?
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, the delay nor millis() has nothing to do with it getting chopped up.  Think about it for a moment.  You are using pin 10 for the fade, and you are cycling from 0 to 13 to turn LEDs on/off ...  0 to 13 ... pin 10 ... pin 10 ... 0 to 13 ...  Write it out if you don't quite see it.  Start with x == 0.  Your loop goes 0, 1, 2, 3 ... 8, 9, ??  What just happened?
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not trying ot be a PITA here, but I don't want to just hand you the answer either.  This is how you learn (at least I do), by figuring out where my mistakes are.  In your case, just step through your loop one by one, and hopefully you'll understand why your fade on pin 10 gets all chopped up.  It's not that hard.  If you really can't tell, I'll try to explain it.
Logged

Pages: [1] 2   Go Up
Jump to: