-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorMonitor.ino
192 lines (155 loc) · 5.06 KB
/
SensorMonitor.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
/*
* 100k Sensor Monitor V1.1
* 2/16/2024
* Jack Shaver
*/
/*
* This code takes an input from the Sub-Master Controller and toggles between
* opening the sd card and writting sensor data, and closing the sd card and waiting
*
* Written for use with the TEENSY4.1, and the Sub-Master Controller V1 PCb
* Indicator LED for when data is being actively collected
* Inputs from array of pins, to be configured.
* 4 bit parrallel transmission from sub-master
* Auxillary Load Cell Amplifier HX711 needed on pins 31 and 32
*
* Todo:
* Change sensors on the analog pins to be the sensors needed for the static fire
* Change file write format to best log data
* Add support for digital output sensors (if needed)
*/
#include <Arduino.h>
#include "HX711.h"
//SD card preamble
#include <SPI.h>
#include <SD.h>
//const int chipSelect = BUILTIN_SDCARD;
File myFile;
HX711 loadCell;
//GPIO preamble
#define ActiveWriteLED 2
#define Error_Comm_LED 3
#define ParallelBit0 7
#define ParallelBit1 6
#define ParallelBit2 5
#define ParallelBit3 4
#define LoadCellDataPin 31
#define LoadCellClockPin 32
#define LOADCELL_CALIBRATION_FACTOR 15.94
int pin7State = 0;
int Switchstate = 0;
int toggle = 0;
int edge = 0;
long collectInterval = 100; //interval between sensor reads in milliseconds
long previousMillis = 0;
void setup() { //---------------------------------setup------------------------------------
pinMode(Error_Comm_LED, OUTPUT);
pinMode(ActiveWriteLED, OUTPUT);
pinMode(ParallelBit0, INPUT_PULLDOWN);
pinMode(ParallelBit1, INPUT);
pinMode(ParallelBit2, INPUT);
pinMode(ParallelBit3, INPUT);
Serial.begin(9600);//begin UART comm with computer, 9600 baud
//while (!Serial){}
//initialize onboard sd card, make sure its initialized
Serial.print("Initializing SD card...");
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("initialization failed!");
while (1){
digitalWrite(Error_Comm_LED, HIGH);
delay(100);
digitalWrite(Error_Comm_LED, LOW);
delay(100);
}
}
Serial.println("initialization done.");
//create and open a file on the sd card
//myFile = SD.open("DataCollection1.txt", FILE_WRITE);
//myFile.println("Measuring the Load Cell, Units = Grams");
//Load cell initialization
loadCell.begin(LoadCellDataPin, LoadCellClockPin);
loadCell.set_scale(LOADCELL_CALIBRATION_FACTOR);
loadCell.power_up();
//GPIO initialization
digitalWrite(Error_Comm_LED, HIGH);
delay(1000);
digitalWrite(Error_Comm_LED, LOW);
}
void loop() { //----------------------------------------loop(main)--------------------------
if(checkpin(ParallelBit0, &pin7State) == 1){
Switchstate = 1;
digitalWrite(Error_Comm_LED, HIGH);
delay(50);
digitalWrite(Error_Comm_LED, LOW);
}else{
Switchstate = 0;
}
//0: no change, 1: pressed, 2: released
if(Switchstate == 1){//on pressed cyle, toggle the state
toggle = !toggle;
edge = 1;
}
if(toggle == 1){ //toggle state active
if(edge == 1){ //first cycle after state change
openfile();
loadCell.tare();
delay(1000);
edge = 0;
}
//active toggle state runtime here
//collect data every specified interval
if((millis() - previousMillis) > collectInterval){
previousMillis = millis();
writefile();
}
}else{ //toggle state inactive
if(edge == 1){ //first cycle after state change
closefile();
edge = 0;
}
}
}
void openfile(void){ //------------------------------Open the file-------------------------
digitalWrite(ActiveWriteLED, HIGH);
Serial.println("File Opened");
int counter = 1;
char fileName[30];
memset(fileName, 0, sizeof(fileName));
sprintf(fileName, "DataCollection%d", counter);
while(SD.exists(fileName))
{
counter++;
memset(fileName, 0, sizeof(fileName));
sprintf(fileName, "DataCollection%d", counter);
}
myFile = SD.open(fileName, FILE_WRITE);
}
void closefile(void){ //----------------------------Close the file--------------------------
digitalWrite(ActiveWriteLED, LOW);
Serial.println("File Closed");
myFile.close();
}
void writefile(void){ //-----------------------------Write to File--------------------------
float seconds = millis()/1000.0;
myFile.print(seconds);
myFile.print(",");
myFile.println(loadCell.get_units(), 1);
}
//check desired pin, edge triggered output. Debounce and anti-transient design
int checkpin(int pinNumber, int *pinState){ //------------------------------Check Switch-------------------------
int currentValue;
currentValue = digitalRead(pinNumber); //check switch, 1: not pressed, 0: pressed
if(currentValue != *pinState){ //if they dont match, possible button state change
delay(5); //debounce delay
currentValue = digitalRead(pinNumber); //check switch again, transient buffer
if(currentValue != *pinState){ //if the dont match, actual button state change
*pinState = currentValue; //realize state change
if(*pinState == HIGH){
return 1; //low to high
}else if(*pinState == LOW){
return 2; //high to low
}
}
}
return 0; //inactive
}