Commits

Leigh Honeywell committed c0b365b

working buzzer, and corrected math!

Comments (0)

Files changed (1)

 /*
 
-LED and haptic buzzer display for an HMC6352 compass
+LED and haptic buzzer display for an HMC6352 compass.  GPL v3.
  
 */
 
 
 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()   {
 // as long as the Arduino has power
 
 void loop()                     
-{
+{  
   hmc6352.wake();
-  int heading = (int)hmc6352.getHeading();
+  float heading = hmc6352.getHeading();
   hmc6352.sleep();
  
   Serial.print(heading,DEC);
   Serial.print(" raw heading\r\n");
-  heading = (int)(heading / 45);
-  //heading = 14;
-  Serial.print(heading,DEC);
+  //0 - 7 (may need floor)
+  int headingLED = (int)((heading - 22.5)/45.0);
+  
+  Serial.print(headingLED,DEC);
   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;
   
   // write to the shift register with the correct bit set high:
-  registerWrite(bitToSet, 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 * 255.0);
+  } else if(negheading <= buzzerlim) {
+    buzzerness = (int)((buzzerlim - negheading)/buzzerlim * 255.0);
+  }
+  
+  analogWrite(buzzerPin, buzzerness);
   }
 
 // This method sends bits to the shift register:
     // turn on the output so the LEDs can light up:
   digitalWrite(latchPin, HIGH);
 }
-