I have written a code for Accelerometer. So now I want the code to perform this--If I enter a “C f1 f2”, f1 and f2 being floating point numbers, the Arduino will calibrate the output. The first value will be the calibration_zero and the second will be the calibration_factor. I am trying hard to write but cannot. Can someone give me a hint to write this.
const int xPin = 2; // x-axis pin
const int yPin = 1; // y-axis pin
const int zPin = 0; // z-axis pin
const float supplyVoltage = 3.0; // voltage connected Aref
int xReading = 0; // analog reading from x-axis pin
int yReading = 0; // analog reading from y-axis pin
int zReading = 0; // analog reading from z-axis pin
float xVoltage, yVoltage, zVoltage;
float cal_factor = 0.3; // V/g
float cal_zero_offset = 1.55; // Volts
float xG, yG, zG;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
analogReference(EXTERNAL);
}
void loop() {
// put your main code here, to run repeatedly:
int incoming;
if (Serial.available()>0)
{
incoming = Serial.read();
switch (incoming)
{
case 71: // "G"
readonce();
break;
default:
Serial.println("Command not recognized.");
}
}
}
void readonce()
{
// Get input values
xReading = analogRead(xPin);
yReading = analogRead(yPin);
zReading = analogRead(zPin);
xVoltage = supplyVoltagexReading/1024.;
yVoltage = supplyVoltageyReading/1024.;
zVoltage = supplyVoltage*zReading/1024.;
xG = (xVoltage - cal_zero_offset)/cal_factor;
yG = (yVoltage - cal_zero_offset)/cal_factor;
zG = (zVoltage - cal_zero_offset)/cal_factor;
// Output Reading
Serial.print("x: ");
Serial.print(xG);
Serial.print("\ty: ");
Serial.print(yG);
Serial.print("\tz: ");
Serial.println(zG);
}