As expected, Blackfin provided a complete solution as is his or her wont, so you can stop reading now if you like. Or, read on:
dougeffle:
I'm having trouble finding anything about coding the bwod stuff into it's own function for each led
Pretty much take the stuff that's in bwod's loop() and put it in its own function, then call that function from loop.
So here is the virgin bwod for reference, some comments removed to save space:
//Blink without Delay
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
Then here, I copied most of loop() into a new function myBWOD(), leaving the line that puts millis() into currentMillis behind in loop(). Also changed it so that currentMillis is declared at the top above setup(). See lines marked XXXXX
//Blink without Delay in a function
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
unsigned long currentMillis = millis(); // new XXXXXXXXXXXXXXXXXXX
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
currentMillis = millis(); //left in loop but doesn't do the declare here any more XXXXXXXX
myBWOD(); // calls the new function XXXXXXXX
}
void myBWOD() { //contains most of the old loop() XXXXXXXXXXXXXXXXXXXXxx
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
dougeffle:
I'm having trouble finding anything about .... calling the functions to set the interval to slow or fast.
Then here, I just added another function to read the button. Button is from pin to ground. Pressed or unpressed gives different blink rate. See lines marked YYYYYYYY
//Blink without Delay in a function and button to change rate
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
long interval = 1000; // interval at which to blink (milliseconds) YYYYYY not const any more, need to be changed
unsigned long currentMillis = millis(); // new XXXXXXXXXXXXXXXXXXX
byte button = 2; // YYYYYYYYYYYYYYY
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT_PULLUP); //YYYYYYYYYYYYYYYY
}
void loop()
{
currentMillis = millis(); //left in loop but doesn't do the declare here any more XXXXXXXX
checkButton(); //YYYYYYYYYYYYYYYYY
myBWOD(); // calls the new function XXXXXXXX
}
void checkButton() //YYYYYYYYYYYYYYY
{
if (digitalRead(button) == HIGH) interval = 1000; // not presssed
else interval = 100; // presssed
}
void myBWOD() { //contains most of the old loop() XXXXXXXXXXXXXXXXXXXXxx
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
That's for one led of course, so you need to double up and have names like button1 and button2 etc.
I'm on my way out right now, Saturday beckons, back in a few hours....