2 * activates the relay when the rate of flow exceeds
10 #define TRIM_ADC 2 /* ADC2 */
11 #define TRIM_PIN 4 /* ADC2 is on PC4 */
15 /* our frequency counter */
16 volatile int pulsecount = 0;
18 /* increase the counter on interrupt */
24 unsigned long last_time = 0;
28 * div 3 to give a 1 - 342 range
30 void update_threshold(void)
32 threshold = analogRead(TRIM_ADC);
39 // pin 2 is the pulse from the flow sensor
40 pinMode(FLOW_PIN, INPUT);
42 // pin 4 (ADC2) is an analog input
43 pinMode(TRIM_PIN, INPUT);
45 // pin 0 & 1 are the LEDs
46 pinMode(LED_RED, OUTPUT);
47 pinMode(LED_GREEN, OUTPUT);
48 pinMode(RELAY, OUTPUT);
50 // starting state: RED, relay open
51 digitalWrite(LED_RED, HIGH);
52 digitalWrite(LED_GREEN, LOW);
53 digitalWrite(RELAY, LOW);
58 // now start the interupt routine
59 attachInterrupt(FLOW_INT, counterISR, FALLING);
63 unsigned long now = millis();
65 // clock has looped, restart
66 if (now < last_time) {
72 // it has been a second
73 if (now >= (last_time + 1000)) {
74 int count = pulsecount;
78 digitalWrite(LED_RED, HIGH);
79 digitalWrite(LED_GREEN, LOW);
80 digitalWrite(RELAY, LOW);
82 if (count > threshold) {
83 // good flow: green, close relay
84 digitalWrite(LED_RED, LOW);
85 digitalWrite(LED_GREEN, HIGH);
86 digitalWrite(RELAY, HIGH);
89 digitalWrite(LED_RED, HIGH);
90 digitalWrite(LED_GREEN, HIGH);
91 digitalWrite(RELAY, LOW);
94 // check if the threshold moved
102 // otherwise do nothing, just wait