// Temporary log file. Will be deleted if a reset or power failure occurs. | // Temporary log file. Will be deleted if a reset or power failure occurs. | ||||
#define TMP_FILE_NAME "tmp_log.bin" | #define TMP_FILE_NAME "tmp_log.bin" | ||||
// Size of file base name. Must not be larger than six. | |||||
// Size of file base name. | |||||
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | ||||
const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | ||||
char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | ||||
if (sizeof(block_t) != 512) { | if (sizeof(block_t) != 512) { | ||||
error("Invalid block size"); | error("Invalid block size"); | ||||
} | } | ||||
// Allow userSetup access to SPI bus. | |||||
pinMode(SD_CS_PIN, OUTPUT); | |||||
digitalWrite(SD_CS_PIN, HIGH); | |||||
// Setup sensors. | |||||
userSetup(); | |||||
// Initialize at the highest speed supported by the board that is | // Initialize at the highest speed supported by the board that is | ||||
// not over 50 MHz. Try a lower speed if SPI errors occur. | // not over 50 MHz. Try a lower speed if SPI errors occur. | ||||
if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | ||||
sd.initErrorPrint(&Serial); | sd.initErrorPrint(&Serial); | ||||
fatalBlink(); | fatalBlink(); | ||||
} | } | ||||
userSetup(); | |||||
} | } | ||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
void loop(void) { | void loop(void) { |
// User data functions. Modify these functions for your data items. | // User data functions. Modify these functions for your data items. | ||||
// Start time for data | // Start time for data | ||||
uint32_t startTime; | |||||
static uint32_t startMicros; | |||||
// Acquire a data record. | // Acquire a data record. | ||||
void acquireData(data_t* data) { | void acquireData(data_t* data) { | ||||
// Print a data record. | // Print a data record. | ||||
void printData(Print* pr, data_t* data) { | void printData(Print* pr, data_t* data) { | ||||
if (startTime == 0) { | |||||
startTime = data->time; | |||||
if (startMicros == 0) { | |||||
startMicros = data->time; | |||||
} | } | ||||
pr->print(data->time - startTime); | |||||
pr->print(data->time - startMicros); | |||||
for (int i = 0; i < ADC_DIM; i++) { | for (int i = 0; i < ADC_DIM; i++) { | ||||
pr->write(','); | pr->write(','); | ||||
pr->print(data->adc[i]); | pr->print(data->adc[i]); | ||||
// Print data header. | // Print data header. | ||||
void printHeader(Print* pr) { | void printHeader(Print* pr) { | ||||
startTime = 0; | |||||
startMicros = 0; | |||||
pr->print(F("micros")); | pr->print(F("micros")); | ||||
for (int i = 0; i < ADC_DIM; i++) { | for (int i = 0; i < ADC_DIM; i++) { | ||||
pr->print(F(",adc")); | pr->print(F(",adc")); |
#define FILE_BASE_NAME "adc4pin" | #define FILE_BASE_NAME "adc4pin" | ||||
const uint8_t ADC_DIM = 4; | const uint8_t ADC_DIM = 4; | ||||
struct data_t { | struct data_t { | ||||
unsigned long time; | |||||
unsigned short adc[ADC_DIM]; | |||||
uint32_t time; | |||||
uint16_t adc[ADC_DIM]; | |||||
}; | }; | ||||
void acquireData(data_t* data); | void acquireData(data_t* data); | ||||
void printData(Print* pr, data_t* data); | void printData(Print* pr, data_t* data); |
// Temporary log file. Will be deleted if a reset or power failure occurs. | // Temporary log file. Will be deleted if a reset or power failure occurs. | ||||
#define TMP_FILE_NAME "tmp_log.bin" | #define TMP_FILE_NAME "tmp_log.bin" | ||||
// Size of file base name. Must not be larger than six. | |||||
// Size of file base name. | |||||
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | ||||
const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | ||||
char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | ||||
if (sizeof(block_t) != 512) { | if (sizeof(block_t) != 512) { | ||||
error("Invalid block size"); | error("Invalid block size"); | ||||
} | } | ||||
// Allow userSetup access to SPI bus. | |||||
pinMode(SD_CS_PIN, OUTPUT); | |||||
digitalWrite(SD_CS_PIN, HIGH); | |||||
// Setup sensors. | |||||
userSetup(); | |||||
// Initialize at the highest speed supported by the board that is | // Initialize at the highest speed supported by the board that is | ||||
// not over 50 MHz. Try a lower speed if SPI errors occur. | // not over 50 MHz. Try a lower speed if SPI errors occur. | ||||
if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | ||||
sd.initErrorPrint(&Serial); | sd.initErrorPrint(&Serial); | ||||
fatalBlink(); | fatalBlink(); | ||||
} | } | ||||
userSetup(); | |||||
} | } | ||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
void loop(void) { | void loop(void) { |
const uint8_t DATAZ1 = 0x37; //Z-Axis Data 1 | const uint8_t DATAZ1 = 0x37; //Z-Axis Data 1 | ||||
void writeADXL345Register(const uint8_t registerAddress, const uint8_t value) { | void writeADXL345Register(const uint8_t registerAddress, const uint8_t value) { | ||||
// Max SPI clock frequency is 5 MHz with CPOL = 1 and CPHA = 1. | |||||
SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE3)); | |||||
digitalWrite(ADXL345_CS, LOW); | digitalWrite(ADXL345_CS, LOW); | ||||
SPI.transfer(registerAddress); | SPI.transfer(registerAddress); | ||||
SPI.transfer(value); | SPI.transfer(value); | ||||
digitalWrite(ADXL345_CS, HIGH); | digitalWrite(ADXL345_CS, HIGH); | ||||
SPI.endTransaction(); | |||||
} | } | ||||
void userSetup() { | void userSetup() { |
// User data types. Modify for your data items. | // User data types. Modify for your data items. | ||||
const uint8_t ACCEL_DIM = 3; | const uint8_t ACCEL_DIM = 3; | ||||
struct data_t { | struct data_t { | ||||
unsigned long time; | |||||
uint32_t time; | |||||
int16_t accel[ACCEL_DIM]; | int16_t accel[ACCEL_DIM]; | ||||
}; | }; | ||||
void acquireData(data_t* data); | void acquireData(data_t* data); |
#ifdef ERROR_LED_PIN | #ifdef ERROR_LED_PIN | ||||
#undef ERROR_LED_PIN | #undef ERROR_LED_PIN | ||||
#endif // ERROR_LED_PIN | #endif // ERROR_LED_PIN | ||||
const int8_t ERROR_LED_PIN = 3; | |||||
const int8_t ERROR_LED_PIN = -1; | |||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
// File definitions. | // File definitions. | ||||
// | // | ||||
// Temporary log file. Will be deleted if a reset or power failure occurs. | // Temporary log file. Will be deleted if a reset or power failure occurs. | ||||
#define TMP_FILE_NAME "tmp_log.bin" | #define TMP_FILE_NAME "tmp_log.bin" | ||||
// Size of file base name. Must not be larger than six. | |||||
// Size of file base name. | |||||
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1; | ||||
const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | const uint8_t FILE_NAME_DIM = BASE_NAME_SIZE + 7; | ||||
char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | char binName[FILE_NAME_DIM] = FILE_BASE_NAME "00.bin"; | ||||
if (sizeof(block_t) != 512) { | if (sizeof(block_t) != 512) { | ||||
error("Invalid block size"); | error("Invalid block size"); | ||||
} | } | ||||
// Allow userSetup access to SPI bus. | |||||
pinMode(SD_CS_PIN, OUTPUT); | |||||
digitalWrite(SD_CS_PIN, HIGH); | |||||
// Setup sensors. | |||||
userSetup(); | |||||
// Initialize at the highest speed supported by the board that is | // Initialize at the highest speed supported by the board that is | ||||
// not over 50 MHz. Try a lower speed if SPI errors occur. | // not over 50 MHz. Try a lower speed if SPI errors occur. | ||||
if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) { | ||||
sd.initErrorPrint(&Serial); | sd.initErrorPrint(&Serial); | ||||
fatalBlink(); | fatalBlink(); | ||||
} | } | ||||
userSetup(); | |||||
} | } | ||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
void loop(void) { | void loop(void) { |