Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ChronoLCD Color - An example sketch for the Color LCD Shield Library
- by: Jim Lindblom
- license: CC-BY SA 3.0 - Creative commons share-alike 3.0
- use this code however you'd like, just keep this license and
- attribute. Let me know if you make hugely, awesome, great changes.
- This sketch draws an analog and digital clock on the Color LCD
- Shield. You can also use the on-board buttons to set the hours
- and minutes.
- Use the defines at the top of the code to set the initial time.
- You can also adjust the size and color of the clock.
- To set the time, first hit S3. Then use S1 and S2 to adjust the
- hours and minutes respsectively. Hit S3 to start the clock
- back up.
- This example code should give you a good idea of how to use
- the setCircle, setLine, and setStr functions of the Color LCD
- Shield Library.
- */
- #include <ColorLCDShield.h>
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_LSM303_U.h>
- #define CIRCLE_RADIUS 50 // radius of clock face
- #define CIRCLE_CENTER 55 // If you adjust the radius, you'll probably want to adjust this
- #define S_LENGTH 48 // length of compass hand
- #define Q_LENGTH 40 // length of arrow hand
- #define BACKGROUND BLACK // room for growth, adjust the background color according to daylight
- #define C_COLOR RED // This is the color of the circle
- #define S_COLOR YELLOW // compass hand color
- #define Q_COLOR GREEN // qibla hand color
- #define T_COLOR RED // text color;
- LCDShield lcd;
- Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
- bool magSetup = false;
- void setup()
- {
- pinMode(10, OUTPUT);
- analogWrite(10, 1023); //PWM control blacklight
- /* Initialize the LCD, set the contrast, clear the screen */
- lcd.init(PHILLIPS);
- lcd.contrast(40);
- lcd.clear(BACKGROUND);
- magSetup = mag.begin();
- drawCompass(); // Draw the compass face, this includes N, E, S, W
- drawCompassHands(); // Draw the compass hands
- }
- void loop()
- {
- delay(250);
- /* Once each 250 milliseconds, we'll redraw the compass with new values */
- drawCompass();
- drawCompassHands();
- }
- /*
- drawCompass() simply draws the outer circle of the compass, and 'N',
- 'E', 'S', and 'W'.
- */
- void drawCompass()
- {
- /* Draw the circle */
- lcd.setCircle(CIRCLE_CENTER, 66, CIRCLE_RADIUS, C_COLOR);
- /* Print 12, 3, 6, 9, a lot of arbitrary values are used here
- for the coordinates. Just used trial and error to get them
- into a nice position. */
- lcd.setStr("E", CIRCLE_CENTER - CIRCLE_RADIUS, 66-9, C_COLOR, BACKGROUND);
- lcd.setStr("S", CIRCLE_CENTER - 9, 66 + CIRCLE_RADIUS - 12, C_COLOR, BACKGROUND);
- lcd.setStr("W", CIRCLE_CENTER + CIRCLE_RADIUS - 18, 66-4, C_COLOR, BACKGROUND);
- lcd.setStr("N", CIRCLE_CENTER - 9, 66 - CIRCLE_RADIUS + 4, C_COLOR, BACKGROUND);
- }
- /*
- drawCompassHands() draws the compas and qibla hands in their proper
- position. Room for growth here, I'd like to make the compass hands
- arrow shaped, or at least thicker and more visible.
- */
- void drawCompassHands()
- {
- static int sx, sy,qx,qy;
- /* Delete old lines: */
- lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+sx, 66+sy, BACKGROUND); // delete second hand
- lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+qx, 66+qy, BACKGROUND); // delete qibla hand
- /* Get a new sensor event */
- sensors_event_t event;
- mag.getEvent(&event);
- float Pi = 3.14159;
- // Calculate the angle of the vector y,x
- float heading = -(atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi - 180;
- float nth = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
- // Normalize to 0-360
- if (heading < 0)
- {
- heading = 360 + heading;
- }
- if (nth < 0)
- {
- nth = nth + 360;
- }
- /* Calculate and draw new lines: */
- s = heading; // map the 0-60, to "360 degrees"
- sx = S_LENGTH * sin(3.14 * ((double) s)/180); // woo trig!
- sy = S_LENGTH * cos(3.14 * ((double) s)/180); // woo trig!
- qx = Q_LENGTH * sin(3.14 * ((double) s + 225)/180); // woo trig!
- qy = Q_LENGTH * cos(3.14 * ((double) s + 225)/180); // woo trig!
- lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+sx, 66+sy, S_COLOR); // print second hand
- lcd.setLine(CIRCLE_CENTER, 66, CIRCLE_CENTER+qx, 66+qy, M_COLOR);
- char angle[10];
- dtostrf(nth, 8, 1, angle);
- lcd.setStr(angle, CIRCLE_CENTER + CIRCLE_RADIUS + 4, 22, C_COLOR, BACKGROUND);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement