1. Leigh Honeywell
  2. Compass Hat


Compass Hat / hat.pde


LED and haptic buzzer display for an HMC6352 compass

#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;

// The setup() method runs once, when the sketch starts
void setup()   {
  //setup for the compass
  // 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()                     
  int heading = (int)hmc6352.getHeading();
  Serial.print(" raw heading\r\n");
  heading = (int)(heading / 90);
  Serial.print(" comparison\r\n");
  // ASCII '0' through '9' characters are
  // represented by the values 48 through 57.
  // so if the user types a number from 0 through 9 in ASCII, 
  // you can subtract 48 to get the actual value:
  int bitToSet = heading + 1;
  // write to the shift register with the correct bit set high:
  registerWrite(bitToSet, HIGH);

// 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);