# Finding the difference between two numbers Positive and negative

Morning all, relatively new to the Arduino and scripting.

I am currently working on a small project using the HMC5833L chip (currently prototyping) lol, and i have managed to pull the data from the chip no problem, i am then dividing that data by 10, in order to get a more manageable and less bouncy result... so my final result is along the lines of

Values: 3 4 -59 Change: 3 4 -59
Values: 4 4 -59 Change: 4 4 -59
Values: 4 4 -59 Change: 4 4 -59
Values: 3 4 -59 Change: 3 4 -59
Values: 3 4 -59 Change: 3 4 -59
Values: 4 4 -59 Change: 4 4 -59
Values: 4 4 -59 Change: 4 4 -59
Values: 4 4 -59 Change: 4 4 -59
Values: 4 4 -59 Change: 4 4 -59

This is somewhat what i am looking for however, as you will see under the column CHANGE : the numbers are exactly the same, i want to know the difference between the previous reading and the new reading.. so in most cases the result should be 0 0 0.

Now i have tried the basic maths functions, however, since the results can be both positive and negative numbers, that does not work. the above print out shows the + operand. i will past the - operand below.

Values: 4 4 -59 Change: -4 -4 59
Values: 4 4 -59 Change: -4 -4 59
Values: 4 4 -59 Change: -4 -4 59
Values: 4 4 -60 Change: -4 -4 60
Values: 4 4 -59 Change: -4 -4 59
Values: 4 4 -59 Change: -4 -4 59
Values: 3 4 -59 Change: -3 -4 59
Values: 3 4 -59 Change: -3 -4 59
Values: 4 4 -59 Change: -4 -4 59

as you can see this just takes the number and changes wether its positive or negative..
what i need is below (not manually edited)

Values: 4 4 -59 Change: 0 0 0
Values: 4 4 -59 Change: 0 0 0
Values: 4 4 -59 Change: 0 0 0
Values: 4 4 -60 Change: 0 0 1
Values: 4 4 -59 Change: 0 0 -1
Values: 4 4 -59 Change: 0 0 0
Values: 3 4 -59 Change: -1 0 0
Values: 3 4 -59 Change: 0 0 0
Values: 4 4 -59 Change: 1 0 0

if anyone can help i would be grateful...

here ist he simple code im using

//WORK OUT THE CHANGE EACH CYCLE

u=x/10; // Divide raw data by 10
v=y/10; // Divide raw data by 10
w=z/10; // Divide raw data by 10

changea = r-u;
changeb = s-v;
changec = t-w;

r = u;
s = v;
t = w;

cheers
keith

You haven’t posted the entire sketch. The fault is not in the snippet you’ve provided - it seldom is,
which is why we explicitly ask for entire sketches to be posted.

Here is the full sketch, this is mainly a demo sketch slightly modified in order to provide what i need.

``````#include <Wire.h> //I2C Arduino Library

void setup(){

Serial.begin(9600);
Serial.print("HMC5833L COMPASS SENSOR BEGIN");
Serial.println();
Wire.begin();

Wire.write(0x02); // Set the Register
Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
Wire.endTransmission();

}

void loop(){

int x,y,z; //triple axis data
int r,s,t,u,v,w;
int changea, changeb, changec;

//Tell the HMC what regist to begin writing data into
Wire.endTransmission();

//Read the data.. 2 bytes for each axis.. 6 total bytes
if(6<=Wire.available()){
}

//WORK OUT THE CHANGE EACH CYCLE

u=x/10;  // Divide raw data by 10
v=y/10;  // Divide raw data by 10
w=z/10;  // Divide raw data by 10

changea = r-u;  //set changea based on previous result
changeb = s-v;  //set changeb based on previous result
changec = t-w;  //set changec based on previous result

r = u;  //reset r to newer result
s = v;  //reset r to newer result
t = w;  //reset r to newer result

Serial.print("Values: ");
Serial.print(u);
Serial.print(" ");
Serial.print(v);
Serial.print(" ");
Serial.print(w);

Serial.print("   Change: ");
Serial.print(changea);
Serial.print(" ");
Serial.print(changeb);
Serial.print(" ");
Serial.print(changec);

Serial.println();

delay(10);
}
``````

Cheers
keith

Welcome,

Something like that : 7kMFLB - Online C++ Compiler & Debugging Tool - Ideone.com

Try making these global:

``````  int r, s, t, u, v, w;
``````

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.