-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilaGuard.ino
More file actions
299 lines (242 loc) · 7.72 KB
/
FilaGuard.ino
File metadata and controls
299 lines (242 loc) · 7.72 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
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
//ELEG398 Engineering Entrepreneurship and Design Challenges (University of Delaware)
//Students: Christopher Yonek, Justin Clavette, Steve Shreve, Omar Romero, and Karl Antonio
//Assignment: This is the driver code for the FilaGuard 3D printer spool holder
//Devices Used: Arduino Pro Micro, HX711, Rotary Encoder(WYTP68), DHT22, and 0.96" SSD1306 128X64 OLED LCD Display
//Date: April 2022
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <elapsedMillis.h>
#include <EEPROM.h>
#include "HX711.h"
const int LOADCELL_DOUT_PIN = 2; // HX711 circuit wiring
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define CLK 10
#define DT 11
#define SW 12
#define DHTTYPE DHT22
#define DHTPIN 7
elapsedMillis debouce; //When click is 1 should counter reset back to 0.
elapsedMillis reading;
uint8_t currentCLK;
uint8_t lastCLK;
int8_t counter = 0;
uint8_t buttonState;
uint8_t lastButtonState = 0;
uint8_t debouceDelay = 50;
uint8_t click = 0;
uint8_t menuLocation = 0;
uint8_t menuLength = 10;
uint8_t selectedSpool = 0;
uint8_t currentSpool = 0;
float temp = 0;
float humidity = 0;
DHT dht (DHTPIN, DHTTYPE);
uint8_t maxSpools = 20;
uint16_t readingDelay = 3000;
struct spool{
char name[16];
uint16_t weight;
};
spool storedSpools[20];
#define OLED_RESET 4 //Reset pin # (or -1 if sharing Arduino reset pin)--CHANGE 4 to -1 --
#define SCREEN_ADDRESS 0x3c //See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32--CHANGE 0x3D to 0X3c--
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 //Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
0b00000001, 0b11000000,
0b00000001, 0b11000000,
0b00000011, 0b11100000,
0b11110011, 0b11100000,
0b11111110, 0b11111000,
0b01111110, 0b11111111,
0b00110011, 0b10011111,
0b00011111, 0b11111100,
0b00001101, 0b01110000,
0b00011011, 0b10100000,
0b00111111, 0b11100000,
0b00111111, 0b11110000,
0b01111100, 0b11110000,
0b01110000, 0b01110000,
0b00000000, 0b00110000 };
void setup() {
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); //Don't proceed, loop forever
}
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
lastCLK = digitalRead(CLK);
reading = 0;
dht.begin();
display.display();
delay(2000); //Pause for 2 seconds
display.clearDisplay(); //Clear the buffer
}
void loop() {
if(menuLocation == 0){
menuLength = 10;
if(reading >= readingDelay){
Serial.println("Readings:");
temp = dht.readTemperature(true);
humidity = dht.readHumidity();
display.setTextSize(1); //Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); //Draw white text
display.setCursor(10,0); //(0, 0) = Start at top-left corner (0,10) = center
if (scale.is_ready()) {
long reading = (scale.read()-122600)/(-1080);
display.clearDisplay();
Serial.print("HX711 reading before if statements: ");
Serial.println(reading);
//MAKING THE DISPLAY ONLY SHOW A MAX OF 1000g-------------------------------
//This code is still incomplete (need to hard code extra weight of the stand and the spool thats not the PLA cylinder)
int spoolStand = 86;
int spoolCyl = 0;
reading = reading - (spoolStand + spoolCyl);
if (reading > 1000){ //To catch if the reading is reading over 1000g
int extra = reading - 1000;
reading = reading - extra;
}
if(reading <= 0){ //To catch if there's a negative value as the reading
reading = 0;
}
//--------------------------------------------------------------------------
display.print(F("Spool Weight: "));
display.print(reading);
display.println(F("g"));
display.setCursor(10,10);
display.print(F("Temp: "));
display.print(temp);
display.println(F("F: "));
display.setCursor(10,20);
display.print(F("Humidity: "));
display.print(humidity);
display.print(F("%: "));
} else {
Serial.println("HX711 not found.");
}
reading = 0;
}
if(click == 1) {
menuLocation = 1;
counter = 0;
click = 0;
}
} else if (menuLocation == 1) {
menuLength = maxSpools + 1;
if(counter != (menuLength - 1)) {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Spool ");
display.print(counter);
} else {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Back");
}
if((counter == (menuLength - 1)) && (click == 1)){
menuLocation = 0;
counter = 0;
click = 0;
} else if(click == 1) {
currentSpool = counter;
menuLocation = 2;
counter = 0;
click = 0;
}
} else if (menuLocation == 2) {
menuLength = 3;
if(counter == 0) {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Set as default");
} else if (counter == 1) {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Calibrate");
} else {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Back");
}
if((counter == (menuLength - 1)) && (click == 1)) {
menuLocation = 1;
counter = 0;
click = 0;
} else if ((counter == 1) && (click == 1)) {
menuLocation = 3;
counter = 0;
click = 0;
} else if ((counter == 0) && (click == 1)) {
selectedSpool = currentSpool;
click = 0;
}
} else if (menuLocation == 3) {
menuLength = 3;
if(counter == 0) {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Full spool cali");
} else if (counter == 1) {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Empty Spool cali");
} else {
display.clearDisplay();
display.setCursor(0, 10);
display.print("Back");
}
if((counter == (menuLength - 1)) && (click == 1)) {
menuLocation = 1;
counter = 0;
click = 0;
} else if ((counter == 0) && (click == 1)) {
delay(100);
long reading = (scale.read()-122600)/(-1080);
delay(100);
storedSpools[currentSpool].weight = reading - 1000;
} else if ((counter == 1) && (click == 1)) {
delay(100);
long reading = (scale.read()-122600)/(-1080);
delay(100);
storedSpools[currentSpool].weight = reading;
}
}
currentCLK = digitalRead(CLK);
if((currentCLK != lastCLK) && currentCLK == 1) {
if(digitalRead(DT) == currentCLK) {
counter++;
} else {
counter--;
}
}
if(counter >= menuLength) {
counter = 0;
} else if (counter < 0) {
counter = menuLength;
}
buttonState = !digitalRead(SW);
if(buttonState != lastButtonState) {
debouce = 0;
}
if(buttonState != lastButtonState) {
if(buttonState == 1){
click = 1;
}
}
lastButtonState = buttonState;
lastCLK = currentCLK;
display.display();
}