Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By rudy
#72672 I have run the two Ticker examples and I did understand it at some level. But having my own application that I needed to implement gave me a better understanding of how to use the library.

I need to implement a data logger that saves voltage and current readings from an INA219 to a SD card. A minimal user interface. Two push buttons and a character LCD. I had the INA219 and SD code mostly done but I needed the timing and button handling framework.

The example does not include the INA219 or LCD, just Ticker and two buttons and three LEDs. It waits for a start button then it takes 1000 samples of A0 every 20mS and stores it in an array. The start button enables the Ticker, when all samples are acquired the ticker routine disables itself. After the array is filled the readings are sent to the serial port when a print button is pressed. After the data is printed a new sample set is allowed.

The program makes use of a button library that I liked the implementation of. https://github.com/JChristensen/Button

Code: Select all// This is a Ticker example. A repeating ticker and a ticker that is enabled, repeats, then is disabled.
// The analog input is the reading measured and saved to an array.
//
// When start button is pressed/released ticker is enabled and takes samples at the sampling period
// and puts into an array. The complete fill of the array disables ticker in it's callback routine.
// A print button does a serial print of the array data. The print button could be eliminated if
// it was desired to print the data upon completion of filling the array.

#include <ESP8266WiFi.h>
#include <Ticker.h>

//----------------------------------------------------
#include <Button.h>        //https://github.com/JChristensen/Button
const int startSW = 4;     //
const int printSW = 0;     //
#define PULLUP true        //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true        //negative logic, i.e. a high state means the button is NOT pressed.
#define DEBOUNCE_MS 20     //debounce of 20 mS usually works well for tactile button switches.

Button printSwitch(printSW, PULLUP, INVERT, DEBOUNCE_MS);    //Declare the button
Button startSwitch(startSW, PULLUP, INVERT, DEBOUNCE_MS);    //Declare the button
//----------------------------------------------------

const int LEDcomplete = 14;     // completed samples, cleared when samples printed
const int LEDsecond = 12;       // once per second toggle light
const int LEDrunning = 13;      // fast toggles while taking samples

Ticker tickerLogger;
Ticker tickerSeconds;

const int samplePeriod = 20;     // in mS
const int sampleSize = 1000;     // 20ms x 1000 = 20 sec

int sampleBuffer[sampleSize];
int indx = 0;

bool sampleComplete = 0;

//===================================================
void toggleLEDsecond() {    // once per second ticker
  digitalWrite(LEDsecond, !digitalRead(LEDsecond));   //toggle led
}

//----------------------------------------------------
void tickerLoggerCallback() {
  int sample = analogRead(A0);     // just in case I wanted sample to be something else

  sampleBuffer[indx] = sample;
  ++indx;

  digitalWrite(LEDrunning, !digitalRead(LEDrunning));   //toggle led

  if (  indx == sampleSize )      // when we have our samples then disable ticker
  {
    tickerLogger.detach();
    indx = 0;
    sampleComplete = 1;
    digitalWrite(LEDcomplete, 1);  // all samples done light, will be turned off when printed
  }
}

//-----------------------------------------------------
void printReadingBuffer()           // print the index and value, repeat.
{
  for (int ix = 0; ix < sampleSize; ix++) {
    Serial.print(ix);
    Serial.print(", ");
    Serial.println(sampleBuffer[ix]);
    yield();                         // give some time to background tasks
  }
}

//=====================================================
void setup()
{
  Serial.begin(115200);
  Serial.println("\n Start");

  pinMode(LEDsecond, OUTPUT);       // 1 sec toggle
  pinMode(LEDrunning, OUTPUT);      // toggles while taking samples
  pinMode(LEDcomplete, OUTPUT);     // has samples
  digitalWrite(LEDrunning, 0);
  digitalWrite(LEDcomplete, 1);

  tickerSeconds.attach(1, toggleLEDsecond);  // every second toggle led
}

//=====================================================
void loop()
{
  //-------------
    startSwitch.read();               //on start switch release clear flags, start ticker
    if (startSwitch.wasReleased()) {

    sampleComplete = 0;               //clear flags, led
    digitalWrite(LEDcomplete, 0);
    tickerLogger.attach_ms(samplePeriod, tickerLoggerCallback);
    }
 
  //-------------
  if (sampleComplete == 1)            //if we have all samples then print if button pressed
  {
    printSwitch.read();   
    if (printSwitch.wasReleased()) {   
       
      printReadingBuffer();
      sampleComplete = 0;
      digitalWrite(LEDcomplete, 0);
    }
  }

  //-------------
 
}

//===================================================