Evaluate math expressions from strings at runtime no dependencies, no heap beyond the parser itself.
MathParser lets your Arduino evaluate a math expression stored as a string at runtime. Instead of hardcoding y = x * 1.8 + 32, you store "x * 1.8 + 32" in EEPROM and evaluate it dynamically. Change the formula without reflashing.
Real-world use cases:
- ๐ก๏ธ Sensor calibration curves stored in EEPROM/SPIFFS
- โ๏ธ Config-driven thresholds and formulas
- ๐ฎ User-entered expressions via Serial or touchscreen UI
- ๐ง Dynamic PID tuning formulas
- ๐ Unit conversions defined at runtime
- ๐ค Joystick / motor input shaping with custom functions
| Category | Examples |
|---|---|
| Operators | + - * / ^ % |
| Grouping | ( ) |
| Functions | sin cos tan asin acos atan atan2 sinh cosh tanh |
sqrt cbrt pow exp log log2 log10 |
|
abs ceil floor round fmod |
|
| Constants | pi e |
| Variables | any name you bind via setVariable() |
| Custom functions | 0, 1, 2, or 3 argument function pointers |
- Download
MathParser.zipfrom Releases - Sketch โ Include Library โ Add .ZIP Library...
Search MathParser in Tools โ Manage Libraries.
cd ~/Arduino/libraries
git clone https://github.com/meerzafarnoohani/MathParser.git#include <MathParser_Arduino.h>double r = MathParser::evaluate("2^10 + sqrt(144)"); // 1036.0double r = MathParser::evaluate("x * sin(x)", "x", 1.5708); // โ 1.5708MathParser te;
double x = 0.0, y = 0.0;
te.setVariable("x", &x);
te.setVariable("y", &y);
te.compile("sqrt(x^2 + y^2)"); // compile once
x = 3.0; y = 4.0;
double dist = te.eval(); // 5.0 variables read at eval time
x = 5.0; y = 12.0;
dist = te.eval(); // 13.0double myClamp(double val, double limit) {
return val > limit ? limit : val;
}
MathParser te;
te.addFunction2("clamp", myClamp);
double v = 0.0;
te.setVariable("v", &v);
te.compile("clamp(v * 2.5, 100)");
v = 30.0;
double out = te.eval(); // 75.0
v = 50.0;
out = te.eval(); // 100.0 (clamped)double MathParser::evaluate(const char* expr, int* error = nullptr);
double MathParser::evaluate(const char* expr, const char* varName, double value, int* error = nullptr);MathParser te;
// Bind variables (up to 8)
te.setVariable("x", &x); // double* pointer
// Bind custom functions
te.addFunction0("myConst", fn); // double fn()
te.addFunction1("myFn", fn); // double fn(double)
te.addFunction2("myFn2", fn); // double fn(double, double)
// Compile and evaluate
te.compile("x^2 + 1"); // returns bool
te.eval(); // returns double
// Error reporting
te.isValid(); // bool last compile succeeded?
te.errorPosition(); // int 0 = no error
te.printError("x^2 + 1"); // print arrow pointing at error
// Reset
te.reset(); // clear expression + all bindings| Example | Description |
|---|---|
BasicEval |
One-shot evaluation of 12 test expressions with pass/fail |
WithVariables |
Sensor calibration formula with live ADC simulation |
CustomFunctions |
Register clamp, deadband, unit conversion, motor shaping |
- Stack usage per
MathParserinstance: ~200 bytes (8 variable slots) - Parser uses heap transiently during
compile(), then frees it eval()uses no heap walks the compiled expression tree
| Platform | Status |
|---|---|
| ESP32 | โ Primary target |
| ESP8266 | โ Supported |
| Arduino Uno / Nano | โ Use short expressions |
| Arduino Mega | โ Supported |
| SAMD / RP2040 | โ Supported |
Original MathParser by Lewis Van Winkle github.com/codeplea/MathParser (zlib licence). Arduino/ESP32 port and C++ wrapper by Meer Zafarullah Noohani github.com/meerzafarnoohani.
MIT see LICENSE.