This project is "much more lame" than my True Arduino Multithreading... check that outhere.
I love Arduinos. They are on almost all of my projects. However, running code anything but sequentially takes
careful manipulation of program flow. Arduino Timed Events essentially replicates the JS event loop by allowing users to schedule
code that should be run at a certain time. I had a blast making it, so I am putting it here.
The code keeps a list of scheduled Tasks, which can either be scheduled to run once, at an
interval, or anytime a boolean condition is true. It doesn't utilize timer interrupts, so if you schedule code
that includes a delay and that code is called, the program will hang until the code is completed. Missed events
will run after that event, however.
Check out the github for an usage
guide. Here is some sample code. Note the use of function pointers, lambdas and capturing lambdas. Memory
de-allocation is handled for you in the case of the capturing lambda.
#include<MemoryFree.h>#definepl(x)Serial.println(F(x));#definept(x)Serial.print(F(x));#definepSize(x){Serial.print(F("sizeof("#x));Serial.print(F(") = "));Serial.println(sizeof(x));}#include<Tasks.h>#definePIN2voidfm(){pt("Free Memory = ");Serial.println(freeMemory());}voidsetup(){//Required setuppinMode(PIN,INPUT_PULLUP);Serial.begin(115200);//Setup something that prints the free memory every secondsetInterval(fm,0,1000);pt("Starting free memory: ");fm();// Add an event listener that should run bytex=addEventListener([](){Serial.println("Pin is high!");delay(100);},[]()->boolean{returndigitalRead(PIN);});// Cancel the event listener using the ID of the old event listener after 20 secondssetTimeout(newauto([=](){pl("Stopping event listener");deleteTask(x);}),20000);pt("After setup: ");fm();}voidloop(){runTasks();}
Go To TopGuess what? I made this website! Check it and many of my other projects on my github.