-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.ino
More file actions
233 lines (185 loc) · 7.24 KB
/
Copy path2.ino
File metadata and controls
233 lines (185 loc) · 7.24 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <TM1637Display.h>
#define CLK 2
#define DIO 3
const int potPin = A0; // connecting a pot to A0 in order to set starting value
int potValue; // variable for initial pot state value
const int analogLow = 0; // values comming from the analog read
const int analogHigh = 1023; // values comming from the analog read
unsigned long currentMillis;
unsigned long previousMillis = 0;
const long interval = 500; // set to half of wanted interval
int displayTime; // the display time in seconds
int lastDisplayTime; // last used display time
int blinkDots = 0;
int machineState = 0; // 0 for stop. 1 for run
int machineStateError = 0;
const int NO_WATER_Pin = 13; // used to connectto Error light
const int pb1Pin = 12; // Push button 1 - start/puse + reset
const int pb2Pin = 11; // Push button 2 - Add Time + reset
const int float1Pin = 10;
const int float2Pin = 9;
const int relayPin = 8; // pin for connecting the relay to run stop machine
const int fillValvePin = 7; // pin for connecting the relay to open water valve to fill up tank
int pb1State; // save push buttons state from ISR
int pb2State;
int float1State;
int float2State;
int lastPb1State = LOW;
int lastPb2State = LOW;
int lastfloat1State = LOW;
int lastfloat2State = LOW;
unsigned long lastDebounceTime1 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime2 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime3 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime4 = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
TM1637Display display(CLK, DIO); // create instance of the display
void setup() {
pinMode(relayPin,OUTPUT); // making sure machine dont run at the begining
digitalWrite(relayPin,LOW);
pinMode(fillValvePin,OUTPUT); // making sure water fillup valve close at start of machine
digitalWrite(fillValvePin,LOW);
// Serial.begin(9600); // Starting serial communication
pinMode(potPin,INPUT); // setting the pin for input
potValue = map(analogRead(potPin),analogLow,analogHigh,10,30);
pinMode(pb1Pin, INPUT); // setting Push button pins for input.
pinMode(pb2Pin, INPUT);
pinMode(float1Pin, INPUT); // setting pins connected floats for input
pinMode(float2Pin, INPUT);
pinMode(NO_WATER_Pin,OUTPUT); // Setting pin connected to the error light as output
displayTime = potValue * 60; // init the display time value
lastDisplayTime = displayTime;
display.setBrightness(0x0f);
// while(!Serial); // Making sure serial communication work
// Serial.println("Here we go! "); // Printing welcome massege.
// Serial.println("\n");
// Serial.print("Start run time value: ");
// Serial.print(displayTime/60);
// Serial.print(" : ");
// Serial.println(displayTime%60);
}
void loop() {
int myTime = (displayTime/60)*100+displayTime%60;
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (blinkDots == 1) {
display.showNumberDecEx(myTime, (0x80 >> 1), true,4,0);
blinkDots = 0;
} else {
display.showNumberDec(myTime, true,4,0);
blinkDots = 1;
if (machineState == 1) {
displayTime--;
digitalWrite(relayPin,HIGH);
}
}
previousMillis = currentMillis;
}
if (displayTime <= 0) {
displayTime = lastDisplayTime;
machineState = 0;
digitalWrite(relayPin,LOW);
}
// int readFloat = digitalRead(float1Pin);
// if(readFloat == HIGH) {
// digitalWrite(NO_WATER_Pin,HIGH);
// machineStateError = 1;
// } else {
// digitalWrite(NO_WATER_Pin,LOW);
// machineStateError = 0;
// }
int reading1 = digitalRead(pb1Pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading1 != lastPb1State) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != pb1State) {
pb1State = reading1;
if (pb1State == HIGH) {
machineState = !machineState;
}
}
}
lastPb1State = reading1;
int reading2 = digitalRead(pb2Pin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading2 != lastPb2State) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != pb2State) {
pb2State = reading2;
// only toggle the LED if the new button state is HIGH
if (pb2State == HIGH) {
displayTime += 60;
lastDisplayTime += 60;
}
}
}
lastPb2State = reading2;
int reading3 = digitalRead(float1Pin);
// check to see if float changed
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading3 != lastfloat1State) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading3 != float1State) {
float1State = reading3;
// only toggle the LED if the new button state is HIGH
if (float1State == HIGH) {
digitalWrite(NO_WATER_Pin,HIGH);
machineStateError = machineState;
machineState = 0;
digitalWrite(fillValvePin,HIGH); // Open water fillup valve
} else {
digitalWrite(NO_WATER_Pin,LOW);
machineState = machineStateError;
}
}
}
lastfloat1State = reading3;
int readFloat2 = digitalRead(float2Pin);
// check to see if float changed
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (readFloat2 != lastfloat2State) {
// reset the debouncing timer
lastDebounceTime4 = millis();
}
if ((millis() - lastDebounceTime4) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readFloat2 != float2State) {
float2State = readFloat2;
// only toggle the LED if the new button state is HIGH
if (float2State == HIGH) {
digitalWrite(fillValvePin,LOW); // close fill up water valve
}
}
}
lastfloat2State = readFloat2;
}