/* TinyDuino Compass Demo This example code is in the public domain. January 26, 2015 by Tony Batey https://tiny-circuits.com/ */ #include #define Addr 0x1E // 7-bit address of HMC5883 compass void setup() { Serial.begin(9600); delay(100); // Power up delay Wire.begin(); // Set operating mode to continuous Wire.beginTransmission(Addr); Wire.write(byte(0x02)); Wire.write(byte(0x00)); Wire.endTransmission(); } int x_max=-10000; // Starting values for hard iron calibration int y_max=-10000; // We want these values to be extreme in the int x_min=10000; // opposite direction so it calibrates nicely int y_min=10000; void loop() { int x, y, z; // Initiate communications with compass 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(); } // Print raw values Serial.print("X="); Serial.print(x); Serial.print(", Y="); Serial.print(y); Serial.print(", Z="); Serial.print(z); if(x > x_max) //Find values of hard iron distortion x_max = x; //This will store the max and min values if(y >y_max) //of the magnetic field around you y_max = y; if(y21) return; char highpin[21]={6,7,6,8,5,8,8,7,9,7,9,8,5,9,6,9,9,5,6,5,7}; char lowpin[21]= {7,6,8,6,8,5,7,8,7,9,8,9,9,5,9,6,4,6,5,7,5}; ledNum--; digitalWrite(highpin[ledNum],HIGH); digitalWrite(lowpin[ledNum],LOW); pinMode(highpin[ledNum],OUTPUT); pinMode(lowpin[ledNum],OUTPUT); }