Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: December 08, 2012, 08:27:09 am » |
Hello,
First, love the whole Arduino concept. And I also did try various search terms and did read for some little time but didn't find, or missed the answer.
I have as you might have guessed a Leonardo board, it's my first. And it works except for the problem(?) I'll be getting to in a moment, well, right now actually:
I am under the impression that the loop function is supposed to loop indefinitely. However my Leonardo does not, it WILL run the sketch, the loop function will be called but it is as though it runs once, then exits.
I know the Leonardo is different... (which is ok) is this the cause of the loop issue? How is it resolved? Also anything Leonardo specific someone might want to share I am all eyes.
Thanx, and happy to be here.
|
|
|
|
|
Logged
|
|
|
|
|
Canada
Offline
Sr. Member
Karma: 3
Posts: 391
|
 |
« Reply #1 on: December 08, 2012, 12:55:22 pm » |
Can you post your sketch?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #2 on: December 08, 2012, 02:15:45 pm » |
/* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */
int led = 9; // 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
// the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); }
// the loop routine runs over and over again forever: void loop() { // 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 == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(500); }
I am thinking as a work around of adding another function, my own loop function? Lock it in a while loop? And thanx for the relpy.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #3 on: December 08, 2012, 02:23:31 pm » |
It's as if the program sorry, sketch, starts to run... BUT execution gets interrupted because of the serial re-enumeration? As it runs then I get a blink on the rx. tx, lines and execution stops?
|
|
|
|
|
Logged
|
|
|
|
|
Värmland, Sweden
Offline
Full Member
Karma: 7
Posts: 222
|
 |
« Reply #4 on: December 08, 2012, 03:00:08 pm » |
That sketch should work.
Do you have anything else trying to open the serial port? Try running the same sketch while on batteries (without USB) if you can.
What kind of LED are you using on pin 9 and do you have a current limiting resistor in series with the LED?
|
|
|
|
|
Logged
|
|
|
|
|
Saskatchewan
Offline
Full Member
Karma: 10
Posts: 223
When the going gets weird, the weird turn pro. - Hunter S. Thompson
|
 |
« Reply #5 on: December 08, 2012, 03:16:29 pm » |
Put some serial prints at various places to help see where it's stopping.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #6 on: December 08, 2012, 03:36:28 pm » |
Concur the code looks good. When you say execution stops, what do you mean? How do you know it stops?
With delay(500) it's going to take 25 seconds to fade up and another 25 seconds to fade down… are you sure it's not working? A shorter delay may be part of your solution.
Serial print is a good idea.
-br
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #7 on: December 08, 2012, 05:31:00 pm » |
Posted by: Jimmy60 "Put some serial prints at various places to help see where it's stopping."
Ok, serial print? I'll look into how to do that. Print an index or loop count; sounds like a good idea thanks.
Posted by: billroy "With delay(500) it's going to take 25 seconds"
I thought delay() took its parameter in microseconds?
Posted by: perhof "Try running the same sketch while on batteries (without USB) if you can."
A 5 volts phone charger shouldn't hurt it if powered the board from the usb port right?
But the consensus is blink should keep blinking, and you guys gave me some stuff to work on, I'll try it.
Thanx.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #8 on: December 08, 2012, 05:45:13 pm » |
What kind of LED are you using on pin 9 and do you have a current limiting resistor in series with the LED?
On my board its the LED next to the "on" light. Ok. Replaced 500 with 5. And powered it from an external source and it blinks 13 times, and just stops. When plugged into the computer it does the same thing but the rx light blinks and then, it stops.
|
|
|
|
|
Logged
|
|
|
|
|
Värmland, Sweden
Offline
Full Member
Karma: 7
Posts: 222
|
 |
« Reply #9 on: December 08, 2012, 05:48:04 pm » |
Posted by: billroy "With delay(500) it's going to take 25 seconds"
I thought delay() took its parameter in microseconds?
It does, and your code waits 500 ms for every time you increase or decrease your LED level with 5. That means it will take 25 seconds (50 * 500ms) to go from 0 to 255 and another 25 secs to go back to 0. The comment in your code says: wait for 30 milliseconds to see the dimming effect which would have been a better choice. Posted by: perhof "Try running the same sketch while on batteries (without USB) if you can."
A 5 volts phone charger shouldn't hurt it if powered the board from the usb port right?
Should be safe. How well it would work depends on how stable the voltage is.
|
|
|
|
|
Logged
|
|
|
|
|
Värmland, Sweden
Offline
Full Member
Karma: 7
Posts: 222
|
 |
« Reply #10 on: December 08, 2012, 05:50:57 pm » |
On my board its the LED next to the "on" light.
That would be the LED on pin 13 - not pin 9. You probably see the LED flashing when the bootloader starts and then nothing.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #11 on: December 08, 2012, 06:07:30 pm » |
On my board its the LED next to the "on" light.
That would be the LED on pin 13 - not pin 9. You probably see the LED flashing when the bootloader starts and then nothing. That was it. I had switched from the blink example to the fade: blink uses 13, fade uses 9. So on non-leonardo boards pin 9, might have been ok? It's working now though that I switched from pin 9 to 13. So thanks and a million years good luck.
|
|
|
|
|
Logged
|
|
|
|
|
Värmland, Sweden
Offline
Full Member
Karma: 7
Posts: 222
|
 |
« Reply #12 on: December 08, 2012, 06:11:11 pm » |
Uno and older boards also have the LED on pin 13 and no LED on pin 9 but because pin 13 is not a PWM pin on those boards the example is made for an external LED connected to one of the PWM pins.
Glad to hear that you got it working.
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 672
|
 |
« Reply #13 on: December 08, 2012, 06:57:48 pm » |
While you have the code working there is STILL a BUG in it. This line if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ;
only works if fadeAmount is 1 or 5 or 255. It won't work if fadeAmount is any other value, as your using == . Mark PS brightness and fadeAmount should be unsigned byte and not int. It would also help if you check that brightness is in range befor you do the digitalWrite. Mark
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #14 on: December 08, 2012, 10:22:29 pm » |
While you have the code working there is STILL a BUG in it. This line if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ;
only works if fadeAmount is 1 or 5 or 255. It won't work if fadeAmount is any other value, as your using == . Mark PS brightness and fadeAmount should be unsigned byte and not int. It would also help if you check that brightness is in range befor you do the digitalWrite. Mark I had "blink" on the brain there for a sec, and you're right but, its from the example code - sketch, base. They probably only meant it to be, such as it is. So I don't think it is an error per se. More like unoptimized. How could brightness not be in range? Something external to the code? Not as bad idea as a rule though, in any case. Thanx for the reply.
|
|
|
|
« Last Edit: December 08, 2012, 10:30:50 pm by Epoch_Void »
|
Logged
|
|
|
|
|
|