-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCANduino.ino
More file actions
183 lines (157 loc) · 5.37 KB
/
CANduino.ino
File metadata and controls
183 lines (157 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//Changed the display driver, as the old implementation did not specify the library.
//Here the hd44780 by Bill Perry.
#include <Wire.h>
#include "RTClib.h"
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
hd44780_I2Cexp lcd(0x20,I2Cexp_PCF8574, 4,5,6,3,2,1,0);
RTC_DS1307 RTC;
int debug = 1;
float v = 1; // init
float scalea = 0.999;
float scaleb = 0.001;
int state = 0; // on or off (0 == free, 1 == busy)
int IN = 5;
int LED = 6; // change to 7
int REL = 12;
long int prevm = 0; // counter for seconds
long int interval = 1000;
// for message toggle
long int displayinterval = 2000;
long int prevdism = 0;
// skal skifte til state = 0,1,2..
int displaystate = 0; // 0 = visits, 1 = average, 2 = last visit
long int unsigned busytemp = 0; // temp timer
//long int unsigned busyshort = 0; // short visits - phased out
//long int unsigned busylong = 0; // long visits - phased out
long int unsigned busytotal = 0; // total use time
long int unsigned visits = 0;
long int busylast = 0;
long avgmins;
long avgsecs;
void setup(){
// lcd.setBacklightPin(13, POSITIVE);
lcd.begin(16, 2);
lcd.setCursor(0,0);lcd.print("Toilet is VACANT");
//start serial connection
if (debug) { Serial.begin(9600); }// works best with Serial enabled
if (debug) { Serial.println("CANduino v4 - k2OS - RGG"); }
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(IN, INPUT_PULLUP);
pinMode(REL,OUTPUT); // one is for the LED on the door and the other is for the relay
pinMode(LED,OUTPUT);
digitalWrite(IN,HIGH);
digitalWrite(REL,LOW);
digitalWrite(LED,LOW);
pinMode(13, OUTPUT); // onboard LED
}
void loop(){
//read the pushbutton value into a variable
int sensorVal = digitalRead(IN);
// set a flag and start timer when door is locked. door is not declared 'locked' until after a set interval
v = scalea*v + scaleb*sensorVal;
// is the visit over?
if (v >= 0.999 && state == 1) {
// we ignore all visits under 15 seconds
if (busytemp > 15) {
visits++;
busylast = busytemp;
// we no longer handle long vs short visits on the 'duino as we save all data to SD (later)
busytotal = busytotal + busytemp;
/* if (busytemp >= 90) {
// tissetår tager fra ca 50 sekunder og op
busylong = busylong + busytemp;
busytemp = 0;
} else {
busyshort = busyshort + busytemp;
busytemp = 0;
} */
}
if (debug) { Serial.println(v); }
digitalWrite(13,LOW);
digitalWrite(REL,LOW);
digitalWrite(LED,LOW);
state = 0;
lcd.setCursor(10,0);
lcd.print("VACANT");
lcd.setCursor(11,1);
lcd.print(" ");
busytemp = 0;
} else if (v <= 0.0001 && state == 0) {
if (debug) { Serial.println(v); }
digitalWrite(13,HIGH);
digitalWrite(REL,HIGH);
digitalWrite(LED,HIGH);
state=1;
lcd.setCursor(10,0);
lcd.print("IN USE");
}
if (millis()-prevm > interval) {
// show busy message
if (state == 1) {
busytemp++; // increase the busy temp counter
long mins = busytemp/60; //convert seconds to minutes
long secs = busytemp-(mins*60); //subtract the coverted seconds to minutes in order to display 59 secs max
lcd.setCursor(11,1);
lcd.print(" ");
lcd.setCursor(11,1);
if (mins < 10) { lcd.print("0"); }
lcd.print(mins);
lcd.print(":");
if (secs < 10) { lcd.print("0"); }
lcd.print(secs);
} // eo 'if state ..'
else {
lcd.setCursor(11,1);
lcd.print(" ");
}
//show one message or the other, based on prevdism
if ((millis()-prevdism) > displayinterval) {
prevdism = millis();
displaystate++;
if (displaystate == 3) { displaystate = 0; }
}
switch(displaystate) {
case 0:
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Visits:");
if (visits > 90) { visits = 0;} //Avoid the 99 visits overflow
if (visits < 10) { lcd.print(" "); }
lcd.print(visits);
break;
case 1:
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Avg ");
// avgmins = ((busylong+busyshort)/visits+1)/60; //convert seconds to minutes
// avgsecs = ((busylong+busyshort)/visits+1)-(avgmins*60); //subtract the coverted seconds to minutes in order to display 59 secs max
avgmins = (busytotal/visits+1)/60; //convert seconds to minutes
avgsecs = (busytotal/visits+1)-(avgmins*60); //subtract the coverted seconds to minutes in order to display 59 secs max
if (avgmins < 10) { lcd.print("0"); }
lcd.print(avgmins);
lcd.print(":");
if (avgsecs < 10) { lcd.print("0"); }
lcd.print(avgsecs);
break;
case 2:
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Last ");
avgmins = busylast/60; //convert seconds to minutes
avgsecs = busylast-(avgmins*60); //subtract the coverted seconds to minutes in order to display 59 secs max
if (avgmins < 10) { lcd.print("0"); }
lcd.print(avgmins);
lcd.print(":");
if (avgsecs < 10) { lcd.print("0"); }
lcd.print(avgsecs);
break;
}
prevm = millis();
} // eo 'is ... prevm'
} // eo loop