-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackCellu8g2Switch.ino
192 lines (160 loc) · 5.22 KB
/
SlackCellu8g2Switch.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
/*
Based on Slackcell code by Markus Rampp (copyright 2020)
Designed for use with ESP32 using SSD1306_128x64 OLED Display
Tested with and recommended for Heltec WifiKit32 controller
*/
#include "HX711.h" //library for loadcell amplifier
#include <U8g2lib.h> //library for OLED
// libraries for SD card
#include "FS.h"
#include "SD.h"
#include <spi.h> //also needed for some OLEDs
const long baud = 115200;
// HX711 circuit wiring
const int LOADCELL_SCK_PIN = 26;
const int LOADCELL_DOUT_PIN = 25;
const long LOADCELL_OFFSET = 2330;
const long LOADCELL_DIVIDER_N = -232;
const long LOADCELL_DIVIDER_kg = LOADCELL_DIVIDER_N * 9.81;
const long LOADCELL_DIVIDER_lb = LOADCELL_DIVIDER_N * 4.448;
// SD Card circuit wiring
#define SD_CS 2
HX711 loadcell; //setup HX711 object
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /*reset= */ 16, /*clock=*/ 15, /*data=*/ 4); //setup display connection
unsigned long timestamp = 0;
long maxForce = 0;
long force = -1;
long prevForce = -100;
const int Switch = 22;
//bool recording = false;
//char prev_cmd = '0';
String sdMessage;
int readingID = 0;
unsigned long timeNow = 0;
void setup() {
Serial.begin(baud);
Serial.println("Welcome to SlackCell!");
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
// This statement will declare pin 15 as digital input
pinMode(Switch, INPUT);
u8g2.setBusClock(1000000);
u8g2.begin();
u8g2.setPowerSave(0);
u8g2.setFont(u8g2_font_inb21_mf);
u8g2.setFontPosTop();
u8g2.clearBuffer();
u8g2.drawStr(0, 0, "SLACK");
u8g2.drawStr(0, 36, "CELL");
u8g2.sendBuffer();
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
loadcell.set_offset(LOADCELL_OFFSET);
loadcell.set_scale(LOADCELL_DIVIDER_N);
force = loadcell.get_units(30);
if (force < 30) {
loadcell.tare();
force = 0;
}
delay(2000);
u8g2.clearBuffer();
// Initialize SD card
SD.begin(SD_CS);
if(!SD.begin(SD_CS)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("ERROR - SD card initialization failed!");
return;
}
// If the data.txt file doesn't exist
// Create a file on the SD card and write the data labels
File file = SD.open("/data.txt");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "/data.txt", "Reading ID, Time (ms), Force (N) \r\n"); //move out of if statement so it separates logging sessions?
}
else {
Serial.println("File already exists");
}
file.close();
}
void loop() {
if (loadcell.is_ready()) {
Serial.print("Reading: ");
force = loadcell.get_units(1);
timeNow = millis(); //milliseconds since startup
Serial.print(abs(force), 1); //prints first sigfig of force
Serial.print(" N"); //change depending on divider used
Serial.println();
int Switch_state = digitalRead(Switch);
if ((force != prevForce) && (Switch_state == LOW)) {
prevForce = force;
maxForce = max(abs(force), abs(maxForce));
// display updates only value at a time to increase speed, privileges maxForce
if (maxForce == abs(force)) {
displayForce(maxForce, 36);
}
else {
displayForce(force, 0);
}
}
writeSD(readingID, timeNow, force);
readingID += 1;
}
}
void displayForce(long force, uint8_t line) {
// Maximum number of letters the screen can display in one row
uint8_t maxLength = 6;
// Long range is +-2,147,483,648
char bufferF[12] = {};
ltoa(force, bufferF, 10);
// align right by padding with spaces
char bufferLCD[maxLength + 1] = {' ', ' ', ' ', ' ', ' ', ' ', '\0'};
for (int i = 0; i < strlen(bufferF); i++) {
bufferLCD[maxLength - strlen(bufferF) + i] = bufferF[i];
}
u8g2.drawStr(0, line, bufferLCD);
u8g2.sendBuffer();
}
void writeSD(int readingID, long timeNow, long force) {
sdMessage = String(readingID) + "," + String(timeNow) + "," + String(force) + "\r\n";
appendFile(SD, "/data.txt", sdMessage.c_str());
}
// Write to the SD card (DON'T MODIFY THIS FUNCTION)
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}