-
Notifications
You must be signed in to change notification settings - Fork 0
/
neswater.pde
226 lines (198 loc) · 5.67 KB
/
neswater.pde
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
#include <Servo.h>
#include <LiquidCrystal.h>
#define ERROR_WINDOW 50 // +/- this value
#define BUTTONDELAY 200
#define DEBUG_OFF
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// Servo who control flow of sirup & water
Servo sirup1Servo;
Servo sirup2Servo;
Servo sirup3Servo;
Servo waterServo;
int sirup1ServoPin = 2;
int sirup2ServoPin = 3;
int sirup3ServoPin = 4;
int waterServoPin = 5;
int servoOpen = 100;
int servoClose = 50;
int vidangeButtonPin = 6;
int vidangeState = 0;
int ledPin = 5; // LED connected to digital pin 9
int analogPin = 1; // switch circuit input connected to analog pin 3
long buttonLastChecked = 0; // variable to limit the button getting checked every cycle
int position = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output on a PWM capable pin
pinMode(vidangeButtonPin, INPUT);
sirup1Servo.attach(sirup1ServoPin);
sirup2Servo.attach(sirup2ServoPin);
sirup3Servo.attach(sirup3ServoPin);
waterServo.attach(waterServoPin);
// Set all the switch to the close position
sirup1Servo.write(servoClose);
sirup2Servo.write(servoClose);
sirup3Servo.write(servoClose);
waterServo.write(servoClose);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Welcome !");
Serial.begin(115200);
}
void loop()
{
if( buttonLastChecked == 0 ) // see if this is the first time checking the buttons
buttonLastChecked = millis()+BUTTONDELAY; // force a check this cycle
if( millis() - buttonLastChecked > BUTTONDELAY )
{
if(digitalRead(vidangeButtonPin) == HIGH)
{
vidange();
}
// make sure a reasonable delay passed
if( int buttNum = buttonPushed(analogPin) )
{
Serial.print("Button ");
Serial.print(buttNum);
Serial.println(" was pushed.");
}
buttonLastChecked = millis(); // reset the lastChecked value
}
}
/*
*
* analogPin +5 V
* | |
* | \
* ---------------- /
* | \ .5K
* | /
* | \
* |____ \____|
* | SW1 |
* | \
* | /
* | \ .5K
* | /
* | \
* |____ \____|
* | SW2 |
* | |
* | \
* | /
* | \ .5K
* | /
* | \
* |____ \____|
* | SW3 |
* |
* |
* _____
* ___ ground
* _
*
*/
int buttonPushed(int pinNum) {
int val = 0; // variable to store the read value
digitalWrite((14+pinNum), HIGH); // enable the 20k internal pullup
val = analogRead(pinNum); // read the input pin
#ifdef DEBUG_ON
Serial.println(val);
analogWrite(ledPin, val/4); // analog input 0-1023 while analogWrite 0-255
#endif
// we don't use the upper position because that is the same as the
// all-open switch value when the internal 20K ohm pullup is enabled.
//if( val >= 923 and val <= 1023 )
// Serial.println("switch 0 pressed/triggered");
if( val >= (711-ERROR_WINDOW) and val <= (711+ERROR_WINDOW) ) { // 830
#ifdef DEBUG_ON
Serial.println("switch 1 pressed/triggered");
#endif
//position = position + 10;
//Serial.println(position);
// waterServo.write(servoOpen);
// sirup2Servo.write(servoOpen);
// sirup3Servo.write(servoOpen);
// Serial.println(sirup1Servo.read());
sirup1Action();
return 1;
}
else if ( val >= (400-ERROR_WINDOW) and val <= (400+ERROR_WINDOW) ) { // 630
#ifdef DEBUG_ON
Serial.println("switch 2 pressed/triggered");
#endif
//position = position - 10;
//Serial.println(position);
//waterServo.write(servoClose);
// sirup2Servo.write(servoClose);
// sirup3Servo.write(servoClose);
//Serial.println(sirup1Servo.read());
sirup2Action();
return 2;
}
else if ( val >= (14-ERROR_WINDOW) and val <= (14+ERROR_WINDOW) ) { // 430
#ifdef DEBUG_ON
Serial.println("switch 3 pressed/triggered");
#endif
sirup3Action();
return 3;
}
else
return 0; // no button found to have been pushed
}
void sirup1Action()
{
lcd.print("Mint -> Plz wait !");
sirup1Servo.write(servoOpen);
delay(5000);
waterServo.write(servoOpen);
delay(3000);
// close all !
sirup1Servo.write(servoClose);
delay(2000);
waterServo.write(servoClose);
lcd.print("Enjoy !");
}
void sirup2Action()
{
lcd.print("Grenade -> Plz wait !");
sirup2Servo.write(servoOpen);
delay(7000);
waterServo.write(servoOpen);
delay(3000);
// close all !
sirup2Servo.write(servoClose);
delay(2000);
waterServo.write(servoClose);
lcd.print("Enjoy !");
}
void sirup3Action()
{
lcd.print("Rasberry -> Plz wait !");
sirup3Servo.write(servoOpen);
delay(5000);
waterServo.write(servoOpen);
delay(3000);
// close all !
sirup3Servo.write(servoClose);
delay(2000);
waterServo.write(servoClose);
lcd.print("Enjoy !");
}
void vidange()
{
if(!vidangeState){
sirup1Servo.write(servoOpen);
sirup2Servo.write(servoOpen);
sirup3Servo.write(servoOpen);
waterServo.write(servoOpen);
vidangeState = 1;
}
else{
sirup1Servo.write(servoClose);
sirup2Servo.write(servoClose);
sirup3Servo.write(servoClose);
waterServo.write(servoClose);
vidangeState = 0;
}
}