13Ko-Robo / Mbed 2 deprecated main2016

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Revision:
1:d0cada9a5381
Child:
2:a8bbf677e774
diff -r 174d34b8177d -r d0cada9a5381 compass.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compass.h	Tue Aug 04 04:13:02 2015 +0000
@@ -0,0 +1,45 @@
+#ifndef COMPASS_H
+#define COMPASS_H
+
+#include "mbed.h"
+#include "HMC6352.h"
+
+class Compass {
+private:
+    float initialAngle;
+    HMC6352* hmc6352;
+
+public:
+    float diffAngle;
+    Compass(PinName sda, PinName scl);
+    void measureAngle(void);
+};
+
+Compass::Compass(PinName sda, PinName scl) {
+    hmc6352 = new HMC6352(sda, scl);
+    hmc6352->setOpMode(HMC6352_CONTINUOUS, 1, 20);
+    wait(0.1);
+    this->initialAngle = hmc6352->sample() / 10.0;
+}
+
+void Compass::measureAngle(void) {
+    wait(0.1);
+    this->diffAngle = hmc6352->sample() / 10.0 - initialAngle;
+}
+
+#endif
+
+/*
+ * example program
+
+#include "mbed.h"
+#include "compass.h"
+
+int main(void) {
+    Compass compass(D14, D15);
+    while (1) {
+        compass.measureAngle();
+        pc.printf("%f\r\n", compass.diffAngle);
+    }
+}
+ */
\ No newline at end of file