Commits

Leigh Honeywell committed 6f3c7a3

currently has 4 LEDs working with the compass, but not with a shift register, and the buzzer is not yet implemented.

  • Participants

Comments (0)

Files changed (1)

+/*
+
+LED and haptic buzzer display for an HMC6352 compass
+ 
+*/
+
+#include <hmc6352.h>
+#include <Wire.h>
+
+Hmc6352 hmc6352;
+
+int buzzerPin = 3;
+int ledPin1 =  6;    // LED connected to digital pin 13
+int ledPin2 =  9;
+int ledPin3 = 10;
+int ledPin4 = 11;
+int heading =  3;
+
+// 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);
+  pinMode(ledPin1, OUTPUT);
+  pinMode(ledPin2, OUTPUT);
+  pinMode(ledPin3, OUTPUT);
+  pinMode(ledPin4, OUTPUT);
+  analogWrite(buzzerPin, LOW);
+  digitalWrite(ledPin1, LOW);
+  digitalWrite(ledPin2, LOW);
+  digitalWrite(ledPin3, LOW);
+  digitalWrite(ledPin4, LOW);
+}
+
+// the loop() method runs over and over again,
+// as long as the Arduino has power
+
+void loop()                     
+{
+  hmc6352.wake();
+  int heading = (int)hmc6352.getHeading();
+  hmc6352.sleep();
+ 
+  Serial.print(heading,DEC);
+  Serial.print(" raw heading\r\n");
+  heading = (int)(heading / 90);
+  Serial.print(heading,DEC);
+  Serial.print(" comparison\r\n");
+  switch (heading) { 
+  case 0:
+    digitalWrite(ledPin1, HIGH);   // set the LED on
+    delay(100);                  // wait for a second
+    digitalWrite(ledPin1, LOW);    // set the LED off
+    break;
+  case 1:
+    digitalWrite(ledPin2, HIGH);   // set the LED on
+    delay(100);                  // wait for a second
+    digitalWrite(ledPin2, LOW);    // set the LED off
+    break;
+  case 2:
+    digitalWrite(ledPin3, HIGH);   // set the LED on
+    delay(100);                  // wait for a second
+    digitalWrite(ledPin3, LOW);    // set the LED off
+    break;
+  case 3:
+    digitalWrite(ledPin4, HIGH);   // set the LED on
+    delay(100);                 // wait for a second
+    digitalWrite(ledPin4, LOW);    // set the LED off
+    break;
+  default:
+    digitalWrite(buzzerPin, HIGH);
+    delay(100);
+    digitalWrite(buzzerPin, LOW);
+    break;
+  }
+}