Degrees mapping 360deg formulas

Hi all,

Im working on a project but cant seam to cum up with a solution to a issue im having.

im using poxzy sensor and from the imu i can get the orientation of the sensor but im having an issue with it would be reading 130 deg when i want to 90 to be read

so it would be easy reading_angle-40= 90 deg

but say if the reading_angle = 30 deg

my formula would be reading 30-40 = -10 deg

but i don't want to measure -10 deg i want 350 deg

anyone have a formula that i could use or any idea how i would solve this issue?


Something like ?

if (degreeVal < 0) {
   degreeVal += 360;


There is a Function in the Arduino Core called map wich could do this, take a look here: map() - Arduino Reference