Hey guys. im new here and my this is my first question. ![]()
So i have a sensor that give me values and i need to compare the deifrence between the last two result that sensor gave me.
ex:
...
...
x=500
x=600
so i want to pick this last two results and make the math with it ![]()
objective
if(x2 - x1) ==100
{ Do something}
How can i save this last two results in a variable and compare it?
Simple.Store the readings in an Array and then do the math you want retriving the values from the array
unsigned int myReadings[5];//Can store 5 readings, adjust to how many readings you want
sensorReading = myReadings[0];//first read
...do someting else
sensorReading = myReadings[1]; //second readif(myReadings[0] - myReadings[1]) >=100
{ Do something}
Something like this. Have two variables, lastVal and secondlastVal.
This is VERY pseudo code....
initialise lastVal
initialise seconflastVal
//loop from here
copy lastVal to secondlastVal //save the current reading, before overwriting with sensor input
read sensor into lastVal //overwrites previous
//now we have the last two readings
do stuff with them
repeat from top
I am pretty sure that the AVR chip in your Arduino has a comparator built in. I'll look it up.
Yes indeed in the 328 family there is an analog comparator, in section 23 of the datasheet.
I can no longer cut&paste but if you'll bear with me;
The comparator measures the input values on positive pin AIN0 and negative pin AIN1. When the voltage on AIN0 is higher than the voltage on AIN1, the Analog Comparator Output ACO (letter O, not numeral 0) is set and that can be made to trigger Timer/Counter1 Capture function (it says) or a special Analog comparator interrupt.
And that's just part of the overview, the datasheet goes into every detail you could want in the rest of the chapter.
Op Amps are also good for comparing voltages.
There is probably a circuit you could build that would take a digital pin HIGH or LOW depending on combined signals.
thank you all. i try to use and array. now i have 4 difrente variables but all with same value becouse i cant make the sensor read, give the value to a variable, then read again and only after give that second value to second variable. ere is my code
Wire.beginTransmission(Addr);
Wire.write(byte(0x03)); // Send request to X MSB register
Wire.endTransmission();Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
if(Wire.available() <=6) { // If 6 bytes available
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
}for (k=0; k<(4); k++) {
myReadings[k]=x;
}
Serial.println(myReadings[0]);
Serial.println(myReadings[1]);
Serial.println(myReadings[2]);
Serial.println(myReadings[3]);
Even if i copy past the first part where it read the sensor and give value to another variable again the x y z still the same only change when code starts form the beggining again
i did it lke this. not sure if its right though.
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
myReadings[0]=x;
x |= Wire.read(); //X lsb
myReadings[1]=x;
if(Wire.available() <=6) { // If 6 bytes available
That code says to execute the body if there are 6 bytes OR LESS, not 6 bytes or more.
The first byte arrives; you read 6 and exit the function. That hardly seems like the correct thing to do.