at least in the context of the code you posted, why is there a need for it to be non-blocking? you're calling a sub-function one time expecting it to return the # of active LEDs
in this context, this is how i would write that sub-function
char s [80];
// -----------------------------------------------------------------------------
void
ledWriteCode (
int code )
{
Serial.print (" ledWriteCode: ");
Serial.println (code);
}
// -------------------------------------
int
measureCurrent (void)
{
return random (250, 800);
}
// -------------------------------------
#define EepromNledAddr 123
void
save (
unsigned addr,
byte val )
{
sprintf (s, "save: addr %d, val %d", addr, val);
Serial.println (s);
}
// -----------------------------------------------------------------------------
#define CheckLedsDelay 250
#define MinLedCurrent 300
#define LedCode0 405
#define LedCodeN 500
#define LedCodeDel 5
int
checkLeds (void)
{
Serial.println ("checkLeds:");
unsigned n = 0;
for (unsigned code = LedCode0; LedCodeN >= code; code += LedCodeDel, n++) {
ledWriteCode (code);
delay (10);
int cur = measureCurrent ();
sprintf (s, " checkLeds: %d, code %3d, cur %3d", n, code, cur);
Serial.println (s);
if (MinLedCurrent > cur)
break;
}
return n;
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (115200);
int nLeds = checkLeds ();
save (EepromNledAddr, nLeds);
}
void
loop ()
{
}
but i thought you said this feature, determining the # of LEDs, is actually driven by some other processor (e.g. PC) sending LED codes. in this context, a different approach is needed
i was going to say there needs to be a cmd to enter a mode for determining the # of LEDs available, but that mode would be on the sending processor (PC). the esp would only need to recognize a command to set the LED code, measure the current and return either the value of the measured current or a boolean that it exceeds some minimum
here's another approach based of you post #19
Output:
cmdProcess: ledCurrent 405
ledWriteCode: 405
ledCurrent - code 405, cur 557
cmdProcess: ledCurrent 415
ledWriteCode: 415
ledCurrent - code 415, cur 449
cmdProcess: ledCurrent 410
ledWriteCode: 410
ledCurrent - code 410, cur 723
cmdProcess: junk
cmd unknown
Code:
char s [80];
// -----------------------------------------------------------------------------
void
ledWriteCode (
int code )
{
Serial.print (" ledWriteCode: ");
Serial.println (code);
}
// -------------------------------------
int
measureCurrent (void)
{
return random (250, 800);
}
// -------------------------------------
#define EepromNledAddr 123
void
save (
unsigned addr,
byte val )
{
sprintf (s, "save: addr %d, val %d", addr, val);
Serial.println (s);
}
// -----------------------------------------------------------------------------
#define CheckLedsDelay 250
#define MinLedCurrent 300
#define LedCode0 405
#define LedCodeN 500
#define LedCodeDel 5
int
checkLeds (void)
{
Serial.println ("checkLeds:");
unsigned n = 0;
for (unsigned code = LedCode0; LedCodeN >= code; code += LedCodeDel, n++) {
ledWriteCode (code);
int cur = measureCurrent ();
delay (10);
sprintf (s, " checkLeds: %d, code %3d, cur %3d", n, code, cur);
Serial.println (s);
if (MinLedCurrent > cur)
break;
}
return n;
}
// -----------------------------------------------------------------------------
void
cmdProcess (
char *buf )
{
sprintf (s, "%s: %s", __func__, buf);
Serial.println (s);
char cmd [20];
int val;
int n = sscanf (buf, "%s %d", cmd, &val);
if (2 == n) {
if (! strcmp(cmd, "ledCurrent")) {
ledWriteCode (val);
delay (10);
int cur = measureCurrent ();
sprintf (s, " ledCurrent - code %3d, cur %3d", val, cur);
Serial.println (s);
return;
}
// of course there can be other commands
}
sprintf (s, " cmd unknown");
Serial.println (s);
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (115200);
}
// ---------------------------------------------------------
char buf [80];
void
loop ()
{
if (Serial.available ()) {
int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
cmdProcess (buf);
}
}