-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuimon.ino
214 lines (192 loc) · 5.52 KB
/
suimon.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
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <ArduinoJson.h>
#include <FirebaseHTTPClient.h>
#define STEPPER_PIN_1 D2
#define STEPPER_PIN_2 D3
#define STEPPER_PIN_3 D4
#define STEPPER_PIN_4 D5
#define FIREBASE_HOST "suimon2.firebaseio.com"
#define FIREBASE_AUTH "x3gMbN3tCSl0xmuWw37buwlm7geKAC8jkX8NWrUW"
#define WIFI_SSID "cobacoba" // input your home or public wifi name
#define WIFI_PASSWORD "nunutaeteros" //password of wifi ssid
int step_number = 0;
const int trigPin = D0; //D4
const int echoPin = D1; //D3
long duration;int distance;
int adwal, gate, lastgate, lastdist, distPercent, gateConv;
void ultrasonik(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Firebase.setInt("ultrasonik",distance);
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
}
}
void AdwalFunction(){
adwal = Firebase.getInt("adwal");
if(adwal == 1){
Serial.print("Mode :");
Serial.println(adwal);
}else{
Serial.print("Mode :");
Serial.println(adwal);
}
}
void GateFunction(){
gate = Firebase.getInt("gate");
Serial.print("Gate Open :");
Serial.print(gate);
Serial.println("%");
}
void manual(){
if(gate != lastgate){
if(lastgate >= gate){
for(int a = 0; a <= abs(lastgate - gate) *50; a++){
OneStep(true);
delay(2);
}
}
else if(lastgate <= gate){
for(int a = 0; a <= abs(lastgate - gate) *50; a++){
OneStep(false);
delay(2);
}
}
lastgate = gate;
}
}
void automatic(){
if(abs(distance-30) != lastdist){
if(lastdist >= abs(distance-30)){
for(int a = 0; a <= abs(lastdist - abs(distance-30)) *166; a++){
OneStep(true);
delay(2);
}
}
else if(lastdist <= abs(distance-30)){
for(int a = 0; a <= abs(lastdist - abs(distance-30)) *166; a++){
OneStep(false);
delay(2);
}
}
lastdist = abs(distance-30);
}
}
void OneStep(bool dir){
if(dir){
switch(step_number){
case 0:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
}
}else{
switch(step_number){
case 0:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
}
}
step_number++;
if(step_number > 3){
step_number = 0;
}
}
void setup() {
Serial.begin(115200);
//mOTOR
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
//ultrasonic
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// put your setup code here, to run once:
delay(1000);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
}
}
void loop() {
// put your main code here, to run repeatedly:
ultrasonik();
AdwalFunction();
GateFunction();
if(adwal == 1){
automatic();
distPercent = lastdist / 30 * 100;
lastgate = distPercent;
}else{
manual();
gateConv = lastgate / 100 *30;
lastdist = gateConv;
}
}