Hi All,
I am sandboxing in the blink without delay example, and I'm getting an output that I didn't expect. Here is the code that works as anticipated (sorry it is slightly insulting, I'm a High School teacher, and this is based on student input):
/*
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
// Character Variables
char charRead;
char pangram_1[] = "The five boxing wizards jump quickly";
char pangram_2[] = "Pack my box with five dozen milk jugs";
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long inputTime = 0;
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
const long patience = 6000; // interval at which the program reprompts for input
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.println("BlinkWithoutDelay_Sandbox.ino");
Serial.println("this program is kinda mean...");
Serial.println("Enter a letter");
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check for user input and mock them
if(Serial.available()) {
inputTime = millis(); // record the time of input
charRead = tolower(Serial.read()); // force lowercase
Serial.write(charRead); // write user input back to serial
if (isAlpha(charRead)) {
switch (charRead) {
case 'a':
Serial.print(" ... ");
Serial.println(pangram_1);
break;
case 'b':
Serial.print(" ... ");
Serial.println(pangram_2);
break;
default:
Serial.println(" ... Well that was a dumb choice.");
break;
}
} else {
Serial.println(" ... That's not even a letter, idiot!");
}
}
unsigned long currentMillis = millis();
if (currentMillis - inputTime >= patience) {
Serial.println("... Waiting on you, dude...");
inputTime = currentMillis;
}
// 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 >= 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);
Serial.println(currentMillis / 1000);
}
}
When I try changing the code so currentMillis is universal and I separate out the code that is running all the time as a stand alone function, everything halts until there is a user input:
/*
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
// Character Variables
char charRead;
char pangram_1[] = "The five boxing wizards jump quickly";
char pangram_2[] = "Pack my box with five dozen milk jugs";
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long currentMillis = millis();
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long inputTime = 0;
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
const long patience = 6000; // interval at which the program reprompts for input
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.println("BlinkWithoutDelay_Sandbox.ino");
Serial.println("this program is kinda mean...");
Serial.println("Enter a letter");
}
void loop() {
do_work();
// 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 >= 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);
Serial.println(currentMillis / 1000);
}
}
void do_work() {
// here is where you'd put code that needs to be running all the time.
// check for user input and mock them
if(Serial.available()) {
inputTime = millis(); // record the time of input
charRead = tolower(Serial.read()); // force lowercase
Serial.write(charRead); // write user input back to serial
if (isAlpha(charRead)) {
switch (charRead) {
case 'a':
Serial.print(" ... ");
Serial.println(pangram_1);
break;
case 'b':
Serial.print(" ... ");
Serial.println(pangram_2);
break;
default:
Serial.println(" ... Well that was a dumb choice.");
break;
}
} else {
Serial.println(" ... That's not even a letter, idiot!");
}
}
if (currentMillis - inputTime >= patience) {
Serial.println("... Waiting on you, kiddo...");
inputTime = currentMillis;
}
}
It seems like it is stuck in the do_work function waiting on an input, but I wonder if anyone can help me understand why this happens and how to anticipate and fix it so I can code better in the future.
Thanks!