Help me check my program!

HI,

Basically, I want mount a accelerometer via magnet onto a motor, to monitor its vibration. I was hoping to get 600 readings from the accelerometer in each axis, so that I can do the FFT and analyse from there.

I did a dry run without mounting onto the motor, and I was able to get 600 readings without any problem. So I moved on to the real deal. I first set the RPM of the motor, waited for it to stabilize before mounting the sensor, however, I found myself getting readings of less than 300. And as the speed increases, the number of readings decreases as well!

Wondering if there is any problem in the program or the hardware. Maybe due to the magnet mounting? Or some technical knowledge that I am missing out.

I am using the ADXL345 for vibration trending.Accelerometer ADXL345
I am using the Arduino Uno R3 as a microcontroller. Arduino Uno Board R3
Logging data on the sd storage on the Arduino Wifi Shield.

#include <Wire.h>
#define DEVICE (0x53)    //ADXL345 device address
#define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)
#define THRESH_ACT  (0x24)
#define THRESH_INACT  (0x25)
#include <SPI.h>
#include <SD.h>
File myFile;

// Declare Variable
    byte runComplete; //setting the number of iteration
    int i; //delare i for count
    byte buff[TO_READ];//6 bytes buffer for saving data read from the device
    float voltage; //Analog input voltage from AD8495 thermocouple
    float temp; //Temperature variable thermocoupple
    float tem[10]; //Temperature variable thermocoupple
    float x, y, z; //Acceleration variable
    float x1, y1, z1; //Acceleration variable
       
// define Analog pin   
    float tpin = A0; //thermocouple to A0

    void thermocouple() {
          float voltage = analogRead(tpin); //Read voltage coming from AD8495
          float vout = (voltage * 5)/ 1023.0; //Calculate vout
          float temp = (vout - 1.25) / 0.005 ; //Convert to temperature
          Serial.print("Thermocouple : "); Serial.print(temp); Serial.print((char)186); Serial.print(" C");Serial.println();
           myFile = SD.open("temperature.txt", FILE_WRITE); // if the file opened okay, write to it:
            if (myFile) {
            Serial.print("Writing to Temperature.txt...");
            myFile.println(temp);
            myFile.close();// close the file:
            Serial.println("done.");
          } else {
            Serial.println(F("error opening Tempearture.txt")) ;// if the file didn't open, print an error:
          }
        }
        
    void writeTo(int device, byte address, byte val) {
      Wire.beginTransmission(device); //start transmission to device
      Wire.write(address);        // send register address
      Wire.write(val);        // send value to write
      Wire.endTransmission(); //end transmission
    }

    void readFrom(int device, byte address, int num, byte buff[]) { //reads num bytes starting from address register on device in to buff array
      Wire.beginTransmission(device); //start transmission to device
      Wire.write(address);        //sends address to read from
      Wire.endTransmission(); //end transmission
      int n = Wire.requestFrom(device, num);    // request 6 bytes from device
      if( n == num)
  {
    Wire.readBytes(buff, n);
  }
    }
    
    void regAddress()
    {
        int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
        readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
        
         //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
         //thus we are converting both bytes in to one int
        x = (((int)buff[1]) << 8) | buff[0];   
        y = (((int)buff[3])<< 8) | buff[2];
        z = (((int)buff[5]) << 8) | buff[4];
        
        x1 = (x/256)*9807;
        y1 = (y/256)*9807;
        z1 = (z/256)*9807;

        Serial.println(" ");
        Serial.print("X: ");Serial.print(x1); Serial.println(" mm/s2");
        Serial.print("Y: ");Serial.print(y1); Serial.println(" mm/s2");
        Serial.print("Z: ");Serial.print(z1); Serial.println(" mm/s2");
        Serial.println(" ");
       
        myFile = SD.open("x2.txt", FILE_WRITE); // if the file opened okay, write to it:
        if (myFile) {
          Serial.print("Writing to X.txt...");
          myFile.println(x1);
          myFile.close();// close the file:
          Serial.println("done.");
        } else {
          Serial.println(F("error opening x.txt")) ;// if the file didn't open, print an error:
        }
        
        myFile = SD.open("y2.txt", FILE_WRITE); // if the file opened okay, write to it:
        if (myFile) {
          Serial.print("Writing to Y.txt...");
          myFile.println(y1);
          myFile.close();// close the file:
          Serial.println("done.");
        } else {
          Serial.println(F("error opening Y.txt")) ; // if the file didn't open, print an error:
        }
          myFile = SD.open("z2.txt", FILE_WRITE);// if the file opened okay, write to it:
        if (myFile) {
          Serial.print("Writing to Z.txt...");
          myFile.println(z1);
          myFile.close();// close the file:
          Serial.println("done.");
          Serial.println("");
        } else {
          Serial.println(F("error opening Z.txt")); // if the file didn't open, print an error:
          Serial.println("");
        }  
           }
     
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  writeTo(DEVICE, 0x2D, 0);   //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 16);  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 8);   //Turning on the ADXL345
  writeTo(DEVICE, 0x31, 8);
  Serial.println("Initializing Accelerometer...");
  Serial.print("Initializing SD card...");
  Serial.println("");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    Serial.println("");
    return;
  }
  delay(2000); 
}

void loop() {
if (runComplete == 0){
  for (i=0; i<600; i=i+1){
  Serial.print("Number of iteration(seconds) = "); Serial.print(i);
  regAddress();
  thermocouple();
     delay(100);
   }
  runComplete = 1;
  }
}
char str[512];          //string buffer to transform data before sending it to the serial port
char str1[512];
char str2[512];

Plus SD buffers.

What are you running this on?

Serial.println("error opening test.txt");

should be Serial.println(F("error opening test.txt")); and so on.

AWOL:

char str[512];          //string buffer to transform data before sending it to the serial port

char str1[512];
char str2[512];



Plus SD buffers.

What are you running this on?

sorry, they were meant for another code used for acceleration, forget to remove them.
What are SD buffers? 



Serial.println("error opening test.txt");



should be `Serial.println(F("error opening test.txt"));` and so on.

what does the "F" in the Serial.println(F("error opening test.txt")); means or represent ?

motomage:
what does the "F" in the Serial.println(F("error opening test.txt")); means or represent ?

I'm not sure, but it may be the 'F' in RTFM

It means "Please store this string in Flash memory and save the precious RAM for actual variables."

It is very relevant on an Uno or other 8-bit Arduino. If you're currently able to compile it with those large string buffers, then that means you're probably using a bigger Arduino like a Due or Teensy. On those, the F macro is not used as often, but it still does save memory.

MorganS:
It means "Please store this string in Flash memory and save the precious RAM for actual variables."

It is very relevant on an Uno or other 8-bit Arduino. If you're currently able to compile it with those large string buffers, then that means you're probably using a bigger Arduino like a Due or Teensy. On those, the F macro is not used as often, but it still does save memory.

oh ok I see the rational there. Ok so I have removed those large string buffers already. Anywhere that I should improved on?

good day..

is taking 600 iteration too much does it cause it to hang or stop working? what be an ideal iteration for FFT?