You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.8 KiB

/*
* Using code from https://www.arduino.cc/en/Tutorial/Smoothing
*
*/
#include <Wire.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
// Force Sensitive Resistor Settings
int analogInPin = A2; // Analog pin the FSR is attached to
int sensorValue = 0; // default value for the FSR
int sensorGate = 500; // set this as the 'sensitivity' for the sitting detection. (could use a potentiometer for input)
int maxSitTime = 3600; //default time the user can sit.
int snoozebutton = 300; // how long it adds when you press snooze
int breakTime = 1800; // time you can be away from the desk for
//smoothed input from resistor
const int numReadings = 10; // Constant, shows how many to store for average
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
// Button Settings
int buttonPin = 11; // Digital Pin the button is attached to
int buttonState = 0; // default value for the button
// Intermediate Variables
int satDownTime = 0;
bool isSitting = 0;
bool snoozed = 0;
int snoozeTime = 0;
void setup() {
// all reading for avg to 0
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// RTC
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.println("Serial Debug Output")
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
// Change sittime
Alarm.alarmRepeat(9,00,0, 9amBreak);
Alarm.alarmRepeat(12,00,0, 12pmBreak);
//Alarm.alarmRepeat(17,00,0, 5pmBreak); //Disabled due to overlap with the 12-5pm break time.
Alarm.alarmRepea
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
delay(1); // delay in between reads for stability
// sitting Detection
if (average > sensorGate && isSitting == 0) {
satDownTime = now(); //if the average pressure on seat is higher than the defined gate, AND isSitting is 0
Serial.println("User is now sitting");
isSitting = 1;
}
if (average =< sensorGate && isSitting == 1) {
satDownTime = now(); //if not sitting down, change satDownTime to now (for break calc)
Serial.println("User is no longer sitting");
isSitting = 0;
}
if (now() == satDownTime + maxSitTime && isSitting == 1) { // if the time you sat down at, (plus the max time you can sit down at) equals now
Serial.println("ALERT: You have been sitting for the maximum allotted time, please take a break. This message will only show up once.");
}
if (now() == satDownTime + breakTime + snoozeTime && isSitting == 0) { // if the time you sat up at, plus the max time you can break for) equals now
Serial.println("ALERT: You have been on break for the maxmimum allotted time, either press the snooze button for 5 more minutes, or get back to work.")
snoozeTime = 0; // Reset snoozeTime back down to 0, so it doesn't keep compounding snooze Time every press.
snoozed = 0; // reset to 0, so user can press snooze again.
}
// on button press, add
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && snoozed == 0 && isSitting = 0) {
snoozed = 1;
snoozeTime = snoozebutton;
}
}
void alert() {
// Empty because jesus christ phone alerts are too much work
}
void 9amBreak() {
breakTime = 900;
}
void 12pmBreak() {
breakTime = 3600;
}
void 5pmBreak() {
breakTime = 1200;
}