GasMeter: verschil tussen versies

Uit MakerSpace Leiden
Ga naar: navigatie, zoeken
(Nieuwe pagina aangemaakt met ' /* * Demo program for the non-blocking: * * float gas_Meter_Reader(float gasMeter) * * Reads an analog pulse from a reflection sensor on an input. Adds 0,1...')
(geen verschil)

Versie van 9 mrt 2017 om 15:56

/*

* Demo program for the non-blocking: 
* 
* float gas_Meter_Reader(float gasMeter)
* 
* Reads an analog pulse from a reflection sensor on an input. Adds 0,1 M3 to gasMeter per pulse
* Tested on a ESP8266 but should run on almost anything
* 
* Aart 03-2017
*/
  1. define SENSOR_PIN A0 // Gas meter reflection sensor pin (analog)
  2. define DETECT 500 // Analog value "definately a mirror"
  3. define RELEASE 700 // Analog value "definately no mirror"
  1. define LOAD_PIN 2 // PWM output for CPU load. 2 = onboard led on the Wemos D1. High = no load = led off
  2. 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); // The actual function is called
 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);   
 while (millis() - start_time  < 100) {
   delay(1); // Do nosePicking();  
 }
 digitalWrite (LOAD_PIN,LOW); 
 start_time = millis(); 

}

float gas_Meter_Reader(float gasMeter) {

 /*
  * 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 < 500) 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; 

}