void samplingMode(){
//PUMP A
if (aAlarmFlag == 0){//Execute the following if aAlarmFlag is 0
digitalWrite(pumpAPin, LOW);//Make sure pumpA is off
if ((aSecTime != 0)&&(nowSecTime > aSecTime)){aAlarmFlag = 1;}//If the time now is past the time pumpA should go off, set aAlarmFlag to 1
}
if (aAlarmFlag == 1){//Execute the following if aAlarmFlag is 1
digitalWrite(pumpAPin, HIGH);//Turn on pumpA
aEndTime = nowSecTime + (int(sampleVolume/(pumpACalibration/10.0))); //Calculate the amount of time that pumpA should run
}
if (aAlarmFlag == 2){//Execute the following if aAlarmFlag is 2
digitalWrite(pumpAPin, HIGH);//Make sure pumpA is on
if (nowSecTime > aEndTime){//Execute the following if duration of pumpA time is greater than the amount of time pumpA should run
if (sampleMode == 1){aAlarmFlag = 3;}//If the sample mode is set to once, set aAlarmFlag to 3, i.e., never to run again.
if (sampleMode == 0){//If the sample mode is set to daily, calculate the new aSecTime, the time for the aAlarm to wake
aSecTime = (aSecTime + 86400);// Add 24 hrs to the time that A fires (aSecTime)
tmElements_t updateAtm;//This is a time elements variable named updateAtm
breakTime(aSecTime, updateAtm);// Break aSecTime into the time elements variable updateTm
aHr = updateAtm.Hour;// Name the hour that pump A needs to fire aHr
aMin = updateAtm.Minute;// Name the minute that pump A needs to fire aMin
aSec = updateAtm.Second;// Name the second that pump A needs to fire aSec
aDay = updateAtm.Day;// Name the day that pump A needs to fire aDay
aMon = updateAtm.Month;// Name the month that pump A needs to fire aMon
aYr = updateAtm.Year-30;// Name the year that pump A needs to fire aYr. Minus thirty because value is years since 1970, and we are dealing with years since 2000
aAlarmFlag = 0; //Set aAlarmFlag to 0, so that it will restart and fire again when it wakes up at the appropriate time
digitalWrite(pumpAPin, LOW);
}
}
}
if (aAlarmFlag == 3){digitalWrite(pumpAPin, LOW);}//If aAlarmFlag is 3, then make sure pumpA is off
//PUMP B
if (bAlarmFlag == 0){//Execute the following if bAlarmFlag is 0
digitalWrite(pumpBPin, LOW);//Make sure pumpB is off
if ((bSecTime != 0)&&(nowSecTime > bSecTime)){bAlarmFlag = 1;}//If the time now is past the time pumpB should go off, set bAlarmFlag to 1
}
if (bAlarmFlag == 1){//Execute the following if bAlarmFlag is 1
digitalWrite(pumpBPin, HIGH);//Turn on pumpB
bEndTime = nowSecTime + (int(sampleVolume/(pumpBCalibration/10.0))); //Calculate the amount of time that pumpB should run
}
if (bAlarmFlag == 2){//Execute the following if bAlarmFlag is 2
digitalWrite(pumpBPin, HIGH);//Make sure pumpB is on
if (nowSecTime > bEndTime){//Execute the following if duration of pumpB time is greater than the amount of time pumpB should run
if (sampleMode == 1){bAlarmFlag = 3;}//If the sample mode is set to once, set bAlarmFlag to 3, i.e., never to run again.
if (sampleMode == 0){//If the sample mode is set to daily, calculate the new bSecTime, the time for the aAlarm to wake
bSecTime = (bSecTime + 86400);// Add 24 hrs to the time that B fires (bSecTime)
tmElements_t updateBtm;//This is a time elements variable named updateBtm
breakTime(bSecTime, updateBtm);// Break bSecTime into the time elements variable updateTm
bHr = updateBtm.Hour;// Name the hour that pump B needs to fire bHr
bMin = updateBtm.Minute;// Name the minute that pump B needs to fire bMin
bSec = updateBtm.Second;// Name the second that pump B needs to fire bSec
bDay = updateBtm.Day;// Name the day that pump B needs to fire bDay
bMon = updateBtm.Month;// Name the month that pump B needs to fire bMon
bYr = updateBtm.Year-30;// Name the year that pump B needs to fire bYr. Minus thirty because value is years since 1970, and we are dealing with years since 2000
bAlarmFlag = 0; //Set bAlarmFlag to 0, so that it will restart and fire again when it wakes up at the appropriate time
digitalWrite(pumpBPin, LOW);
}
}
}
if (bAlarmFlag == 3){digitalWrite(pumpBPin, LOW);}//If aAlarmFlag is 3, then make sure pumpB is off
}
void checkSleep(){//Determines if the sampler should go back to sleep
if ((aAlarmFlag == 0)&&(bAlarmFlag == 3)&&(aSecTime>nowSecTime+60)){goToSleep();}
if ((aAlarmFlag == 3)&&(bAlarmFlag == 0)&&(bSecTime>nowSecTime+60)){goToSleep();}
if ((aAlarmFlag == 0)&&(bAlarmFlag == 0)&&(aSecTime>nowSecTime+60)&&(bSecTime>nowSecTime+60)){goToSleep();}
if ((aAlarmFlag == 3)&&(bAlarmFlag == 3)){goToSleep();}
}
void goToSleep(){//Puts the sampler to sleep
long sleepSecTime;
if ((aAlarmFlag != 3)&&(bAlarmFlag != 3)&&(aSecTime < bSecTime)){sleepSecTime = aSecTime - nowSecTime;}
if ((aAlarmFlag != 3)&&(bAlarmFlag != 3)&&(bSecTime < aSecTime)){sleepSecTime = bSecTime - nowSecTime;}
if ((aAlarmFlag != 3)&&(bAlarmFlag != 3)&&(aSecTime == bSecTime)){sleepSecTime = aSecTime - nowSecTime;}
if ((aAlarmFlag != 3)&&(bAlarmFlag == 3)) {sleepSecTime = aSecTime - nowSecTime;}
if ((aAlarmFlag == 3)&&(bAlarmFlag != 3)) {sleepSecTime = bSecTime - nowSecTime;}
if ((aAlarmFlag == 3)&&(bAlarmFlag == 3)) {sleepSecTime = 2628000;}//sleep one month, will wake and go to sleep again after that one month
hrToSleep = int(sleepSecTime/3600);// calculates the hours to sleep
minToSleep = int((sleepSecTime-(3600*hrToSleep))/60);// calculates the remaining minutes to sleep
secToSleep = int(sleepSecTime-(3600*hrToSleep)-(60*minToSleep));// calculates the remaining seconds to sleep
tempSensor.shutdown();// Shut down the temperature sensor
delay (200);
digitalWrite(oledPowerPin, LOW);// Turn off the power to the OLED
digitalWrite(tempPowerPin, LOW);// Turn off the power to the temperature sensor
digitalWrite(irPowerPin, LOW);// Turn off the power to the IR sensor
digitalWrite(voltageReadPin, LOW);// Turn off the power to the voltage read pin A15
alarm.setRtcTimer(hrToSleep, minToSleep, secToSleep);// Sets the RTC TIMER with the appropriate amount of hour, min, sec to sleep
delay (100);
int who;
who = Snooze.hibernate( config_teensy35 );// wake up the Teensy and identify who woke it up
if (who == REED_INTERRUPT_PIN){
delay(500);
if (digitalRead(REED_INTERRUPT_PIN)==HIGH){
delay(500);
if (digitalRead(REED_INTERRUPT_PIN)==HIGH){
digitalWrite(oledPowerPin, HIGH);// Power on the oled
digitalWrite(tempPowerPin, HIGH);// Power on the temperature sensor
digitalWrite(irPowerPin, HIGH);// Power on the IR sensor
digitalWrite(voltageReadPin, HIGH);// Turn on the power to the voltage read pin A15
tempSensor.wake();// Wake up the temperature sensor
delay(200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Start the OLED back up again
display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE,BLACK); display.setCursor(0,16); display.println("Interrupt");// Put "Interrupt" on the first line of the OLED
display.display();// Update the OLED display
menu = 0; pos = 0;//Sets menu and position to 0
readSampleParamArray();
delay(1000);
}
if (digitalRead(REED_INTERRUPT_PIN)==LOW){menu = 9;}
}
if (digitalRead(REED_INTERRUPT_PIN)==LOW){menu = 9;}
}
else{//alarm pin wake up
digitalWrite(oledPowerPin, HIGH);// Power on the oled
digitalWrite(tempPowerPin, HIGH);// Power on the temperature sensor
digitalWrite(voltageReadPin, HIGH);// Turn on the power to the voltage read pin A15
tempSensor.wake();// Wake up the temperature sensor
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Start the OLED back up again
delay(500);
getVoltage();
menu = 9;//finishes out sleep and sends back to loop running sampling mode (case 9)
}
}
void samplingDisplay(){//Updates the display during sampling
if ((aAlarmFlag == 2)&&(bAlarmFlag !=2)){display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE,BLACK); display.setCursor(0,16); display.println("Pump A");}// Display Pump A
if ((aAlarmFlag != 2)&&(bAlarmFlag ==2)){display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE,BLACK); display.setCursor(0,16); display.println("Pump B");}// Display Pump B
if ((aAlarmFlag == 2)&&(bAlarmFlag ==2)){display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE,BLACK); display.setCursor(0,16); display.println("Both Pumps");}// Display Both Pumps
if ((aAlarmFlag != 2)&&(bAlarmFlag !=2)){display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE,BLACK); display.setCursor(0,16); display.println("Wait...");}// Display Wait...
display.display();// Update the OLED display
}