Advertisement
What_Ever

compass.ino

Mar 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. /*
  2.   ChronoLCD Color - An example sketch for the Color LCD Shield Library
  3.   by: Jim Lindblom
  4.   license: CC-BY SA 3.0 - Creative commons share-alike 3.0
  5.   use this code however you'd like, just keep this license and
  6.   attribute. Let me know if you make hugely, awesome, great changes.
  7.  
  8.   This sketch draws an analog and digital clock on the Color LCD
  9.   Shield. You can also use the on-board buttons to set the hours
  10.   and minutes.
  11.  
  12.   Use the defines at the top of the code to set the initial time.
  13.   You can also adjust the size and color of the clock.
  14.  
  15.   To set the time, first hit S3. Then use S1 and S2 to adjust the
  16.   hours and minutes respsectively. Hit S3 to start the clock
  17.   back up.
  18.  
  19.   This example code should give you a good idea of how to use
  20.   the setCircle, setLine, and setStr functions of the Color LCD
  21.   Shield Library.
  22. */
  23. #include <ColorLCDShield.h>
  24. #include <Wire.h>
  25. #include <Adafruit_Sensor.h>
  26. #include <Adafruit_LSM303_U.h>
  27.  
  28. #define CIRCLE_RADIUS 50  // radius of clock face
  29. #define CIRCLE_CENTER 55  // If you adjust the radius, you'll probably want to adjust this
  30. #define S_LENGTH  48  // length of compass hand
  31. #define Q_LENGTH  40  // length of arrow hand
  32.  
  33. #define BACKGROUND  BLACK  // room for growth, adjust the background color according to daylight
  34. #define C_COLOR  RED  // This is the color of the circle
  35. #define S_COLOR  YELLOW  // compass hand color
  36. #define Q_COLOR  GREEN   // qibla hand color
  37. #define T_COLOR  RED     // text color;
  38.  
  39. LCDShield lcd;
  40.  
  41. Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
  42. bool magSetup = false;
  43. void setup()
  44. {
  45.   pinMode(10, OUTPUT);
  46.   analogWrite(10, 1023); //PWM control blacklight
  47.   /* Initialize the LCD, set the contrast, clear the screen */
  48.   lcd.init(PHILLIPS);
  49.   lcd.contrast(40);
  50.   lcd.clear(BACKGROUND);
  51.  
  52.   magSetup = mag.begin();
  53.    
  54.  
  55.   drawCompass();  // Draw the compass face, this includes N, E, S, W
  56.   drawCompassHands();  // Draw the compass hands
  57. }
  58.  
  59. void loop()
  60. {
  61.   delay(250);
  62.   /* Once each 250 milliseconds, we'll redraw the compass with new values */
  63.   drawCompass();
  64.   drawCompassHands();
  65. }
  66.  
  67. /*
  68.   drawCompass() simply draws the outer circle of the compass, and 'N',
  69.   'E', 'S', and 'W'.
  70. */
  71. void drawCompass()
  72. {
  73.   /* Draw the circle */
  74.   lcd.setCircle(CIRCLE_CENTER, 66, CIRCLE_RADIUS, C_COLOR);
  75.  
  76.   /* Print 12, 3, 6, 9, a lot of arbitrary values are used here
  77.      for the coordinates. Just used trial and error to get them
  78.      into a nice position. */
  79.   lcd.setStr("E", CIRCLE_CENTER - CIRCLE_RADIUS, 66-9, C_COLOR, BACKGROUND);
  80.   lcd.setStr("S", CIRCLE_CENTER - 9, 66 + CIRCLE_RADIUS - 12, C_COLOR, BACKGROUND);
  81.   lcd.setStr("W", CIRCLE_CENTER + CIRCLE_RADIUS - 18, 66-4, C_COLOR, BACKGROUND);
  82.   lcd.setStr("N", CIRCLE_CENTER - 9, 66 - CIRCLE_RADIUS + 4, C_COLOR, BACKGROUND);
  83. }
  84.  
  85. /*
  86.   drawCompassHands() draws the compas and qibla hands in their proper
  87.   position. Room for growth here, I'd like to make the compass hands
  88.   arrow shaped, or at least thicker and more visible.
  89. */
  90. void drawCompassHands()
  91. {
  92.   static int sx, sy,qx,qy;
  93.    
  94.   /* Delete old lines: */
  95.   lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+sx, 66+sy, BACKGROUND);  // delete second hand
  96.   lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+qx, 66+qy, BACKGROUND);  // delete qibla hand
  97.  
  98.   /* Get a new sensor event */
  99.   sensors_event_t event;
  100.   mag.getEvent(&event);
  101.  
  102.   float Pi = 3.14159;
  103.  
  104.   // Calculate the angle of the vector y,x
  105.   float heading = -(atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi - 180;
  106.  
  107.   float nth = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
  108.  
  109.   // Normalize to 0-360
  110.   if (heading < 0)
  111.   {
  112.     heading = 360 + heading;
  113.   }
  114.  
  115.   if (nth < 0)
  116.   {
  117.     nth = nth + 360;
  118.   }
  119.  
  120.   /* Calculate and draw new lines: */
  121.   s = heading;  // map the 0-60, to "360 degrees"
  122.  
  123.   sx = S_LENGTH * sin(3.14 * ((double) s)/180);  // woo trig!
  124.   sy = S_LENGTH * cos(3.14 * ((double) s)/180);  // woo trig!
  125.   qx = Q_LENGTH * sin(3.14 * ((double) s + 225)/180);  // woo trig!
  126.   qy = Q_LENGTH * cos(3.14 * ((double) s + 225)/180);  // woo trig!
  127.  
  128.   lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+sx, 66+sy, S_COLOR);  // print second hand
  129.   lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+qx, 66+qy, M_COLOR);
  130.   char angle[10];
  131.   dtostrf(nth, 8, 1, angle);
  132.   lcd.setStr(angle, CIRCLE_CENTER + CIRCLE_RADIUS + 4, 22, C_COLOR, BACKGROUND);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement