take a look at this single line of code
dbg("1:",inbyte);
This single line of code does three things:
- print a fixed text
- print the name of the variable
- print the value of the variable
In this example dbg("1:",inbyte);
- the fixed text is: "1:"
- the variable-name is "inbyte"
- the value of the variable: will be what you have sent over the serial connecttion
some more examples
let's say a variable with name "myInteger"
int myInteger;
myInteger = -24;
So the command
dbg("blabla", myInteger);
will print to the serial monitor
"blabla" myInteger=-24
let's say a variable with name "OutsideTemperature"
float OutsideTemperature;
OutsideTemperature= readTemperature();
So the command
dbg("HuhU!", OutsideTemperature);
will print to the serial monitor
"HuhU!" OutsideTemperature=5.32
This gives you insight what your code is really doing and what values variables really have
This makes it very easy to analyse code.
This is done by a "thing" called macro.
macros work on a different level than code. You can imagine a macro as a co-worker typing on the keyboard modifying your sourcecode in a programmed way
If take a first look at the macro you will be confused.
Well you have seen example how to use it. (see above)
So using the maro is easy. Understanding needs some time.
The macro that does the above looks like this
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
Here is your demo-code with a second macro that prints only in intervals.
This makes sense if you have a fast running loop where you don't need hundreds of prints every second but only one print per second
Here is a code-version of your code that demonstrates this
use this example code with the serial monitor of the arduino-IDE!
Your python-code is somehow lazy in printing the send back characters.
This is caused by the logic of your python-code not by the computerspeed itslef.
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
String inbyte;
void setup() {
Serial.begin(9600);
Serial.println( F("Setup-Start") );
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
dbgi("top of loop",digitalRead(LED_BUILTIN),3000); // print only once every 3000 milliseconds
if (Serial.available() > 0) {
inbyte = Serial.readStringUntil('\n'); // <<<===== assign the read-in-bytes to the variable
dbg("1:",inbyte);
if (inbyte == "on") {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("led on");
}
if (inbyte == "off") {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("led off");
}
}
}
best regards Stefan