In the code below, in void Brew(), do I need the noInterrupts(); and interrupts(); lines? The variable FlowSensorCount is declared volatile, and an interrupt runs the code "FlowSensorCount++;"
Perhaps I need to turn off interrupts when checking that variable, or maybe not? Won't that make my flow meter miss some counts when interrupts are turned off.
// D2---FLOW SENSOR
// D3--WATER VALVE SOLENOID
#include <LED.h>
#include <Button.h>
Button startButton = Button(A5,PULLUP);
Button fullButton = Button(A2,PULLUP);
Button halfButton = Button(A0,PULLUP);
LED startLED = LED(A4);
LED fullLED = LED(A3);
LED halfLED = LED(A1);
//3 selection conditions:
const int emptyPot = 0; //no selection made
const int fullPot = 1; //full pot selection
const int halfPot = 2; //half pot selection
// variables:
int potSelection = 0; //selection starts out with nothing on power up
int fullCount = 200; //number of pulses for a full pot of coffee
int halfCount = 100; //number of pulses for a half pot of coffee
unsigned long lastPress; //last time any button was pressed, for timeout
//***************setup variables***************
volatile int FlowSensorCount; //the counter for the flow sensor
void setup() {
Serial.begin(9600); //initiate serial port
Serial.println("Bunn Coffee Water Version 1.0");
attachInterrupt(0, CountPulse, RISING);//turn on interrupt for flow sensor on pin 2
digitalWrite(2, HIGH); //turn on pull up resistor for flow sensor on pin 2
delay(200);
}
void CountPulse() //called from the interupt on Pin 2, from the water sensor
{
FlowSensorCount++; //increases count by 1
}
void loop() {
updateStartLED(); //update start LED status
if (fullButton.isPressed()){ //if the full button was pressed
lastPress = millis(); //update the time
Serial.println("Full Selection Button was pressed");
fullLED.on(); //turn on full LED
halfLED.off(); //turn off half LED
potSelection = fullPot; //they selected a full pot
}
if (halfButton.isPressed()) { //if the half button was pressed
lastPress = millis(); //update the time
Serial.println("Half Selection Button was pressed");
halfLED.on(); //turn on half LED
fullLED.off();//turn off full LED
potSelection = halfPot; //they selected a full pot
}
if (startButton.isPressed()) { //if the startbutton was pressed
lastPress = millis(); //update the time
Serial.println("Start Button was pressed");
if (potSelection == fullPot) Brew(fullCount); //brew a full pot
else if (potSelection == halfPot) Brew(halfCount); //brew a half pot
else flashSelection();
}
if (millis() - lastPress > 10000) { //they changed their mind?
potSelection = emptyPot; //cancel the selection
fullLED.off(); //turn off any LEDS
halfLED.off(); //turn off any LEDS
startLED.off(); //turn off any LEDS
}
delay(100);
}
void Brew(int pulseCount){
int solenoidPin = 3; //water valve solenoid pin
pinMode(solenoidPin, OUTPUT); //set it as an output
FlowSensorCount = 0; //reset sensor count
digitalWrite(solenoidPin, HIGH); //turn on the water valve
while (1) {
noInterrupts();
if (FlowSensorCount >= pulseCount) {
digitalWrite(solenoidPin, LOW); //turn off the water valve
interrupts();
break;
}
}
}
void updateStartLED() {
if (potSelection == emptyPot) return; //no selection is made so do nothing with the start LED
unsigned long lastBlink; //last state change
if (millis() - lastBlink > 500) { //it's time to toggle
startLED.toggle();
}
}
void flashSelection() {
//user pressed start before making a selection
//flash the half and full LEDS to give them a hint on what to do!
Serial.println("make a selection first! (Half or Full)");
boolean state;
halfLED.on();
for (int i = 0; i < 10; i++) {
state = !state; //change the LED state
halfLED.toggle();
fullLED.toggle();
delay(100);
}
halfLED.off(); //turn off on leaving
fullLED.off(); //turn off on leaving
}