Hello, I am trying to take this code I have that writes "LEFT" and "RIGHT" to the serial monitor every time I take a footstep, and record that onto an sd card with a timestamp (can just be millis) of when "LEFT" or "RIGHT" was written to the serial monitor. Basically I'm trying to find and record the space between each footstep onto an SD card.
I am using an Adafruit Data Logger Shield (the older model that doesn't have the 2x3 pin header) with an Arduino UNO.
Adafruit's code example only writes data to the SD card in even intervals. I need my code to only send data with a time when the "LEFT" or "RIGHT" is triggered.
Does that make sense? Sorry I am a total newbie to coding. How do I integrate writing to the sd card into the code below?
MY CODE (NOTE: I am not using the xbee functionality for this application of the code, just trying to take what the xbee would normally be sending to the other xbee and instead write it to an SD card with a time stamp):
/***********************************************************************
Going/Staying - footstep sensor processor
=========================================
DESCRIPTION:
Obtains, smoothes and analyzes analog readings from two
footstep sensors connected to LEFT_PIN and RIGHT_PIN
respectively. Compares smoothed readings to an automatically
calibrated base value for significant deviations, then sends
"trigger" commands to remote circuit via XBee serial connection.
REQUIRES:
- Arduino Fio
- XBee Series 1 module (installed on Fio and optionally
configured + paired with another XBee module)
- 2 x footstep sensors configured as voltage dividers (or not?)
INSTALLATION + USAGE:
1) Reconfigure XBee module's PAN ID to match that of other XBee
module(s) used by system
2) Upload sketch to Arduino Fio (without XBee installed)
3) Install XBee on Fio
4) Construct and attach two footstep sensors
- connect to pins indicated by LEFT_PIN and RIGHT_PIN below
5) Turn on Fio and use footstep sensors
- On-board LED will flash when trigger command is sent via XBee
CALIBRATION NOTES:
- Sketch will automatically calibrate sensors at startup by taking
many readings from sensors (defined by CALIBRATION_LENGTH) and
using the final average as a base value for future comparisons.
- Whenever left/rightAverage has deviated from their base values by
more than their respective thresholds, a "trigger" is registered
and sent via XBee.
- Increase or decrease the "threshold" varaibles below to decrease
or increase (respectively) the sensitivity of the sensors.
************************************************************************/
#define LEFT_PIN A0 // left sensor pin
#define RIGHT_PIN A1 // right sesnor pin
#define BUFFER_SIZE 10 // more readings = smoother data
int UP = 0, DOWN = 1; // variable names to make states more human-readable
int leftBuffer[BUFFER_SIZE], rightBuffer[BUFFER_SIZE]; // sensor data buffers
int leftTotal = 0, rightTotal = 0; // totals of all sensor data
int leftAverage = 0, rightAverage = 0; // averages of all sensor data
int leftState = UP, rightState = UP; // current sensor states
int leftPreviousState = UP, rightPreviousState = UP; // last known sensor states
boolean leftTriggered = false, rightTriggered = false; // flags to detect triggers
int index = 0;
// Maximum values of sensors (as found in Serial Monitor tests)
int leftMaxValue = 100;
int rightMaxValue = 720;
// Amount of change in sensor readings to trigger a step
int leftThreshold = 30;
int rightThreshold = 250;
// Strings to send over XBee when step is detected (must match receiving sketch)
String leftString = "Left";
String rightString = "Right";
void setup() {
// Establish XBee serial connection
Serial.begin(9600);
// Set up sensor input pins
pinMode(LEFT_PIN, INPUT);
pinMode(RIGHT_PIN, INPUT);
// Initialize buffers to be empty
for(int i=0; i<BUFFER_SIZE; i++) {
leftBuffer[i] = 0;
rightBuffer[i] = 0;
}
}
void loop() {
// Get most recent sensor data and calculate averages
readSensors();
// Process data and look for triggers
detectTriggers();
// If either sensor is triggered...
if(leftTriggered || rightTriggered) {
// Send notification via XBee of which sensor was triggered
if(leftTriggered) Serial.println(leftString);
if(rightTriggered) Serial.println(rightString);
// Reset trigger flags to indicate trigger has been processed
leftTriggered = false;
rightTriggered = false;
// Flash on-board LED to indicate trigger command sent
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
}
}
/**************************************************
Read sensors
- take new readings and calculate new averages
***************************************************/
void readSensors() {
// Remove last readings
leftTotal -= leftBuffer[index];
rightTotal -= rightBuffer[index];
// Obtain new sensor values
leftBuffer[index] = analogRead(LEFT_PIN);
rightBuffer[index] = analogRead(RIGHT_PIN);
// Add values to totals
leftTotal += leftBuffer[index];
rightTotal += rightBuffer[index];
// Advance to next position in array
index++;
// Wrap index when it exceeds size of buffer
if(index >= BUFFER_SIZE)
index = 0;
// Calculate averages
leftAverage = leftTotal / BUFFER_SIZE;
rightAverage = rightTotal / BUFFER_SIZE;
// Delay between reads for stability
delay(1);
}
/****************************************************************************
Detect triggers
- compare most recent sensor averages with base value + threshold to see
if changed significantly
*****************************************************************************/
void detectTriggers() {
// Left sensor -------------------------------------------------
// Find out whether foot is up or down
if(leftAverage <= leftMaxValue - leftThreshold)
leftState = DOWN;
else
leftState = UP;
// If foot was previously up and is now down, register a trigger
if(leftState == DOWN && leftPreviousState == UP)
leftTriggered = true;
// Save current sensor state
leftPreviousState = leftState;
// Right sensor -------------------------------------------------
// Find out whether foot is up or down
if(rightAverage <= rightMaxValue - rightThreshold)
rightState = DOWN;
else
rightState = UP;
// If foot was previously up and is now down, register a trigger
if(rightState == DOWN && rightPreviousState == UP)
rightTriggered = true;
// Save current sensor state
rightPreviousState = rightState;
}