0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« on: March 15, 2011, 12:23:40 am » |
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: #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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #1 on: March 15, 2011, 12:38:45 am » |
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
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #2 on: March 15, 2011, 12:59:14 am » |
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
Offline
Shannon Member
Karma: 116
Posts: 10134
|
 |
« Reply #3 on: March 15, 2011, 01:12:53 am » |
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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #4 on: March 15, 2011, 01:21:18 am » |
Hence my original question of how he had things wired up ... Code is fine, but hardware is questionable ...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #5 on: March 15, 2011, 01:45:49 am » |
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. #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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #6 on: March 15, 2011, 01:51:53 am » |
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: if (x == 14)
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #7 on: March 15, 2011, 01:57:23 am » |
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
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #8 on: March 15, 2011, 02:04:58 am » |
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: #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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #9 on: March 15, 2011, 02:09:20 am » |
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
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #10 on: March 15, 2011, 02:18:06 am » |
thanks. I'm betting you are referring to the delay vs. millis problem, eh? Thanks again for your help!
|
|
|
|
|
Logged
|
|
|
|
|
Colorado
Offline
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #11 on: March 15, 2011, 02:22:05 am » |
Nope. Has to do with your fading LED ...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 17
Arduino rocks
|
 |
« Reply #12 on: March 15, 2011, 02:25:13 am » |
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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #13 on: March 15, 2011, 10:50:32 am » |
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
Edison Member
Karma: 39
Posts: 1207
Reviving dead brain cells with Arduinos.
|
 |
« Reply #14 on: March 15, 2011, 10:52:25 am » |
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
|
|
|
|
|
|