I'm looking to see if there is a way to Pause the Void Loop on an Arduino UNO when I submit a new delay value without using an external button as an interrupt?
For example: If I set the delay for the LED on Pin 8 to blink at 3 minutes on and 3 minutes off, I'll have to wait for the Loop to complete before it would apply any new values sent during the 6 minute or so loop.
Here is the code (combined with a lot of code from contributors here as well) for the project:
#include <EEPROM.h>
#include <avr/eeprom.h>
const byte numChars = 30;
char receivedChars[numChars];
boolean newData = false;
long int redDelayValue = 0;
void setup()
{
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true)
{
parseData();
showParsedData();
long address = 0;
EEPROMWritelong(address, redDelayValue);
address += 4;
}
else
{
digitalWrite(8, HIGH);
delay(EEPROMReadlong(0));
digitalWrite(8, LOW);
delay(EEPROMReadlong(0));
}
newData = false;
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData()
{
char * strtokIndx;
strtokIndx = strtok(receivedChars, ",");
redDelayValue = atof(strtokIndx);
}
void EEPROMWritelong(int address, long value)
{
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long EEPROMReadlong(long address)
{
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16)
& 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void showParsedData()
{
Serial.println(" ");
Serial.print("Red Delay Time (ms): ");
Serial.println(redDelayValue);
}
Thanks