Calibrate accelero realtime with remote

Hello,
To calibrate my accelerometer realtime. I would like to use a remote.
Calibration procedure is not a problem at all.

When I push a certaing button on my remote, I want the accelero to calibrate. The problem is, in my piece of code, I'm using a while code. Which implicits the accelero keeps on calibrating since it is receiving the same Infrared Code:

#include <IRremote.h>


IRrecv irrecv(IRpin);
decode_results results;
int IRpin = 11;  // pin for the IR sensor
byte _xpin= A0;
byte _ypin = A1;
byte _zpin = A2;

int _gc = 92;//gravity constant (inverted for proper sinage)
int x = 660; //defaults to read values
int y = 677; //defaults to reasonable values
int z = 660; //defaults to read values
void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() 
{
    if (irrecv.decode(&results)) 
    {
      irrecv.resume();   // Receive the next value
    }
    
    
[b]   while ((results.value == 165214229)) {   //number of the IR signal 
    calibrate ();
      }[/b]
}


void calibrate()
{      
x = readAverage(_xpin, 10);
Serial.print("X Axis Calibrated:"); Serial.println(_0x);
y = readAverage(_ypin, 10);
Serial.print("Y Axis Calibrated:"); Serial.println(_0y);
z = readAverage(_zpin, 10);
Serial.print("Z Axis Calibrated:"); Serial.println(_0z);

_calibrated = true;
}
int readAverage(int pin, int samples)
{
long readings = 0;

for(int i = 0; i < samples; i++)
{
readings += analogRead(pin);
}

return (readings/samples);
}

float convert(float myReading, float my0G)
{
return convert(myReading, my0G, GFORCE);//defaults to G-Forces
}

float convert(float myReading, float my0G, bool myUnits)
{
float myResult=((my0G-myReading)/(_gc));
if(myUnits)
return myResult; //G-Forces
else
return (myResult * 9.80665 * 10);
}

This is the "While code":
while ((results.value == 165214229)) { //number of the IR signal
** calibrate ();}**
Is someone seeing a solution? I know I can push another button and the calibration will stop. But I want with one push to calibrate it.

Thank you, newbie to arduino!

Some suggestions:

1: reset the results.value, once you have detected the button or use another variable as a flag.

  1. to use a second button for stopping, that button will have a different value. So just add some code to cater for this.

It is probably a good idea to work your way thru all of the examples provided with the library and also as many of the standard Arduino examples providedas possible with the IDE, so you can become more familiar with different approaches to coding arduinos etc.

Is someone seeing a solution?

Sure. Use an if statement, not a while statement. After all, calibration should only happen when a new value arrives.

PaulS:
Sure. Use an if statement, not a while statement. After all, calibration should only happen when a new value arrives.

The idea of the project is that I can calibrate the accelero a lot. For example 5 times in 1 minute by pushing on a button. The accelero should calibrate.

castel:
The idea of the project is that I can calibrate the accelero a lot. For example 5 times in 1 minute by pushing on a button. The accelero should calibrate.

Using an if() instead of a while() should allow you to calibrate a lot. Technically it should allow you to calibrate as often as the main loops frequency.

AnalysIR:
Some suggestions:

1: reset the results.value, once you have detected the button or use another variable as a flag.

  1. to use a second button for stopping, that button will have a different value. So just add some code to cater for this.

It is probably a good idea to work your way thru all of the examples provided with the library and also as many of the standard Arduino examples providedas possible with the IDE, so you can become more familiar with different approaches to coding arduinos etc.

Hello. Thank you for the great advice. Suggestion 1: Reset the result.value. How is that going? I am not able to get this work.
Even with a if loop, it keeps on calibrating the whole time since the receiver is reading the last value from the remote

Even with a if loop, it keeps on calibrating the whole time since the receiver is reading the last value from the remote

Well, that shouldn't be happening, unless you keep holding the remote button down. Even then, most remotes send a different value that means that the same button is being held down.

results.value = 0; // Don't do that again...

Or, move the if statement inside the block where you got the new value.

PaulS:
Well, that shouldn't be happening, unless you keep holding the remote button down. Even then, most remotes send a different value that means that the same button is being held down.

results.value = 0; // Don't do that again...

Or, move the if statement inside the block where you got the new value.

No the last value from the remote was remembered. That's why it kept on calibrating.
But when I added results.value = 0; // Don't do that again... it worked out fine!!! Thank you

Another question with Variables:

I have in my sketch a value set as

int maximumRange;

Now I want to controll the value of maximumRange with the IR Remote.

With the "if or while loop" this is not working cause I'm getting the error that an int variable is not allowed in a loop.

I tried it this way:

while (results.value == 168411){ //code receiving from remote
maximumRange = 20
}

while (results.value == 154154){
maximumRange = 50
}

I do not see my mistake. Probably because I'm new in the Arduino World :slight_smile:

Anyway, thanks for the support!

You're missing a semi colon ...

while (results.value == 168411)
{  //code receiving from remote
	maximumRange = 20;
}

while (results.value == 154154)
{
	maximumRange = 50;
}

I do not see my mistake. Probably because I'm new in the Arduino World

Aside from the missing ;. STOP using while everywhere where you want to do something ONCE!

There is NO reason to assign the same value to the same variable OVER AND OVER AND OVER!

Thank you for the feedback guys. It helped me a lot. Now it's working the way I want it!