Sampling Acceleration with external ADC

Hey All,

I’m a student who just started learning Arduino, so I am still a beginner. I am working on a project where I have to sample impact accelerations. I used this blog to help guide my project:

I am particularly interested in the graph at the end of page 1 of the blog. This is my ultimate goal, to obtain a graph similar to that. I need to sample acceleration at a rate of above 10 kHz, and the graph is representative of a sampling rate of about 40 kHz, so I think it is possible. Attached is the code he used to get this graph. He used an Atmel ATmega328 with an Arduino Duemilanove in his setup, where I have an Arduino Mega 2560. We both use the same acclerometer and same external ADC. The way the code is supposed to work is it stores the data it reads from the accelerometer to an internal storage buffer.

My question is, how do I make this code work for my Mega2560? I do not know where in the code I am going wrong, although I do not completely understand the code. Right now when I run the code, I get numbers on the serial monitor that are not useful.

Thank you for any time and help, and please let me know if there is anything else I can describe or show to you to help.

Thanks!

BiennemanMod.ino (1.85 KB)

Please post your code, in code tags.

When posting a small amount of code, just copy and paste it between code tags as explained in How to post code properly. Then we don't have to download it and open it in a text editor.

   int i = storeIndex;
    while (i != storeIndex-1)

Assume that storeIndex is zero. You will set i to zero and then the while loop will test for while i is not equal to -1. Bit of a problem because i won't be equal to -1 until you've gone round the loop 65535 times.

Pete

Sorry for the formatting issues. Here is the code posted, not including the SPI pin setup. Hope it is easier to understand:

// storage buffer
const int storeLen = 800;
unsigned int store[storeLen];
int storeIndex = 0;
boolean isRecording = false;
int recordEndIndex;



void setup()
{
  // initialize serial communications with pc
  Serial.begin(9600); //9600
  Serial.println("AccelerationSensor 0.1");
 
  // initialize SPI interface
  digitalWrite(CS_PIN,HIGH);
  pinMode(CS_PIN,OUTPUT);
  pinMode(SCK_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  // configure SPI to: enable, master, clock high when idle, sample on trailing edge, freq divider 4
  SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<CPHA); // | (1<<SPR0) | (1<<SPR1);  
  SPSR |=  (1<<SPI2X); // double the clock speed
  
}


unsigned int reading;
long started = 0;
long n = 1;

void loop()
{
  // read from the sensor:  

  n++;

  reading = analogRead(0);
  reading = reading<<6;
  store[storeIndex] = reading;
  storeIndex++;
  if (storeIndex >= storeLen) storeIndex = 0;
  

  if (!isRecording && (reading>33600 || reading<31800))
  {
    isRecording = true;
    recordEndIndex = storeIndex - 100;
    if (recordEndIndex < 0) recordEndIndex += storeLen;
  }
  
 if (isRecording && storeIndex == recordEndIndex)
  {
    isRecording = false;
    Serial.println("start of record");
    int i = storeIndex;
    while (i != storeIndex-1)
    {
//      Serial.print(i,DEC);
//      Serial.print(" ");
      Serial.println(store[i],DEC);
      i++;
      if  (i >= storeLen) i = 0;
    }
    Serial.println("end of record");
    started = millis();
  }
  

  if (millis()-started>1000)
  {
  Serial.print("n: ");
  Serial.println(n);
  
    n = 0;
    
    started = millis();
    
  }
 
}