Good luck on your project.
- Code: Select all
// Pothole Detector V2.0 by Arctic_Eddie, 2/5/2013
// This sketch detects and processes data collected from an accelerometer on a continuous basis.
// Threshold detectors can be added which operate on the three arrays to count particular events.
// Define constants
#define SAMPLE_DELAY 200.0 // Sample delay in milliseconds, probably 20
#define GRAVITYFT_SEC 32.174 // Gravity to velocity,feet/sec
#define G_OFFSET 1.0 // Make the G reading zero for the resting state, usually 1.0 for actual use
#define G_SCALE 6.0 // If accelerometer is on +/- 3G scale
#define INCH_FOOT 12.0 // 12 inches in foot, from velocity to inch displacement calculation
#define NUM_SAMPLES 20 // Capture 6 seconds, probably 300
#define ACCEL_PIN A0 // Use analog pin A0
// Define global veriables
uint8_t firstTime = true; // First time through loop so initial calc not same as first rollover
uint16_t indeX = 0; // Pointer into data arrays
uint32_t tindeX = 0; // Total index counter and not reset
uint32_t dTime; // Clock time for startup reading
float acceleration[NUM_SAMPLES]; // Space for acceleration samples
float velocity[NUM_SAMPLES]; // Space for velocity samples
float displacement[NUM_SAMPLES]; // Space for displacement samples
// Set up system
void setup()
{
Serial.begin( 115200 ); // Prepare the serial monitor to receive the data
Serial.println( "\n\rTime, Acceleration, Velocity, Displacement" ); // Print heading
randomSeed( analogRead( A1 ) ); // Seed the random number generator with noise
// Initialize the accelerometer ****************************
// Start the delay timer
dTime = millis();
}
void loop()
{
// Collect continuous data, calculate other values, and print results
// This section is for continuous running to fill the arrays
// Get a random value or use accelerometer reading here
///acceleration[indeX] = ( ( analogRead( ACCEL_PIN ) / 1024.0 ) * G_SCALE ) - G_OFFSET; // Get an acceleration reading
acceleration[indeX] = float( random( 1025 ) - 512 ) / 1024.0; // Make it +/- 0.5G
// Calculate the running sum integral for velocity and displacement
if( indeX == 0 ) // First reading, no previous data to add to running sum
{
if( firstTime == true ) // Load the first position with initial data
{
velocity[indeX] = acceleration[indeX] * SAMPLE_DELAY * GRAVITYFT_SEC / 1000.0;
displacement[indeX] = velocity[indeX] * SAMPLE_DELAY * INCH_FOOT / 1000.0;
firstTime = false; // Prevent first time from happening again
}
else // Use last position, NUM_SAMPLES-1, just before rollover for accumulated values
{
velocity[indeX] = ( acceleration[indeX] * SAMPLE_DELAY * GRAVITYFT_SEC / 1000.0 )
+ velocity[NUM_SAMPLES-1]; // The previous running sum
displacement[indeX] = ( velocity[indeX] * SAMPLE_DELAY * INCH_FOOT / 1000.0 )
+ displacement[NUM_SAMPLES-1]; // The previous running sum
}
}
else // Not the first reading so add the data from the previous index
{
velocity[indeX] = ( acceleration[indeX] * SAMPLE_DELAY * GRAVITYFT_SEC / 1000.0 )
+ velocity[indeX-1]; // The running sum
displacement[indeX] = ( velocity[indeX] * SAMPLE_DELAY * INCH_FOOT / 1000.0 )
+ displacement[indeX-1]; // The running sum
}
// Insert threshold detector, running average, and reset action here *******************
// Print the results in spreadsheet readable format
Serial.print( float( tindeX * SAMPLE_DELAY ) / 1000.0 ); // The actual time value
Serial.print( ", " ); // Add a comma separator
Serial.print( acceleration[indeX] ); // Acceleration
Serial.print( ", " ); // Add a comma separator
Serial.print( velocity[indeX] ); // Velocity
Serial.print( ", " ); // Add a comma separator
Serial.println( displacement[indeX] ); // Displacement
// Pause until the next interval, check clock for correct delay
while( ( millis() - dTime ) < ( SAMPLE_DELAY * tindeX ) ) {} // Pause here until sample interval has passed
// Index the pointers
tindeX++; // Increment the total index pointer
indeX = ++indeX % NUM_SAMPLES; // Increment the array indexer but cause rollover, circular buffer
}
// Supporting routines go here *********************************************
float fmap( 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;
}


