Skip to content

Commit 6e23f57

Browse files
authored
rewrote for neatnesss
Progress bar doesnt work over 4 segments, hangs
1 parent ca96d3c commit 6e23f57

File tree

2 files changed

+167
-156
lines changed

2 files changed

+167
-156
lines changed

v1.0.ino

-156
This file was deleted.

v2.0.ino

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#include <EEPROM.h>
2+
3+
//pins
4+
int RST = 4; //4017 reset pin 15
5+
int CLK = 5; //4017 clock pin 14
6+
int EN = 6; //4017 enable/clock inhibit pin 13
7+
int PUMP = 7; //Pump control output. Active high.
8+
int UP = 9; //Up bttn input pullup
9+
int DWN = 8; //Down bttn input pullup
10+
int LED = 13; //built in LED
11+
12+
//time vars
13+
unsigned long previousMillis = 0;
14+
unsigned long previousMillisDebounce = 0;
15+
unsigned long previousMillisEEPROM = 0;
16+
unsigned long currentMillis = 0;
17+
int debounceDelay = 300;
18+
long writeEEPROMDelay = 1000;
19+
20+
21+
//misc
22+
int bars = EEPROM.read(0); //reads the EEPROM to recall saved state
23+
int lastBars = bars;
24+
bool pumpActive = 1;
25+
bool userInput = 1;
26+
int iteration = 1;
27+
28+
29+
//config
30+
long waitDuration = (120 * 60000); // How long in minutes to wait
31+
int pumpDuration = (10000); //in seconds, the length of time per bar the pump runs
32+
33+
unsigned long steps = 0;
34+
35+
36+
37+
void setup() {
38+
if (bars > 9) {
39+
bars = 5;
40+
lastBars = bars;
41+
EEPROM.write(0, bars);
42+
}
43+
44+
pinMode(RST, OUTPUT);
45+
pinMode(CLK, OUTPUT);
46+
pinMode(EN, OUTPUT);
47+
pinMode(PUMP, OUTPUT);
48+
pinMode(UP, INPUT_PULLUP);
49+
pinMode(DWN, INPUT_PULLUP);
50+
51+
digitalWrite(RST, LOW);
52+
digitalWrite(CLK, LOW);
53+
digitalWrite(EN, LOW);
54+
55+
displayWrite(bars);
56+
delay(300);
57+
58+
}
59+
60+
void loop() {
61+
currentMillis = millis();
62+
63+
//checks to see if the EEPROM is waiting to be written to
64+
if (lastBars != bars) {
65+
updateEEPROM();
66+
}
67+
68+
//reads button states with debounce
69+
if ((currentMillis - previousMillisDebounce >= debounceDelay) & userInput) {
70+
previousMillisDebounce = currentMillis;
71+
72+
if (digitalRead(UP) == LOW) {
73+
upButton();
74+
}
75+
if (digitalRead(DWN) == LOW) {
76+
downButton();
77+
}
78+
}
79+
80+
//turns on pump
81+
if (pumpActive) {
82+
progressBar();
83+
if (bitRead(PORTD, PUMP) == LOW) { //checks the pump pin to see if its off
84+
digitalWrite(PUMP, HIGH);
85+
digitalWrite(LED, HIGH);
86+
87+
}
88+
if ((currentMillis - previousMillis) >= (pumpDuration * bars)) {
89+
previousMillis = currentMillis;
90+
pumpActive = 0;
91+
userInput = 1; // enables user input
92+
93+
displayWrite(bars);
94+
}
95+
}
96+
97+
//turns off pump
98+
if (!(pumpActive)) {
99+
if (bitRead(PORTD, PUMP) == HIGH) { // checks the pump pin to see if its on
100+
digitalWrite(PUMP, LOW);
101+
digitalWrite(LED, LOW);
102+
}
103+
if ((currentMillis - previousMillis) >= waitDuration) {
104+
previousMillis = currentMillis;
105+
pumpActive = 1;
106+
userInput = 0; // disables user input
107+
iteration = 1;
108+
progressBar();
109+
}
110+
}
111+
}
112+
113+
// triggered when up button is pressed
114+
void upButton() {
115+
if (bars < 9) {
116+
bars = bars + 1;
117+
displayWrite(bars);
118+
updateEEPROM();
119+
}
120+
}
121+
122+
//triggered when down button is pressed
123+
void downButton() {
124+
if (bars > 0) {
125+
bars = bars - 1;
126+
displayWrite(bars);
127+
updateEEPROM();
128+
}
129+
}
130+
131+
//saves new bar values to the EEPROM after a delay (so it doesnt glitch and burn it out)
132+
void updateEEPROM() {
133+
if (currentMillis - previousMillisEEPROM >= writeEEPROMDelay) {
134+
previousMillisEEPROM = currentMillis;
135+
EEPROM.write(0, bars);
136+
lastBars = bars;
137+
}
138+
}
139+
140+
//runs to update the progress bar for watering
141+
void progressBar() {
142+
steps = ((bars * pumpDuration) / 10);
143+
if ((currentMillis - previousMillis) >= (steps * iteration)) {
144+
iteration++;
145+
displayWrite((iteration - 1));
146+
}
147+
}
148+
149+
//displays to the bar graph
150+
void displayWrite(int amount) {
151+
if (amount <= 9) {
152+
bloop(RST);
153+
int effect = 10;
154+
for (int x = 1; x <= amount; x++) {
155+
delay(effect);
156+
bloop(CLK);
157+
effect = effect + 5;
158+
}
159+
}
160+
}
161+
162+
//bloops an output
163+
void bloop(int pin) {
164+
digitalWrite(pin, HIGH);
165+
digitalWrite(pin, LOW);
166+
}
167+

0 commit comments

Comments
 (0)