This is a simple library for reading noisy analog inputs in Arduino.
Datalog is a class for averaging noisy input and storing highlights.
Initialize a datalogger class, with the designated amount of storage. If eeprom_address is specified, the minimums and maximums will be persisted there.
Set the latest value.
Get current (smooth) value. This will be an average of the latest values given in with update(double).
Get a struct that contains the record high and low.
struct MinMaxVal
{
double min;
double max;
};
Reset the minimum and maximum value.
Returns true, if a fast change was noticed on last update, and therefore the value was changed to a "new normal".
For now, it looks like this:
#include <Datalog.h>
// init with pin number and number of samples to
// store for averaging
Datalog temperature(A2, 32);
void setup() {
Serial.begin(9600);
}
void loop() {
// calculate temperature from reading
double voltage = (val / 1024.0) * 5.0;
double temp = ( voltage - 0.5 ) * 100;
// run this often, it will keep your average better
temperature.update(temp);
// get the smoothed value
double val = temperature.getValue();
Serial.print(temp);
Serial.println(" deg C");
delay(100);
}
Button is a class for attaching multiple functions to buttons. Attach only one function per time, and try to add enough time between them (like at least one second). After a button press, the callback with longest matching delay will be executed.
Add a new listener to button, will be executed if button is pressed for at least the amount of milliseconds specified in delay.
Update the value of the input and execute callbacks as necessary. Give in HIGH or LOW (usually directly from digitalRead()).
char BUTTON_PIN = 0;
Button b;
void myAction()
{
// do something here when button pressed
}
void longAction()
{
// do something here when button pressed for one second
}
void setup()
{
pinMode(BUTTON_PIN, INPUT);
b.attachFunction(myAction);
b.attachFunction(longAction, 1000);
}
void loop()
{
b.update(digitalRead(BUTTON_PIN));
}
```
### Settings
The file `settings.h` includes #defines you can comment away to save some space by removing unnecessary code.