-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (175 loc) · 6.63 KB
/
main.cpp
File metadata and controls
205 lines (175 loc) · 6.63 KB
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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "utils.h"
#define MOTOR_PIN D7
#define SWITCH_PIN D0
//#define STOCK
struct selector_pins motor_pins { D3, D2, D1 };
struct selector_pins switch_pins { D4, D5, D6 };
struct switch_state state_switch { D0, LOW, 0 };
int gen_time(int type);
void dispense_time(int type);
void dispense_switch();
void unit_select(int n);
void move(int n);
void man_int(int n);
int inventory[5] = {3, 3, 3, 3, 3};
int times[5] = {1200, 1200, 1200, 1200, 1200};
int dhappy = 0;
int dsad = 0;
int dangry = 0;
int danxious = 0;
static const char cont[] PROGMEM = "<a href=\"/move?n=0\">Motor 1</a> <a href=\"/move?n=1\">Motor 2</a> <a href=\"/move?n=2\">Motor 3</a> <a href=\"/move?n=3\">Motor 4</a> <a href=\"/move?n=4\">Motor 5</a> <a href=/stop>Stop All</a>";
static const char sets[] PROGMEM = "<a href=\"fill?n=0\">Snack 1</a> <a href=\"fill?n=1\">Snack 2</a> <a href=\"fill?n=2\">Snack 3</a> <a href=\"fill?n=3\">Snack 4</a> <a href=\"fill?n=4\">Snack 5</a>";
static const char page[] PROGMEM = "<title>Website</title><h1>Moodi</h1><h2>Select your emotion</h2><div><hr><form action=http://192.168.4.1/m_one><input type=submit value=\"Happy :3\"></form><hr><form action=http://192.168.4.1/m_two><input type=submit value=\"Angry >:3\"></form><hr><form action=http://192.168.4.1/m_three><input type=submit value=\"Sad :c\"></form><hr><form action=http://192.168.4.1/m_four><input type=submit value=\"Anxious :/\"></form><hr></div><style>h1{text-align:center;font-family:Verdana,sans-serif;color:#8b4513;margin-bottom:0}h2{text-align:center;margin-bottom:0}body{display:grid;align-items:center;justify-content:center;background-color:#ffebcd;height:100vh}input{color:#8b4513;font-size:30px;width:100%;background-color:#ffebcd;border:0}hr{width:80%;height:0}div{width:800px;height:70vh;display:grid}</style>";
static const char tim[] PROGMEM = "<form action=time><input type=number id=happy placeholder=Happy value=1200> <input type=number id=angry placeholder=Angry value=1200> <input type=number id=sad placeholder=Sad value=1200> <input type=number id=anxious placeholder=Anxious value=1200> <input type=number id=dhappy placeholder=\"Happy Decrement\"value=0> <input type=number id=dangry placeholder=\"Angry Decrement\"value=0> <input type=number id=dsad placeholder=\"Sad Decrement\"value=0> <input type=number id=danxious placeholder=\"Anxious Decrement\"value=0> <input type=submit></form>";
ESP8266WebServer server(80);
void setup() {
// REQUIRED
Serial.begin(9600);
// Pinmode nearly all of them cuz idk
pinMode(SWITCH_PIN, INPUT);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(motor_pins.a, OUTPUT);
pinMode(motor_pins.b, OUTPUT);
pinMode(motor_pins.c, OUTPUT);
pinMode(switch_pins.a, OUTPUT);
pinMode(switch_pins.b, OUTPUT);
pinMode(switch_pins.c, OUTPUT);
MOTOR_OFF(MOTOR_PIN);
state_switch.time = millis();
// Forever code now
bool wifi_res = WiFi.softAP("RinsingMachine", "", 1, 0, 2);
if (wifi_res) {
Serial.println("WIFI READY");
}else {
Serial.println("WIFI FAILED");
}
server.on("/", []() {
server.send(200, "text/html", FPSTR(page));
});
server.on("/m_one", []() {
move(0);
Serial.println("Motor 1");
});
server.on("/m_two", []() {
move(1);
Serial.println("Motor 2");
});
server.on("/m_three", []() {
move(2);
Serial.println("Motor 3");
});
server.on("/m_four", []() {
move(3);
Serial.println("Motor 4");
});
server.on("/m_five", []() {
move(4);
Serial.println("Motor 5");
});
server.on("/fill", []() {
char buf[32];
int n = server.arg("n").charAt(0) - (char)48;
inventory[n]++;
snprintf(buf, 32, "%d %d %d %d %d", inventory[0], inventory[1], inventory[2], inventory[3], inventory[4]);
server.send(200, "text/plain", buf);
});
server.on("/stock", []() {
char buf[32];
snprintf(buf, 32, "%d %d %d %d %d", inventory[0], inventory[1], inventory[2], inventory[3], inventory[4]);
server.send(200, "text/plain", buf);
});
server.on("/delay", []() {
server.send(200, "text/html", FPSTR(tim));
});
server.on("/time", []() {
times[0] = server.arg("happy").toInt();
times[1] = server.arg("angry").toInt();
times[2] = server.arg("sad").toInt();
times[3] = server.arg("anxious").toInt();
dhappy = server.arg("dhappy").toInt();
dangry = server.arg("dangry").toInt();
dsad = server.arg("dsad").toInt();
danxious = server.arg("danxious").toInt();
});
server.on("/set", []() { server.send(200, "text/html", sets); });
server.on("/man", []() {
server.send(200, "text/html", FPSTR(cont));
});
server.on("/move", []() {
int n = server.arg("n").charAt(0) - (char)48;
select_channel(motor_pins, n);
MOTOR_ON(MOTOR_PIN);
server.send(204, emptyString, emptyString);
});
server.on("/stop", []() {
MOTOR_OFF(MOTOR_PIN);
});
server.begin();
}
void move(int n) {
#ifdef STOCK
man_int(n);
#endif
select_channel(motor_pins, n);
dispense_time(n);
}
void loop() {
server.handleClient();
}
void unit_select(int n) {
select_channel(motor_pins, n);
select_channel(switch_pins, n);
}
// "detect if switch is pressed" - algorithm
void dispense_switch() {
int current = 0;
MOTOR_ON(MOTOR_PIN);
while (1) {
state_switch = monitor_switch(state_switch);
if (current == 0 && read_switch(state_switch) == ON) {
current = 1;
MOTOR_OFF(MOTOR_PIN);
break;
}else if (current == 1 && read_switch(state_switch) == OFF) {
current = 0;
}
yield();
}
}
// rotate motor for n milliseconds
void dispense_time(int type) {
MOTOR_OFF(MOTOR_PIN);
int start_time = millis();
int delay = gen_time(type);
MOTOR_ON(MOTOR_PIN);
while (1) {
if (millis() - start_time >= (unsigned long)(delay)) {
MOTOR_OFF(MOTOR_PIN);
break;
}
yield();
}
}
// use for calculating time, unit in ms
// time to complete 1 revolution - no load 800ms
int gen_time(int type) {
#ifdef STOCK
times[0] -= dhappy;
times[1] -= dangry;
times[2] -= dsad;
times[3] -= dhappy;
#endif
return times[type];
}
void man_int(int n) {
if (inventory == 0) {
server.send(418, "text/plain", "Out of Order :(");
Serial.println("Out");
}else {
inventory[n]--;
server.send(200, "text/html", FPSTR(page));
}
}