/*
* Project HawkSpider_CadenG
* Description: A proof-of-concept integration that measures vibrations within the Hawk Spider energy storage device and
* also controls a relay to disengage components during a vibrations event.
* Author: Caden Gamache
* Date: 05/18/2023 -
*/
SYSTEM_MODE(SEMI_AUTOMATIC);
/********************** Libraries **********************/
#include <SPI.h>
#include <SdFat.h>
/********************** Variables **********************/
// Pinmodes
const int STARTPIN = D7;
const int PIEZOPIN = A0;
const int ACCELX = A2;
const int ACCELY = A3;
const int ACCELZ = A4;
// General
int i;
bool onOff;
// Timers
float logTime;
unsigned int startTime;
// ButtonState
bool logStart;
// SD Card
String dateTime, noYear;
const char FILE_BASE_NAME[] = "Data";
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
const int chipSelect = SS;
float data[4096];
float accelX[4096];
float accelY[4096];
float accelZ[4096];
float timeStamp[4096];
char fileName[13];
int fileNumber = 0;
// Piezo
float piezoVibration;
// Accelerometer
int rawX, rawY, rawZ;
float scaledX, scaledY, scaledZ; // Scaled values for each axis
int scale = 3; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
/*********************** Objects ***********************/
SdFat sd;
SdFile file;
/********************* Prototypes **********************/
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max);
void setup() {
Serial.begin(9600);
waitFor(Serial.isConnected, 10000);
pinMode(STARTPIN,INPUT_PULLDOWN);
pinMode(PIEZOPIN,INPUT);
Particle.syncTime();
startTime = micros();
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.cc
if (!sd.begin(chipSelect, SD_SCK_MHZ(10))) {
Serial.printf("Error starting SD Module");
}
if (BASE_NAME_SIZE > 6) {
Serial.println("FILE_BASE_NAME too long");
while(1); // Stop program
}
sprintf(fileName,"%s%02i.csv",FILE_BASE_NAME,fileNumber);
}
void loop() {
/************************** Button Initialize ***************************/
pinMode(STARTPIN,INPUT_PULLDOWN);
logStart = digitalRead(STARTPIN);
Serial.printf("Press button to log data\n");
while(logStart == FALSE) {
logStart = digitalRead(STARTPIN); // Traps here until button is pressed
Serial.printf("");
}
/******************************* SD Setup *******************************/
Serial.printf("Data Logging Started\n");
while (sd.exists(fileName)) { // Cycle through files until number not found
fileNumber++;
sprintf(fileName,"%s%02i.csv",FILE_BASE_NAME,fileNumber); // Create numbered filename
Serial.printf("Filename is %s\n",fileName);
}
if (!file.open(fileName, O_WRONLY | O_CREAT | O_EXCL)) { // Open file for printing
Serial.println("File Failed to Open");
}
file.printf("Time, Signal, Frequency, FFT Magnitude, Complex FFT, Accelerometer X, Accelerometer Y, Accelerometer Z, Scaled X, Scaled Y, Scaled Z\n"); // Print header row
Serial.printf("\nLogging to: %s\n",fileName);
startTime = micros();
/***************************** Piezo Sensor *****************************/
pinMode(STARTPIN, OUTPUT);
for (i=0;i<4096;i++) {
onOff = !onOff;
digitalWrite(STARTPIN, onOff);
logTime = (micros() - startTime) / 1000000.0;
piezoVibration = analogRead(PIEZOPIN)/1280.0;
data[i] = piezoVibration;
timeStamp[i] = logTime;
accelX[i] = analogRead(ACCELX);
accelY[i] = analogRead(ACCELY);
accelZ[i] = analogRead(ACCELZ);
delayMicroseconds(2500);
}
/***************************** Accelerometer *****************************/
// Scales based on power connected to 5V (Swap 675 values for 1023 if connected to 3.3V)
scaledX = mapf(accelX[1], 0, 675, -scale, scale);
scaledY = mapf(accelY[1], 0, 675, -scale, scale);
scaledZ = mapf(accelZ[1], 0, 675, -scale, scale);
// Scaled accelerometer X,Y,Z readings
Serial.printf("X = %f g\nY = %f g\nZ = %f g\n",scaledX, scaledY, scaledZ);
/************************ Time and Write to Drive ************************/
dateTime = Time.timeStr(); // Collects real world time
noYear = dateTime.substring(0,19);
Serial.printf("Time of full data collect %s\n",noYear.c_str());
for (i=0;i<4096;i++) {
Serial.printf("Time from start: %f Piezo value = %f\n", timeStamp[i], data[i]);
file.printf("%f,%f, , , ,%f,%f,%f\n", timeStamp[i], data[i], accelX[i], accelY[i], accelZ[i]);
delayMicroseconds(500);
}
file.close();
Serial.printf("Done\n");
digitalWrite(STARTPIN, LOW);
delay(500); // Small delay before looping
}
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Comments
Please log in or sign up to comment.