boolean sit;
boolean breaking;
//Reading integers from photocell
int photocellPin;
int photocellReading;
int sitThreshold;
// sit timer
long sitStart;
long sitEnd;
long sitLimit;
// break timer
long breakStart;
long breakEnd;
long breakLimit;
boolean firstBeep;
int firstBeepTime;
int beepWaitTime;
boolean secondBeep;
int secondBeepTime;
void setup() {
Serial.begin(9600);
sit = false;
photocellPin = 0;
sitThreshold = 600;
sitLimit = 10000;
breaking = false;
breakLimit = 5000;
firstBeep = false;
firstBeepTime = 3000;
beepWaitTime = 3000;
secondBeep = false;
secondBeepTime = 4000;
pinMode(13, OUTPUT);
Serial.println("Setup complete!");
}
void reset() {
sit = false;
breaking = false;
firstBeep = false;
secondBeep = false;
sitStart = 0;
sitEnd = 0;
breakStart = 0;
breakEnd = 0;
Serial.println("Resetting.");
}
void loop() {
photocellReading = analogRead(photocellPin);
//sitting down on chair//
if (sit == false) {
if (photocellReading < sitThreshold) {
sit = true;
sitStart = millis();
sitEnd = sitStart + sitLimit;
Serial.println("SOMEONE IS SITTING!");
}
}
//getting up for a break//
if (sit && (millis() < sitEnd)) {
if ((photocellReading >= sitThreshold) && !breaking) {
breaking = true;
breakStart = millis();
breakEnd = breakStart + breakLimit;
Serial.println("Breaking...");
}
}
//up during break for longer than given break time//
if (breaking && (millis() >= breakEnd)) {
reset();
}
//person got up for a break but time ended before the sit limit and break time was up//
if (sit && millis() < sitEnd && breaking && millis() >= breakEnd) {
reset();
}
//first beep//
if (sit && (millis() >= sitEnd) && !firstBeep) {
firstBeep = true;
tone(8, 300, firstBeepTime);
Serial.println("First beep");
delay(firstBeepTime + beepWaitTime);
photocellReading = analogRead(photocellPin);
if (photocellReading > sitThreshold) {
reset();
}
}
//second beep//
if (sit && (millis() >= sitEnd) && firstBeep && !secondBeep) {
secondBeep = true;
tone(8, 400, secondBeepTime);
Serial.println("Second beep");
reset();
}
}
No comments:
Post a Comment