Here’s where I read the digital pins (D1,D2,D3).
i’m exposing the m_bufferLevelXXX variables in a json string like so:
sprintf(jsonResult, "{\"m\":\"%s\",\"s\":\"%s\",\"tin\":%.2f,\"tout\":%.2f,\"thtr\":%.2f,\"tamb\":%.2f,\"bl\":%d,\"b1\":%d,\"b2\":%d,\"b3\":%d}",
str, statusStr, state.tempIn, state.tempOut, state.tempHeater, state.tempAmbient, state.bufferLevel,
m_bufferLevelLow, m_bufferLevelMed, m_bufferLevelHigh);
Spark.variable("getstats", &jsonResult, STRING);
and here’s where the pins are read.
void processBufferLevels()
{
m_bufferLevelLow = digitalRead(PIN_BUFFER_LOW);
m_bufferLevelMed = digitalRead(PIN_BUFFER_MED);
m_bufferLevelHigh = digitalRead(PIN_BUFFER_HIGH);
unsigned long delta;
// debounce
if (m_bufferLevelLow != m_pin_buffer_low_debounce_state)
{
if (m_pin_buffer_low_debounce == 0)
m_pin_buffer_low_debounce = millis();
delta = millis() - m_pin_buffer_low_debounce;
if (delta > 50)
{
m_pin_buffer_low_debounce_state = m_bufferLevelLow;
m_pin_buffer_low_debounce = 0;
}
}
if (m_bufferLevelMed != m_pin_buffer_med_debounce_state)
{
if (m_pin_buffer_med_debounce == 0)
m_pin_buffer_med_debounce = millis();
delta = millis() - m_pin_buffer_med_debounce;
if (delta > 50)
{
m_pin_buffer_med_debounce_state = m_bufferLevelMed;
m_pin_buffer_med_debounce = 0;
}
}
if (m_bufferLevelHigh != m_pin_buffer_high_debounce_state)
{
if (m_pin_buffer_high_debounce == 0)
m_pin_buffer_high_debounce = millis();
delta = millis() - m_pin_buffer_high_debounce;
if (delta > 50)
{
m_pin_buffer_high_debounce_state = m_bufferLevelHigh;
m_pin_buffer_high_debounce = 0;
}
}
if (m_pin_buffer_high_debounce_state != HIGH) state.bufferLevel = BUFFER_LEVEL_HIGH;
else if (m_pin_buffer_med_debounce_state != HIGH) state.bufferLevel = BUFFER_LEVEL_MEDIUM;
else if (m_pin_buffer_low_debounce_state != HIGH) state.bufferLevel = BUFFER_LEVEL_LOW;
else state.bufferLevel = BUFFER_LEVEL_EMPTY;
}
Initially I was reading the pins and keeping their states (m_pin_buffer_low_debounce_state etc…) in a byte variable, but I have now changed them to be ints and I have not seen the problem since (20 minutes and counting)… Could it simply be an sprintf() fluke displaying bytes using %d ?
Something else to note btw, without the debounce code I had quite a bit of flakey bouncing going on… but i’m wondering if that might have been because of the read 32 at the time…