0
Offline
Newbie
Karma: 0
Posts: 47
Bald-Headed, Middle-Aged Nerd
|
 |
« on: February 19, 2013, 10:19:02 am » |
The single line of code in the loop below flashes a LED without use of delay(). Only issue I see is that pin 9 is being written to on every loop cycle. Is this a bad thing to be doing ? Thank you. BobW void setup() { pinMode(9, OUTPUT); // LED on pin 9 pinMode(8, OUTPUT); // virtual ground for LED digitalWrite (8, LOW); // virtual ground for LED }
void loop() { digitalWrite ( 9, (millis()%500 < 250) ? 1 : 0 ); // flash LED at 2Hz
// code that runs all the time goes here }
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2495
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #1 on: February 19, 2013, 10:48:55 am » |
The single line of code in the loop below flashes a LED without use of delay(). Only issue I see is that pin 9 is being written to on every loop cycle. Is this a bad thing to be doing ?
Not necessarily a bad thing, was there a specific concern you had in mind?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 47
Bald-Headed, Middle-Aged Nerd
|
 |
« Reply #2 on: February 19, 2013, 11:08:35 am » |
Jack:
One thought was: does the memory I'm writing to so many times have a life span to it ? Doesn't flash memory have a limited number of writes in its estimated lifetime before it goes bad ?
BobW
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: February 19, 2013, 11:13:17 am » |
You're not writing to flash memory, you're writing to a register.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6368
-
|
 |
« Reply #4 on: February 19, 2013, 12:32:56 pm » |
It does what you want without any side effects that I can see, so there's nothing wrong with that as a solution. Explicitly comparing the result of millis() against a threshold gives you much more flexibility, but it's flexibility that you don't need in this simple example.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2495
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #5 on: February 19, 2013, 12:51:03 pm » |
According to the datasheet, the flash memory that holds the program is good for 10,000 write/erase cycles, so that puts an upper limit on the number of times the part can be programmed. But once the program is running, nothing will "wear out". (The EEPROM memory, used for non-volatile data storage has an endurance of 100,000 write/erase cycles.) "Regular" variables declared in the code usually go into the static RAM (SRAM), which has no endurance limits.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 47
Bald-Headed, Middle-Aged Nerd
|
 |
« Reply #6 on: February 19, 2013, 09:07:14 pm » |
OK. I understand. No practical limit on reading or writing with SRAM.
I'm still weak on understanding differnce in purpose of Flash vs EEPROM. Both non-volatile. Bootloader and sketch stored in Flash. What's EEPROM for then ? Why couldn't non-volatile data be stored in flash ? Why two kinds of non-volatile memory ?
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15316
Measurement changes behavior
|
 |
« Reply #7 on: February 19, 2013, 09:15:45 pm » |
OK. I understand. No practical limit on reading or writing with SRAM.
I'm still weak on understanding differnce in purpose of Flash vs EEPROM. Both non-volatile. Bootloader and sketch stored in Flash. What's EEPROM for then ? Why couldn't non-volatile data be stored in flash ? Why two kinds of non-volatile memory ?
EEPROM is for user only data, used for say storing configuration data to be used when a sketch starts up depending on say a switch input being on or off on startup. It's simply a user scratch pad type memory to use or not use as you please. Useless to many applications, indispensable for other applications. Flash memory can't be (easily?) written to once the sketch has started, only the bootloader code or ISP programming (and a few other hardware programmer methods) can write to flash memory. The user can read or write to EEPROM anytime they wish. Lefty
|
|
|
|
« Last Edit: February 19, 2013, 09:19:09 pm by retrolefty »
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2495
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #8 on: February 19, 2013, 09:18:37 pm » |
OK. I understand. No practical limit on reading or writing with SRAM.
I'm still weak on understanding differnce in purpose of Flash vs EEPROM. Both non-volatile. Bootloader and sketch stored in Flash. What's EEPROM for then ? Why couldn't non-volatile data be stored in flash ? Why two kinds of non-volatile memory ?
Good questions. Executable code/program/sketch can only run from flash. NV data can be stored in flash, there is a modifier called PROGMEM for this purpose, see http://www.arduino.cc/en/Reference/PROGMEM and http://playground.arduino.cc/Learning/MemoryAlso note the F() function which is a more convenient way to store strings in flash with PROGMEM. Data stored in flash with PROGMEM is pretty well fixed (just like program code), where EEPROM can be changed while the sketch runs, and it will retain the data even when power is removed. SRAM must be powered to retain data.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10148
|
 |
« Reply #9 on: February 20, 2013, 12:21:10 am » |
Is this a bad thing to be doing ? At the point millis wraps, the code will not work as expected. The most extreme outcome is that the LED will stay on or off for a bit less than 500ms. You will have to decide if that constitutes a "bad thing". The other potential downside is that some of the long integer helper functions have to be included. For a memory constraint application that can be a problem. For most applications, it makes no difference.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2914
I only know some basic electricity....
|
 |
« Reply #10 on: February 20, 2013, 01:04:39 am » |
From the 328 (and family) datasheet: – Write/Erase Cycles: 10,000 Flash/100,000 EEPROM If you could live with powers of 2 -1 then how about masking millis? digitalWrite ( 9, ((millis() & 0x1FF) < 255) ? 1 : 0 ); // flash LED at 2Hz
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 47
Bald-Headed, Middle-Aged Nerd
|
 |
« Reply #11 on: February 20, 2013, 09:28:33 am » |
OK. Good.
EEPROM useful because it can be changed while sketch running. Got it.
Also - I'm just using the flashing LED as an annunciator for an alrarm condition. Blinking draws attention better than steady state ON. Overflow thus is not an issue for me.
Finally, Re Powers and Masking - I like that better than what I did. Will use this instead.
The reason I was interested in a simpler way to flash the LED was just to avoid having to deal with the logic and curly brackets that go with a *proper* "blink without delay".
I can keep this line "in stock" now and paste it in the future when I need this functionality.
I'm 100% good to go now !
Thank you all.
Bob W
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6368
-
|
 |
« Reply #12 on: February 20, 2013, 11:26:03 am » |
The reason I was interested in a simpler way to flash the LED was just to avoid having to deal with the logic and curly brackets that go with a *proper* "blink without delay".
I'd regard your solution as functional but obscure - the conventional approach is clearer (IMO) and more flexible. If reducing the number of curly brackets in your code was the main motivation for taking this approach, I suggest that's not a good reason.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2914
I only know some basic electricity....
|
 |
« Reply #13 on: February 20, 2013, 11:30:11 am » |
I think it'd be slightly faster with re-ordered logic too, maybe as much as a usec, or half-usec?
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2495
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #14 on: February 20, 2013, 11:39:13 am » |
I was also going to make the point that digitalWrite() is not without its overhead, so calling it only when needed would be more efficient. Having said that, I'm sure that in the vast majority of cases it makes no difference whatsoever. Still, I'd be inclined to write a function, or use one of the various libraries, e.g. #include "Timer.h" //http://www.doctormonk.com/2012/01/arduino-timer-library.html
#define LED LED_BUILTIN
Timer t;
void setup() { pinMode(LED, OUTPUT); t.oscillate(LED, 1000, LOW); }
void loop() { t.update(); }
|
|
|
|
|
Logged
|
|
|
|
|
|