Hi Sam,
If you would like to practise some coding and learning how to debug with the serial monitor you can play with this code-example
This demo-code incorprates two different things:
it shows how timed actions can be coded in a non-blocking way
The command delay() is blocking which is "the bad way" to code timed actions
The code demonstrate the use of two macros that save typing two lines of code that can be added to the code for debugging purposes. instead of typing three lines of code you type a single line of code.
The demo-code shows how fast the main-loop can execute which is nescessary to do
for all things that must be checked thousands of times each second.
And how to realise actions that are executed only after a longer period of time.
This code requires nothing but any kind of pure ESP32-board (or any other microcontroller-board) with an USB-to-serial-interface
Here is the demo-code
// 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
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
//helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long myFastCounter;
unsigned long mySlowCounter;
void setup() {
Serial.begin(115200);
Serial.print( F("\n Setup-Start \n") );
myFastCounter = 0;
mySlowCounter = 0;
}
void loop() {
myFastCounter++;
// loop is running fast but only once per second (1000 milliseconds)
// the value myFastCounter is printed to the serial monitor
dbgi("dbgi-demo",myFastCounter,1000);
// non-blocking timing-function
// evaluates true only if milliseconds given as the second parameter
// have passed by
if (TimePeriodIsOver (MyTestTimer,2000) ) {
mySlowCounter++;
dbg("dbg-Demo",mySlowCounter);
}
// if you un-comment the line below. The serial monitor will be flooded
// with messages
//dbg("dbg-flooding-demo",mySlowCounter);
// this demonstrates why dbgi is the better choice in fast running loops
}
and here is a link that explains how the macros work and what they are
The name "ESP32devC" does not narrow down to one particular board.
There are ESP32devC v1, ESP32devC v2m ESP32devC v3, ESP32devC v4
ESP32-DevKitC-32E, ESP32-DevKitC-32UE, ESP32-DevKitCVE, ESP32-DevKitCVIE, ESP32-DevKitCS1
Does your ESP32-board have a blue LED? If yes
Of course you are free to follow this suggestion or not:
As an exercise to learn coding you could modify the code above to add a function that does blink
this blue LED (which is connected to IO-pin 2 in most cases)
To make this blue LED blink beeing ON 500 milliseconds / beeing OFF 500 milliseconds in parallel to the other stuff that the demo-code already does.
Whenever a question arises regardless what the question is. Just post the question in the forum
together with your actual full sketch
best regards Stefan