-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlateral-root-induction-with-arduino.ino
213 lines (151 loc) · 4.82 KB
/
lateral-root-induction-with-arduino.ino
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
/*
Name: Gravistimulation device
Purpose: Control the rotation of a servo to 170 degrees after a countdown of at least 30 minutes, up to 24 hours maximum.
The circuit:
one or two servos connected to D9
an LCD screen connected to D4, D5, D6, D7, D11 and D12
an LED connected to D3 with a 220Ω resistor
a pushbutton connected to D2
two potentiometers (one for the LCD screen backlight, the other for setting the countdown)
two capacitors
Created 14 March 2017
@author Marion Louveaux
Modified 25 September 2017
By Marion Louveaux
@version 1.0 25/09/2017
References:
This programm is based on:
Project n°5 "Mood cue" from Arduino starter kit book (servo)
Project n°11 "Crystal Ball" from Arduino starter kit book (LCD screen)
https://openclassrooms.com/courses/programmez-vos-premiers-montages-avec-arduino/le-bouton-poussoir (button/light switch)
http://colin.pitrat.free.fr/?p=393 (countdown system)
*/
#include <Servo.h>
#include <LiquidCrystal.h>
Servo myServo;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int const potPin = A0;
int potVal;
int angle;
double duration;
unsigned long dur_mill = 0;
// Countdown variables
#define SECONDS_IN_HOUR 3600.0 // N.B.: Replace 3600.0 by 30.0 to debug the code/test the setup (1mn will last in 1 second)
unsigned long end_millis = 0; // Time in milliseconds at which countdown stops
double remainingSeconds;
double RMinutes;
double remainingMinutes;
double RHours;
double remainingHours;
// Button variables
int pinButton, pinLed;
boolean lightingStatus;
void setup() {
myServo.attach(9);
Serial.begin(9600);
// Initialisation of the button and LED variables
pinButton = 2;
pinLed = 3;
lightingStatus = 0;
angle = 0;
// Definition of modes for the button and the LED
pinMode(pinButton, INPUT_PULLUP);
pinMode(pinLed, OUTPUT);
// Servo reset
myServo.write(angle);
// Start display
lcd.begin(16, 2);
lcd.print("Hello");
lcd.setCursor(0, 1);
lcd.print("AG Maizel :)");
delay(3000);
lcd.clear();
lcd.print("Set time:");
} // end of void setup
void loop() {
// Read and store button state
boolean pinButtonStatus = digitalRead(pinButton);
Serial.println("pinButtonStatus: ");
Serial.println(pinButtonStatus);
// Conditional test
if (!pinButtonStatus) // if the button is pushed (in this case pin is set to 0 -INPUT_PULLUP mode-)
{
if (lightingStatus) // if lightingStatus equal to 1
{
lightingStatus = 0; // then lightingStatus is set to 0
}
else
{
lightingStatus = 1; // otherwise lightingStatus is set to 1
}
}
Serial.print("Light: ");
Serial.print(lightingStatus);
Serial.print("end_millis: ");
Serial.print(end_millis);
Serial.print("millis: ");
Serial.print(millis());
if (lightingStatus) // test if lightingStatus is set to 1
{
digitalWrite(pinLed, HIGH); // LED is turned on
// Display is refreshed and modified
lcd.clear();
lcd.print("Time left:");
if (millis() >= end_millis)
{
angle = 170; // above 170 degrees, servo is noisy
duration = 0;
myServo.write(angle);
lcd.setCursor(0, 1);
lcd.print("Done!");
} else {
RHours = (end_millis - millis()) / (1000.0 * SECONDS_IN_HOUR);
remainingHours = floor(RHours);
RMinutes = (RHours - remainingHours) * 60;
remainingMinutes = floor(RMinutes);
remainingSeconds = floor( (RMinutes - remainingMinutes) * 60 );
lcd.clear();
lcd.print("Time left:");
lcd.setCursor(0, 1);
lcd.print(static_cast<unsigned long>(remainingHours));
lcd.print("h ");
lcd.print(static_cast<unsigned long>(remainingMinutes));
lcd.print("mn ");
lcd.print(static_cast<unsigned long>(remainingSeconds));
lcd.print("s");
Serial.print(", RHours: ");
Serial.print(RHours);
Serial.print(", remainingHours: ");
Serial.print(remainingHours);
Serial.print(", RMinutes: ");
Serial.print(RMinutes);
Serial.print(", remainingMinutes: ");
Serial.print(remainingMinutes);
Serial.print(", remainingSeconds: ");
Serial.print(remainingSeconds);
}
delay(200);
}
else
{
digitalWrite(pinLed, LOW); // LED is turned off
angle = 0;
myServo.write(angle);
// Programming duration before gravistimulation
dur_mill = map(potVal, 0, 1023, 1, 48); // from 30 minutes to 24h, with 30 minutes increment
duration = dur_mill / 2.00;
Serial.print(", dur_mill: ");
Serial.print(dur_mill);
Serial.print(", duration: ");
Serial.print(duration);
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
end_millis = millis() + duration * SECONDS_IN_HOUR * 1000;
lcd.clear();
lcd.print("Set time (hours)");
lcd.setCursor(0, 1);
lcd.print(duration);
delay(200);
}
} // end of void loop