Go Down

Topic: problem fading and blinking leds simultaneously with millis (Read 1 time) previous topic - next topic

erinbanwell

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: [Select]
#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);
}

KirAsh4

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

erinbanwell

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!

Coding Badly

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.

KirAsh4

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

erinbanwell

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: [Select]
#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!

KirAsh4


Code: [Select]
  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: [Select]
if (x == 14)

erinbanwell

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?

erinbanwell

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: [Select]
#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);
}

KirAsh4

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.

erinbanwell

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

KirAsh4


erinbanwell

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?

KirAsh4

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?

KirAsh4

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.

Go Up