Sjabloon:OldPage
/*
* Demo program for the non-blocking:
*
* gas_Meter_Reader(float gasMeter, int interval)
*
* Reads an analog pulse from a reflection sensor on an input. Adds 0,1 M3 to gasMeter per pulse
* Interval sets the time betweet readings, should be set to about 500 ms for the Makerspace meter
* Tested on a ESP8266 but should run on almost anything
*
* Aart 03-2017 V 0.2
*/
#define SENSOR_PIN A0 // Gas meter reflection sensor pin (analog)
#define DETECT 500 // Analog value "definately a mirror"
#define RELEASE 700 // Analog value "definately no mirror"
#define INTERVAL 500 // Time between readings in ms
#define LOAD_PIN 2 // PWM output for CPU load. 2 = onboard led on the Wemos D1. High = no load = led off
#define SEND_INTERVAL 2000 // Time between output messages in ms
// #define debug_gas // gasmeter routine debug messages
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Hello. This is the gasMeter Non blocking routine demo"));
pinMode(LOAD_PIN, OUTPUT);
digitalWrite (LOAD_PIN,LOW);
}
void loop() {
static float gasMeter; // current value of gas meter
static long start_time; // ms. Start time of last 100 ms system tick
static long last_send; // ms. Time since last message
gasMeter = gas_Meter_Reader(gasMeter, INTERVAL); // The actual function is called
if (millis() < last_send) last_send = millis(); // Roll over
if ((millis() - last_send) > SEND_INTERVAL) { // once in a while..
last_send = millis();
Serial.print("gasMeter: ");
Serial.println(gasMeter);
}
// Wait up to 100 ms in this idle & load measurement routine
digitalWrite (LOAD_PIN,HIGH);
if (millis () < start_time) start_time = millis(); // roll over
while (millis() - start_time < 100) {
delay(1); // Do nosePicking();
}
digitalWrite (LOAD_PIN,LOW);
start_time = millis();
}
float gas_Meter_Reader(float gasMeter, int interval) {
/*
* Reading the input and adding 0.1 m3 per detected pulse.
* Non-blocking
*/
static long last_run; // Time of last run of this routine in ms
int sensor; // sensor value
enum states { // States of the input state machine
wait_for_start,
doublecheck,
count,
wait_for_end,
};
static int state = wait_for_start;
if (millis() < last_run) last_run = millis(); // roll over
if (millis() - last_run < interval) return gasMeter;
last_run = millis(); // Run once every 500 ms
sensor = analogRead(SENSOR_PIN);
#ifdef debug_gas
Serial.print ("Sensor: ");
Serial.println (sensor);
#endif
switch (state) {
case wait_for_start: {
if (sensor < DETECT) state = doublecheck;
#ifdef debug_gas
Serial.println("State: Wait for start");
#endif
} break;
case doublecheck: {
if (sensor < DETECT) state = count;
if (sensor > DETECT) state = wait_for_start;
#ifdef debug_gas
Serial.println("State: Doublecheck");
#endif
} break;
case count: {
gasMeter += 0.1; // 0,1 m3 per omwenteling
#ifdef debug_gas
Serial.print("State: count; gasMeterValue: ");
Serial.println(gasMeter);
#endif
state = wait_for_end;
} break;
case wait_for_end: {
if (sensor > RELEASE) state = wait_for_start;
#ifdef debug_gas
Serial.println("State: Wait for end");
#endif
} break;
break;
}
return gasMeter;
}