-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispenser.cpp
executable file
·110 lines (93 loc) · 2.81 KB
/
dispenser.cpp
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
/*
* Arduino-Cocktail
*
* Dispenser
* Copyright 2017 by Thomas Buck <[email protected]>
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return. Thomas Buck
* ----------------------------------------------------------------------------
*/
#include <Arduino.h>
#include "dispenser.h"
static bool dispensing = false;
static unsigned long startTime = 0;
static int timings[VALVE_COUNT] = { 0, 0, 0, 0 };
int valves[VALVE_COUNT] = { V1_OUTPUT, V2_OUTPUT, V3_OUTPUT, V4_OUTPUT };
void initDispenser(void) {
for (int i = 0; i < VALVE_COUNT; i++) {
// Immediately disable valves
pinMode(valves[i], OUTPUT);
digitalWrite(valves[i], HIGH);
}
}
void startDispensing(int t1, int t2, int t3, int t4) {
startTime = millis();
timings[0] = t1;
timings[1] = t2;
timings[2] = t3;
timings[3] = t4;
Serial.print(F("Starting dispenser: "));
Serial.print(t1);
Serial.print(F(", "));
Serial.print(t2);
Serial.print(F(", "));
Serial.print(t3);
Serial.print(F(", "));
Serial.println(t4);
dispensing = false;
for (int i = 0; i < VALVE_COUNT; i++) {
if (timings[i] > 0) {
digitalWrite(valves[i], LOW); // enable active-low
dispensing = true;
}
}
}
bool isDispensing(void) {
return dispensing;
}
int dispensionProgress(void) {
if (!dispensing) {
return 0;
}
int maxTime = 0;
for (int i = 0; i < VALVE_COUNT; i++) {
if (timings[i] > maxTime) {
maxTime = timings[i];
}
}
unsigned long timeRunning = millis() - startTime;
uint32_t percent = (uint32_t)timeRunning * 100 / (uint32_t)maxTime;
return percent;
}
void dispenserThread(void) {
if (!dispensing) {
return;
}
int running = 0;
unsigned long time = millis();
for (int i = 0; i < VALVE_COUNT; i++) {
if (timings[i] > 0) {
if (((time - startTime) > timings[i])) {
if (digitalRead(valves[i]) == LOW) {
Serial.print(F("Dispensing on valve "));
Serial.print(i + 1);
Serial.print(F(" finished after "));
Serial.print(time - startTime);
Serial.println(F("ms..."));
}
// valve is finished
digitalWrite(valves[i], HIGH); // disable, active-low
} else {
// valve is dispensing
running++;
}
}
}
if (running == 0) {
dispensing = false;
}
}