1. Leigh Honeywell
  2. Compass Hat

Source

Compass Hat / hat.pde

Leigh Honeywell 6f3c7a3 

Leigh Honeywell c0b365b 
Leigh Honeywell 6f3c7a3 








Leigh Honeywell 9ab5dda 







Leigh Honeywell 6f3c7a3 

Leigh Honeywell c0b365b 



Leigh Honeywell 6f3c7a3 











Leigh Honeywell 9ab5dda 





Leigh Honeywell 6f3c7a3 





Leigh Honeywell c0b365b 
Leigh Honeywell 6f3c7a3 
Leigh Honeywell c0b365b 
Leigh Honeywell 6f3c7a3 



Leigh Honeywell c0b365b 



Leigh Honeywell 6f3c7a3 
Leigh Honeywell 9ab5dda 

Leigh Honeywell c0b365b 








Leigh Honeywell 018a243 
Leigh Honeywell c0b365b 
Leigh Honeywell 018a243 
Leigh Honeywell c0b365b 


Leigh Honeywell 6f3c7a3 
Leigh Honeywell 9ab5dda 

















Leigh Honeywell 6f3c7a3 
/*

LED and haptic buzzer display for an HMC6352 compass.  GPL v3.
 
*/

#include <hmc6352.h>
#include <Wire.h>

Hmc6352 hmc6352;

int buzzerPin = 3;

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;

int heading =  3;

//Largest angle in degrees to either side that buzzer starts (less than 180)
int buzzerlim = 90;


// The setup() method runs once, when the sketch starts
  
void setup()   {
  //setup for the compass
  Serial.begin(9600);
  delay(100);
  
  Serial.print("RST\r\n");delay(100);  
 
  // initialize the digital pin as an output:
  pinMode(buzzerPin, OUTPUT);
  analogWrite(buzzerPin, LOW);
  
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{  
  hmc6352.wake();
  float heading = hmc6352.getHeading();
  hmc6352.sleep();
 
  Serial.print(heading,DEC);
  Serial.print(" raw heading\r\n");
  //0 - 7 (may need floor)
  int headingLED = (int)((heading - 22.5)/45.0);
  
  Serial.print(headingLED,DEC);
  Serial.print(" comparison\r\n");
  
  // write to the shift register with the correct bit set high:
  registerWrite(headingLED, HIGH);
  
  float negheading = 360.0 - heading;
  //Between 0-255 for PWM
  int buzzerness = 0;
  
  if(heading == 0) {
    buzzerness = 255;
  } else if(heading <= buzzerlim) {
    buzzerness = (int)((buzzerlim - heading)/buzzerlim * 65.0) + 190;
  } else if(negheading <= buzzerlim) {
    buzzerness = (int)((buzzerlim - negheading)/buzzerlim * 65.0) + 190;
  }
  
  analogWrite(buzzerPin, buzzerness);
  }

// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte bitsToSend = 0;

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}