How to save first readout and then compare to it



#include "Wire.h"

#include <MPU6050_light.h>

MPU6050 mpu(Wire);

unsigned long timer = 0;

void setup() {

  Serial.begin(9600);

  Wire.begin();
  
  byte status = mpu.begin();
  
Serial.print(F("MPU6050 status: "));

  Serial.println(status);
 
 while(status!=0){ } // stop everything if could not connect to MPU6050
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
 
 delay(1000);
 
 mpu.calcOffsets(); // gyro and accelero
 
 Serial.println("Done!\n");

}


void loop() {

  mpu.update();
  
 
 if((millis()-timer)>10){ // print data every 10ms
	
Serial.print("X : ");
	
Serial.print(mpu.getAngleX());
	

timer = millis();  
  
}

}

so my question is how can i save first value given by MPU6050 and then compare it to another value .

suppose,

if value of x = 23 degree after some time when i will compare it to vaiable y . i want this

if (y == x ) {
count++; // if y is current value and is equal to value of x
Serial.print(count);
Serial.print(" ; ");

Thank you ;

read it in the setup for example

read it in the setup for example

but where to read ???

please tell me clear , thanks for reading this message

If you want to measure how much a value has changed then use 2 variables, let's name them currentValue and previousValue

Before you read currentValue save it to previousValue. After reading currentValue compare it to previousValue to determine whether it has changed and by how much

but where to read ???

something like this in at the end of the setup() function where x_start is a global variable

mpu.update();
x_start = mpu.getAngleX();

but how can we save a value which is yet to come and also we don't know how much is yet to come .
is there a way to save an indefinite value in variable .

what I tried

unsigned long currentValue ;
unsigned long previousValue;

x_axis = mpu.getAngel( );

currentValue = x_axis ;

delay (10);

currentValue != previousValue

if ( currentValue == previousValue);

// as currentValue is unpredictable both values will always be same . because currentValue is always changing.

thanks for reply.

will x_start can save first readout value of sensor and then store it to compare it with y value .

please explain me a bit more , thanks for reply.

A variable will always have a value, either a default value or one that you define. Take this simple example

const byte potPin = A7;
int currentPotValue; //defaults to zero
int previousPotValue; //defaults to zero

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  currentPotValue = analogRead(potPin) / 100;
  if (currentPotValue != previousPotValue)
  {
    Serial.print("value has changed to : ");
    Serial.println(currentPotValue);
  }
  previousPotValue = currentPotValue;
}

The first time that we read the pot then currentPotValue will almost certainly differ from previousPotValue and the difference will be reported. If that is a problem then ignore the first reading like this

const byte potPin = A7;
int currentPotValue; //defaults to zero
int previousPotValue; //defaults to zero
boolean firstReading = true;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  if (firstReading)
  {
    currentPotValue = analogRead(potPin) / 100;
    firstReading = false;
  }
  else
  {
    currentPotValue = analogRead(potPin) / 100;
    if (currentPotValue != previousPotValue)
    {
      Serial.print("value has changed to : ");
      Serial.println(currentPotValue);
    }
  }
  previousPotValue = currentPotValue;
}
#include "MPU6050.h"
 
 
MPU6050 mpu ;

int16_t gx, gy, gz;

int16_t ax, ay, az;

int currentValue; //defaults to zero

int previousValue; //defaults to zero

boolean firstReading = true;

int valax; 

int val;

int prevVal;



void setup()

{

  

  Wire.begin();

  Serial.begin(9600);

  Serial.println("Initialize MPU");

  mpu.initialize();

  Serial.println(mpu.testConnection() ? "Connected" : "Connection 

failed");

 while (!Serial);

}

void loop(){


mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

valax = map(ax, -17000, 17000, 0, 255);

Serial.println("ax: ");

Serial.println(valax);


  if (firstReading)

  {

    previousValue = valax;

    currentValue = previousValue / 360;

    firstReading = true;

 
 }

 delay (100);

currentValue = valax;

firstReading = false; // now first reading not equal to current 
reading;

currentValue != previousValue;

  
if (currentValue == previousValue) 

/*when sensor will at the same 

place where first reading was taken the both the values are equal
 
*/   
    {
      
      firstReading = true;
      
     Serial.print("rotation : ");
     
     Serial.println(currentValue);
    
     Serial.println(previousValue);
   
 }
  
}


i have tried what u told me please check it if it is correct or not thank u

  if (firstReading)
  {
    previousValue = valax;
    currentValue = previousValue / 360;
    firstReading = true;
  }

Compare your code with my example. The firstReading boolean causes the code to behave differently for the first ever reading. Once the first ever reading has been taken you need to set firstReading to false to prevent the special code executing again then don't change its value again in the code, which you are doing and which is wrong


if (firstReading)  {
  
    previousValue = valax;
   
   currentValue = previousValue / 360;

    firstReading = true;

  }


if ( currentValue == previousValue) {

count++;

Serial.println(count);

Serial.println("  rotations completed :  "); 

}

}

is it correct , thanks for your suggestion and knowledge about arduino.

There is so much wrong that I don't know where to start

firstReading = true;

You only need the special processing the first time that loop() runs. The special processing runs when firstReading is true, so after the special code has run you need to set firstReading to false to prevent it running again

Subsequent iterations of the loop() function will then use the normal processing

previousValue = valax;
    currentValue = previousValue / 360;

Why are you deriving the current value from the previous value ?

if ( currentValue == previousValue) {

Why increment count if two consecutive readings are the same ? Is this what you want to do ?

Serial.println("  rotations completed :  ")

How do you know that the rotations are complete ?

sorry , but i don't know much about arduino . that's why

i compared currentValue to previousValue to know if sensor is at the same position which means it has completed it's 360 degree rotation .

i used "count "to count number of rotations completed by sensor and used Serial.println(" rotations completed : ")
to get value of rotations.

Let's go back to basics. If I understand correctly you want to take a series of readings from a sensor and if the value has changed between readings then you do something.

If that is correct then here is the logic

start of loop()
  currentReading = value from sensor
  if currentReading does not equal previousReading
    do something
  end if
  copy currentReading to previosuReading ready for the next comparison
end of loop()

Please confirm that is what you want to do or explain what it is that you do want

no that's not what i want to do . i want , when currentReading equal previousReading do something

start of loop()
  currentReading = value from sensor
  if currentReading equals previousReading
    do something
  end if
  copy currentReading to previosuReading ready for the next comparison
end of loop()

There. That was easy wasn't it

yes , thanks

If that is the required logic then follow it and turn it into real code and let us know how you get on

hi how can i measure RPM using our previous code

If you are flying a drone for example.
You start lift off start in a direction.

Say you want to compare where you are to where you were.

You need to pick a point and call it Start.

It's easy to compare two points.

Your task is to figure out how to pick the first point.

That's the hard part. And apparently the point you are stuck on.