-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphatest.ino
372 lines (343 loc) · 11.3 KB
/
alphatest.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <Wire.h>
#include <RTClib.h>
#include<LiquidCrystal.h>
#include <Servo.h>
int pin_button2 = 2; //주기 스위치
int pin_button3 = 3; //급여량 스위치
int pin_button4 = 4; //모터 제어 스위치
int pin_button5 = 5; //스피커 on/off 스위치
int state_button;
LiquidCrystal lcd(44,45,46,47,48,49); //lcd출력
int period = 0; //주기
int amount = 0; //급여량
int go = 0; //모터제어
int sound = 0; //스피커 소리
bool stateButton2; //주기
bool stateButton3; //급여량
bool stateButton4; //모터제어
bool stateButton5; //스피커 스위치
int state_previous2 = 0;
int state_previous3 = 0;
int state_previous4 = 0;
int state_previous5 = 0;
unsigned long interval; //가변저항
Servo myServo; //모터 제어
int angle=0; //모터 이동 범위 변수
int servoPin = 11; //모터
int later; // //delay 변수
int mot; //사료 양 변수
int k; //고주파 변수
unsigned long time_previous, time_current;
RTC_DS1307 rtc;
unsigned long time1;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
/*
DateTime now = rtc.now();
if(1<=now.month() || now.month()<=9){
String months = "0"+String(now.month());}
else
String months = String(now.month());
*/
void setup() {
pinMode(A0, INPUT); //가변저항
Serial.begin(9600); //아두이노 출력
lcd.begin(16,2); //lcd 출력
pinMode(pin_button2, INPUT); //2번 스위치
pinMode(pin_button3, INPUT); //3번 스위치
pinMode(pin_button4, INPUT); //4번 스위치
pinMode(pin_button5, INPUT);
myServo.attach(servoPin); //모터
time_previous = millis();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}}
void Speaker(int sound, unsigned long interval ){
switch(sound){
case 0 :{
}
case 1 :{
lcd.setCursor(0,0);
lcd.print("Speaker on / off");
if(interval <= 50){
k = 0;
lcd.setCursor(0,1);
lcd.print("Speaker off");
}
else{
k = 1;
lcd.setCursor(0,1);
lcd.print("Speaker on ");
} }}}
void Time(){
DateTime now = rtc.now();
Serial.println("Current Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
lcd.setCursor(0,0);
lcd.print(" "+String(now.year())+"/"+String(now.month())+"/"+String(now.day())+"/"+String(daysOfTheWeek[now.dayOfTheWeek()])+" ");
lcd.setCursor(0,1);
lcd.print(" "+String(now.hour())+" : "+String(now.minute())+" : "+String(now.second())+" ");
delay(1000);
}
void Motor(int go, int mot, int later, unsigned long time_current, int k){ //모터제어(go 스위치,사료양, 대기시간, 스피커 on(1)/off(0))
Serial.println("go read");
switch(go){
case 0: {
lcd.setCursor(0,0);
lcd.print("switch is off");
break;
}
case 1: {
if(mot == 100){ //사료양 == 100g
angle = 60; //각도 60도
/*
int i=1;
if( i != 0){
Serial.println("ifmoon dongjak");
for(i = 177; i > 0 ; i--){
lcd.setCursor(0,0);
Serial.println("i value : "+String(i));
lcd.print(" Period: "+String(i)+" ");}}
else{
i = 177;
} */
lcd.setCursor(0,1);
lcd.print(" food : 100 g ");
int delayvalue = 3000; //later * 60 * 1000
static int seconds= delayvalue/1000;
lcd.setCursor(0,0);
lcd.print(" seconds : "+String(seconds)+" ");
if(time_current - time_previous >= 1000){
seconds--; // 초 감소
//if(k == 1)
//soundenalbe();
Serial.println(" seconds : "+String(seconds)+" ");
lcd.print(" seconds : "+String(seconds)+" ");
time_previous = time_current;
if(seconds==0){
lcd.setCursor(0,0);
lcd.print(" feeding! ");
seconds=delayvalue/1000;
for(int i = 0; i < angle; i++){
myServo.write(i);
delay(5);
}for(int i = angle ; i > 0; i--){
myServo.write(i);
delay(5);
}
} } }
else if(mot == 150){ //사료양 == 150g
angle = 90; //각도 90도
lcd.setCursor(0,1);
lcd.print(" food : 150 g ");
int delayvalue = 5000; //later * 60 * 1000
static int seconds= delayvalue/1000;
lcd.setCursor(0,0);
lcd.print(" seconds : "+String(seconds)+" ");
if(time_current - time_previous >= 1000){
seconds--; // 초 감소
Serial.println(" seconds : "+String(seconds)+" ");
lcd.print(" seconds : "+String(seconds)+" ");
time_previous = time_current;
if(seconds==0){
lcd.setCursor(0,0);
lcd.print(" feeding! ");
seconds=delayvalue/1000;
for(int i = 0; i < angle; i++){
myServo.write(i);
delay(5);
}for(int i = angle ; i > 0; i--){
myServo.write(i);
delay(5);
}} }}
else if(mot == 200){ //사료양 == 200g
angle = 120; //각도 120도
lcd.setCursor(0,1);
lcd.print(" food : 200 g ");
int delayvalue = 7000; //later * 60 * 1000
static int seconds= delayvalue/1000;
lcd.setCursor(0,0);
lcd.print(" seconds : "+String(seconds)+" ");
if(time_current - time_previous >= 1000){
seconds--; // 초 감소
Serial.println(" seconds : "+String(seconds)+" ");
lcd.print(" seconds : "+String(seconds)+" ");
time_previous = time_current;
if(seconds==0){
lcd.setCursor(0,0);
lcd.print(" feeding! ");
seconds=delayvalue/1000;
for(int i = 0; i < angle; i++){
myServo.write(i);
delay(5);
}for(int i = angle ; i > 0; i--){
myServo.write(i);
delay(5);
}}}}
else if(mot == 250){ //사료양 == 250g
angle = 160; //각도 160도
lcd.setCursor(0,1);
lcd.print(" food : 250 g ");
int delayvalue = 10000; //later * 60 * 1000
static int seconds= delayvalue/1000;
lcd.setCursor(0,0);
lcd.print(" seconds : "+String(seconds)+" ");
if(time_current - time_previous >= 1000){
seconds--; // 초 감소
Serial.println(" seconds : "+String(seconds)+" ");
lcd.print(" seconds : "+String(seconds)+" ");
time_previous = time_current;
if(seconds==0){
lcd.setCursor(0,0);
lcd.print(" feeding! ");
seconds=delayvalue/1000;
for(int i = 0; i < angle; i++){
myServo.write(i);
delay(5);
}for(int i = angle ; i > 0; i--){
myServo.write(i);
delay(5);
}}}
else{
angle = 0; }break;}
}
}}
void P_LCD(int period, unsigned long interval){ //주기 설정(주기 스위치 on/off, 가변저항)
switch(period){
case 0:{
lcd.setCursor(0,0);
lcd.print("switch is off");
break;
}
case 1:{
lcd.setCursor(0,0);
lcd.print(" How often ");
if(interval >= 0 and interval < 25) //가변저항
{ lcd.setCursor(0,1); //lcd출력
lcd.print(" 15 minute ");
later = 15;} //delay 변수 설정
else if(interval >= 25 and interval < 50) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 25 minute ");
later = 25;} //delay 변수 설정
else if(interval >= 50 and interval < 75) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 35 minute ");
later = 35;} //delay 변수 설정
else if(interval >= 75 and interval <99) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 45 minute ");
later = 45;} //delay 변수 설정
else
{lcd.setCursor(0,1); //lcd출력
lcd.print(" e r r o r ! ");}
break;
}}
}
void A_LCD(int amount, unsigned long interval){ //사료량 설정(스위치 on/off , 가변저항)
switch(amount){
case 0:{
lcd.setCursor(0,0);
lcd.print("switch is off");
break;
}
case 1:{
lcd.setCursor(0,0);
lcd.print(" Amount of food ");
if(interval >= 0 and interval < 25) //가변저항
{ lcd.setCursor(0,1); //lcd출력
lcd.print(" 100 g ");
mot = 100;}
else if(interval >= 25 and interval < 50) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 150 g ");
mot = 150;}
else if(interval >= 50 and interval < 75) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 200 g ");
mot = 200;}
else if(interval >= 75 and interval <99) //가변저항
{lcd.setCursor(0,1); //lcd출력
lcd.print(" 250 g ");
mot = 250;}
else
{lcd.setCursor(0,1); //lcd출력
lcd.print(" e r r o r ! ");}
break;
}}
}
void loop() {
time_current = millis();
stateButton3 = digitalRead(pin_button3);
stateButton2 = digitalRead(pin_button2);
stateButton4 = digitalRead(pin_button4);
stateButton5 = digitalRead(pin_button5);
if(stateButton2 == 1){ //주기 스위치
if(state_previous2 == 0){
period = period + 1;
state_previous2 = 1;
}}else{
state_previous2 = 0;}
if(stateButton3 == 1){ //사료량 스위치
if(state_previous3 == 0){
amount = amount + 1;
state_previous3 = 1;
}}else{
state_previous3 = 0;}
if(stateButton4 == 1){ //모터 스위치
if(state_previous4 == 0){
go = go + 1;
state_previous4 = 1;
}}else{
state_previous4 = 0;}
if(stateButton5 == 1){ //스피커 스위치
if(state_previous5 == 0){
sound = sound + 1;
state_previous5 = 1;
}}else{
state_previous5 = 0;}
sound = sound%2; //on/off
go = go%2; //on/off
amount = amount%2; //on/off
period = period%2; //on/off
Serial.print("go = "+String(go));
Serial.print("amount = "+String(amount));
Serial.println("period = "+String(period));
if(period == 1 and amount == 0 and go == 0 and sound == 0){
P_LCD(period, interval); // later(주기 변수) 값을 들고온다
}
else if(amount ==1 and period == 0 and go == 0 and sound == 0){
A_LCD(amount, interval); // mot(사료량 변수) 값을 들고온다
}
else if(period == 0 and amount == 0 and go == 1 and sound == 0){
Motor(go, mot, later,time_current,k);//모터스위치, 사료량 변수, 주기 변수
}
else if(period == 0 and amount == 0 and go == 0 and sound == 0){
Time();
}
else if(period == 0 and amount == 0 and go == 0 and sound == 1){
Speaker(sound, interval);
}
else{lcd.clear();}
Serial.print("later = "+String(later));
Serial.println("mot = "+String(mot));
int adc = analogRead(A0); //가변저항 읽기
interval = map(adc,0,1023,0,100); //범위를 0 ~ 100
}