Calibrating Memsic 2125 Accelerometer. I NEED HELP ASAP.

I am trying to calibrate a Memsic 2125 where it is positioned on the back of the hand.

So far, I am able to display the accelerometer values and logic values, HOWEVER, the logic value

does not change according to the set threshold values.

Help me please~ :wink:

// these constants won't change:
const int xPin = 12;     // X output of the accelerometer
const int yPin = 11;     // Y output of the accelerometer

int highThresholdAccX = 18;
int smallThresholdAccX = -16;
int highThresholdAccY = -12;
int smallThresholdAccY = 24;
int accXLogic = 0;
int accYLogic = 0;
int AccX = 0;
int AccY = 0;

void setup() {
  // initialize serial communications:
  Serial.begin(115200);
  // initialize the pins connected to the accelerometer
  // as inputs:
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
}

void loop() {
  // variables to read the pulse widths:
  int pulseX, pulseY;
  // variables to contain the resulting accelerations
  int accelerationX, accelerationY;
  
  accelerationX = digitalRead(accXLogic);
  accelerationY = digitalRead(accYLogic);
 
  // read pulse from x- and y-axes:
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);
  
  if (accelerationX < smallThresholdAccX){
    accXLogic = 0;
  }
  
  if (accelerationX > smallThresholdAccX){
    accXLogic = 1;
  }
  
  if (accelerationX > highThresholdAccX){
    accXLogic = 1;
  }
  
  if (accelerationX < highThresholdAccX){
    accYLogic = 0;
  }

 
  // convert the pulse width into acceleration
  // accelerationX and accelerationY are in milli-g's:
  // earth's gravity is 1000 milli-g's, or 1g.
  accelerationX = ((pulseX / 10) - 500) * 8;
  accelerationY = ((pulseY / 10) - 500) * 8;

  // print the acceleration
  Serial.print(accelerationX);
  // print a tab character:
  Serial.print("\t");
  Serial.print(accelerationY);
   Serial.print(",");
  Serial.print(accXLogic, DEC);
  Serial.print(",");
  Serial.print (accYLogic, DEC); 
  Serial.print("\t");
  Serial.println();

  delay(1000);
}

What’s this :

accelerationX = digitalRead(accXLogic);
accelerationY = digitalRead(accYLogic);

supposed to be doing ? Below those statements you calculate the accels from the pulsewidths. The above would seem to try to toss those calculations away. I believe it’s successful and so there’s no accelerations to use in your threshold logic. You see a good printout because you subsequently calculate the accels from the PWs and print them out (see below).

Just as an aside … it appears to me that you read in the pulsewidths, perform some thresholding on the prior loops accels from the prior loops PWs and then calculate the new accels from the just-read-in PWs. This will work (after correcting the digitalReads) but why not make everything use the current PWs ? That is read in the PWs, calculate the accels, do the thresholding and then print them out (if needed) … in that order. Wash, rinse repeat.

Also what were you trying to do with the following :

if (accelerationX < smallThresholdAccX){

accXLogic = 0;

}

if (accelerationX > smallThresholdAccX){

accXLogic = 1;

}

if (accelerationX > highThresholdAccX){

accXLogic = 1;

}

if (accelerationX < highThresholdAccX){

accYLogic = 0;

}

I note there’s no threshold calculations using the accelerationY. Were there some typos (note the bolding I added) in the above ? I’m going to guess and say you were trying to window the accels. That is set the “logic” to 0 whenever the accel is below the low threshold and set it to a 1 whenever the accel is above the high threshold. Leave the logic state as is whenever the accel is in the window (between the thresholds). The above does not do that (so you have 2 problems, this and that in the prior post). Noting the polarity difference between the X and Y and assuming it’s there for a good reason …

if (accelerationX < smallThresholdAccX){

accXLogic = 0;

}

if (accelerationX > highThresholdAccX){

accXLogic = 1;

}

if (accelerationY < highThresholdAccY){

accYLogic = 1;

}

if (accelerationY > smallThresholdAccY){

accYLogic = 0;

}

… is what I think you want (note underlined changes). But check me very very carefully on the threshold logic for the Y (due to my guess as to why you have opposite polarities for the small and high thresholds as compared to the X thresholds). Maybe the sign change was a typo as well. If so then the > and < I changed above are wrong.

ps - why not ‘low and high’ or ‘small and large’ … anything but ‘small and high’. :mrgreen:

pps - I didn’t use the code tags because apparently underlining and bolding does not work within a “code box”. Baaaah !