Skip to content

meerzafarnoohani/MathParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

MathParser for Arduino & ESP32

Evaluate math expressions from strings at runtime no dependencies, no heap beyond the parser itself.

Arduino Library License: MIT Platform


What is MathParser?

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

Supported syntax

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

Installation

Via Arduino IDE

  1. Download MathParser.zip from Releases
  2. Sketch โ†’ Include Library โ†’ Add .ZIP Library...

Via Arduino Library Manager

Search MathParser in Tools โ†’ Manage Libraries.

Manual

cd ~/Arduino/libraries
git clone https://github.com/meerzafarnoohani/MathParser.git

Quick Start

#include <MathParser_Arduino.h>

One-shot evaluation (no variables)

double r = MathParser::evaluate("2^10 + sqrt(144)");  // 1036.0

One-shot with a single variable

double r = MathParser::evaluate("x * sin(x)", "x", 1.5708);  // โ‰ˆ 1.5708

Compiled expression with bound variables

MathParser 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.0

Custom functions

double 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)

API Reference

Static helpers (no object needed)

double MathParser::evaluate(const char* expr, int* error = nullptr);
double MathParser::evaluate(const char* expr, const char* varName, double value, int* error = nullptr);

Instance methods

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

Examples

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

Memory

  • Stack usage per MathParser instance: ~200 bytes (8 variable slots)
  • Parser uses heap transiently during compile(), then frees it
  • eval() uses no heap walks the compiled expression tree

Supported Platforms

Platform Status
ESP32 โœ… Primary target
ESP8266 โœ… Supported
Arduino Uno / Nano โœ… Use short expressions
Arduino Mega โœ… Supported
SAMD / RP2040 โœ… Supported

Credits

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.


License

MIT see LICENSE.

About

MathParser lets your Arduino or ESP32 evaluate a math expression stored as a string at runtime.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors