My next step was to try and address organizing examples and knowledge as it has piled up and is frustrating trying to find solutions. We need a library card catalog equivalent or better.
But first, the examples. I made some changes to BlinkWithoutDelay since I send so many new people to that. BTW, there's 12 actual lines of code in this one.
/* Blink without Delay -- with minor fixes by GFS
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
GFS fixes and modifications -- May 5 2013:
. changed time variables to be ALL unsigned longs, as they should be.
. added UL to numbers being assigned to unsigned longs as should be.
. changed the variable name 'interval' to 'blinkTime' as interval is now a
word used by the IDE, it shows up red (like the word 'if') instead of black.
. changed the if-else logic to change the ledState variable to 1 line XOR logic.
. added comments about adding more tasks to the sketch.
*/
// constants won't change. Used here to
// set pin numbers:
const byte ledPin = 13; // the number of the LED pin
// Variables will change:
byte ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0UL; // will store last time LED was updated
unsigned long blinkTime = 1000UL; // interval at which to blink (milliseconds)
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
// here is where you'd put code that needs to be running all the time.
// GFS adds -- you see the if() { } block below? You can add more blocks
// whether if() or switch-case or whatever to do other tasks and as long
// as they run quick without delays or prolonged loops, your sketch will
// be responsive as if everything runs at the same time.
// just as the blink runs on time, another task can run when a button or
// sensor or serial data comes in or changes.
// simple commands run in less than a millionth of a second so you can pack
// a good bit of process into a block and still run quick. analog read takes
// longer, about 9 per millisecond so it's best not to do a bunch of those
// in a row but instead 1 analog read per time through loop() so other tasks
// can get a chance in between analog reads.
// it's also good to avoid using floating-point as that is slooowww and avoid
// using C++ Strings as they mess with your RAM and suck up CPU cycles doing it.
// Back to the original program:
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
if(currentMillis - previousMillis >= blinkTime) // is it time to change the led?
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = ledState ^ 1; // ^ is logical XOR, true if the values are different
// using logic operations can save a lot of tedious, pain to debug if's
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}