aurelius:
This is exactly what I've been looking for, but there are some things that I need some clarification on:
void ABC()
{
byte value = readsensor();
send(value);
}
It seems the function ABC() has a locally declared variable "value" that is updated by calling an external function: readsensor().
Am I correct there?
Yes;
If this is the case, sending one of those "CMD"s from the master to the slave might be a good way to initiate a state change on a pin from low to high, provided it is part of a function like readSensor(), right?
Yes;
Or you just say one of these
digitalWrite(PIN, ! digitalRead(PIN)): // toggle
digitalWrite(PIN, HIGH):
digitalWrite(PIN, LOW);
or a 1 ms pulse
digitalWrite(PIN, HIGH):
delay(1);
digitalWrite(PIN, LOW);
or
if (analogRead(A0) > 314) digitalWrite(PIN, HIGH):
else digitalWrite(PIN, LOW);
Anything can be in a commandHandler
Essentially, the master could send a slave a command from a list of functions the slave has, then call for that slave to execute the function corresponding to the command it just declared, whatever it entails.
Yes, one step further you even can give parameters with the command, e.g. how lon a pulse should take or the precision of a sensor...
If this is the case, I'm sitting pretty good. Still, how would something like an int or a float (2 and 4 bytes, respectively for an Arduino other than the Due) be transmitted from the variable "value" on the slave to the master? Do you need to transmit strictly bytes, or could the slave define "value" to be something like an int, float, unsigned long, etc.. and get passed an int/float/unsigned long from whatever function a slave's handler function calls?
A float is just 4 bytes in memory, the standard trick for this is using a union which maps an array of 4 bytes and a float on the same memory. You fill in the float and send the 4 bytes. On the receiving edge you do similar. receive 4 bytes and use the float.
Perhaps more importantly, if I'm anywhere close to being on the right track here, how do you go about passing any information from the function called in the handler function to the handler? Is a local variable somehow nested? i.e. If the term used for a variable agree between the function called within the handler, and what the handler ends up sending, is there anything the function called needs to do to pass that information to the handler function, other than hold it's own copy of that variable?
Just use a global variable or pass a variable by reference.