commit dfe0c803bb836306fd16f3a0069e7b69724ae9e9 Author: Markus Rosenstihl Date: Wed Mar 8 16:47:33 2017 +0000 PFG security firmware initila realease diff --git a/pfg_security.ino b/pfg_security.ino new file mode 100644 index 0000000..8c2d88e --- /dev/null +++ b/pfg_security.ino @@ -0,0 +1,210 @@ +#include +// Needed for LiquidCrystal_I2C.h +#include +// from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/ +#include + + +// as per http://funduino.de/nr-06-zwei-i%C2%B2c-displays-gleichzeitig +LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); + +boolean setup_ok=true; // false will stop loop() +const int N=16; // number of sample averages +volatile unsigned int deltas[N]; // array of time deltas between interrupt +volatile unsigned int n = 0; // run variable +unsigned long total; +float flow; +int prescaler; +char buff[10]; // temp. buffer for string manipulation/formatting +char serialbuff[16]; // temp. buffer for string manipulation/formatting + +/* +User configuration +================== + +Flow meter +========== + +The PFG flow meter can measure from 0.24 to 17 l/min at 4 pulses/s per l/min +max period about 1s, i.e. min flow of 0.24l/min +min period about 15 ms i.e. max flow of 17l/min + +The time base of the timer counter TCNT1 +t_step or seconds/tick = prescaler/clock + +Note: Maximum TCNT1 value is 65535, i.e. +Maximum time lengths (and thus timeouts) per prescaler value are: +8 0.0327675 s +64 0.26214 s +256 1.04856 s +1024 4.19424 s + +For accuracy the prescaler is set automatically fitting to the TIMEOUT. +*/ +#define TIMEOUT 1.0 // measurement timeout in Hz (needs to be less than timer1 limits above) +#define DISPLAY_INTERVAL 1.0 // display update interval (and serial send interval) + +const float conversion = 4.0; // conversion factor, pulses/s per l/min +const float flow_threshold = 1.0; // alarm threshold for flow + + +/* +Hardware configuration / wiring setup +===================================== + +Pin 13 has an LED connected on most Arduino boards. +This is the alarm LED pin +*/ +#define ALARM_LED_PIN 13 +// interlock output pin +#define INTERLOCK_PIN 12 + + +/* +Setup routine +============= + +Run once upon startup +*/ +void setup() +{ + lcd.begin(16,2); // start LCD (16x2 characters) + lcd.home (); // go home + lcd.print("PFG security"); // string first line + lcd.setCursor ( 0, 1 ); // go to the next line + lcd.print (""); // string second line + Serial.begin(19200); // serial line speed + /* Interrupt pins + UNO: + interrupt 1 is pin 3 + interrupt 0 is pin 2 + */ + attachInterrupt(0, get_time, FALLING); + pinMode(ALARM_LED_PIN, OUTPUT); + pinMode(INTERLOCK_PIN, OUTPUT); + + /* + Set sensible default starting values for the deltas array, + otherwise the array values are maybe zero at the beginning + and we get very high (wrong) flow values. + */ + for (int k=0; k