Go Down

Topic: Trouble Shooting Accelerometer Issue (Read 579 times) previous topic - next topic

flowerspowers

I'm struggling to figure out what is wrong with my code. When I run my code (please see code) without if else statement, it works perfectly fine. It finds the offset for the accelerometer and calibrates the accelerometer. I get correct values and works perfectly fine. (please see image 1) (the zeroed value should be -1 or 0, which i get with out if else statement)

But as soon as I implement my if else statement, it changes my output values. And adds numbers to the output value. (please see image 2) I have no idea why. I have tried changing where my if else statement. I tried adding a constraint for a range of values. Always the same problem. The offset is now a few number too low or too high. But with out if else, constraint, range or any other conditions it works perfectly fine.

I am using a GY-61 Accelerometer and I would love some help!!!! Thank you.

Code: [Select]

const int zpin = A2;
int samplesize[100];
int zaccel;
int runningleds = 6;
int brakeleds = 7;
int zzero, zzero_a, zzero_b;

void setup() {
  Serial.begin(9600);
  pinMode(runningleds,OUTPUT);
  pinMode(brakeleds,OUTPUT);
  pinMode(zpin,INPUT);
  digitalWrite(runningleds,HIGH);
  delay(10);
  calibration();
}

void loop() {
  int zzero = analogRead(zpin) - zaccel;
  zzero_a = zzero;
  zzero_b = zzero;
 
  if(zzero_a <= 0) {
    digitalWrite(brakeleds,HIGH);
    delay(10);
  }
  else if(zzero_b > 1) {
    digitalWrite(brakeleds,LOW);
    delay(10);
  }
 
  Serial.print("Zeroed Axis = ");
  Serial.print(zzero);
  Serial.print("\t");
  Serial.print("Raw Values = ");
  Serial.print(analogRead(zpin));
  Serial.print("\n");
  delay(100);
}

//int calibration( );
 void calibration() {
  zaccel = analogRead(zpin);
  Serial.println(zaccel,DEC);
  delay(100);
 
  for(int i = 0; i > 100; i++) {
    zaccel = samplesize[i];
    Serial.println(zaccel);
  }
  delay(100);
  Serial.println("Calibration Difference");
 }



jremington

#1
Jul 08, 2020, 01:14 am Last Edit: Jul 08, 2020, 05:10 pm by jremington
Thanks for using code tags!

Sorry, the calibration function doesn't make sense to me and doesn't appear to do anything useful. The array named "samplesize" is never properly initialized and is not used for anything.

What is the calibration function supposed to do?

flowerspowers

hi, the purpose of the calibration function is to take an average of the readings from the accelerometer to find an offset. Accelerometers don't come accurately calibrated. Right out of the box the will have readings like for example: x-axis = 234, y-axis =  315 and z-axis 45. When the accelerometer should read (when flat and not moving) x-axis = 0 y-axis = 0 and z-axis = 1.

So I tried in my code to read the values from the accelerometer, average the first 10. Making that the difference of what is vs what it should be (the offset). Take what the accelerometers reading minus what the offset is to give me a calibrated accelerometer.

I am working with multiple accelerometers so i am just trying to write a code that will calibrate any accelerometer i plug it into. The Gy-61 acclerometer is the accelerometers I am using. I hope that clarifies what I am trying to accomplish.

jremington

#3
Jul 08, 2020, 08:27 pm Last Edit: Jul 08, 2020, 08:29 pm by jremington
Quote
the purpose of the calibration function is to take an average of the readings
The calibration function you posted does nothing except read and print one value. It does not do any averaging.

To do averaging, you need to read in several values, add them together and divide the result by the number of values read.

flowerspowers

I did not know that. I thought the calibration function read average and stored the first readings as zaccel. But I guess I did it wrong. It does though read and store the initial reading and is used to subtract reading to a more calibrated reading, when there is nothing else in the code such as the leds. But I am not sure how to fix the calibration so it does actually do something, nor how to have the code not change the readings of the accelerometer, when leds are introduce. Any suggestions?

jremington

Example code to average 10 values from the ADC:

Code: [Select]

   long result = 0;
   for (int i=0; i<10; i++)  result = result + analogRead(pin);
   result = result/10;  //the average

Go Up