|
| 1 | +/* |
| 2 | + MicroOLED_Clock.ino |
| 3 | + Analog Clock demo using SFE_MicroOLED Library |
| 4 | + Jim Lindblom @ SparkFun Electronics |
| 5 | + Original Creation Date: October 27, 2014 |
| 6 | +
|
| 7 | + This sketch uses the MicroOLED library to draw an |
| 8 | + analog clock. |
| 9 | +
|
| 10 | + Hardware Connections: |
| 11 | + This example assumes you are using Qwiic. See the SPI examples for |
| 12 | + a detailed breakdown of connection info. |
| 13 | +
|
| 14 | + This code is beerware; if you see me (or any other SparkFun employee) at the |
| 15 | + local, and you've found our code helpful, please buy us a round! |
| 16 | +
|
| 17 | + Distributed as-is; no warranty is given. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <Wire.h> |
| 21 | +#include <SFE_MicroOLED.h> //Click here to get the library: http://librarymanager/All#SparkFun_Micro_OLED |
| 22 | + |
| 23 | +#define DC_JUMPER 1 |
| 24 | +#define PIN_RESET 9 // Optional - Connect RST on display to pin 9 on Arduino |
| 25 | + |
| 26 | +MicroOLED oled(PIN_RESET, DC_JUMPER); //Example I2C declaration |
| 27 | + |
| 28 | +// Use these variables to set the initial time |
| 29 | +int hours = 11; |
| 30 | +int minutes = 50; |
| 31 | +int seconds = 30; |
| 32 | + |
| 33 | +// How fast do you want the clock to spin? Set this to 1 for fun. |
| 34 | +// Set this to 1000 to get _about_ 1 second timing. |
| 35 | +const int CLOCK_SPEED = 1000; |
| 36 | + |
| 37 | +// Global variables to help draw the clock face: |
| 38 | +const int MIDDLE_Y = oled.getLCDHeight() / 2; |
| 39 | +const int MIDDLE_X = oled.getLCDWidth() / 2; |
| 40 | + |
| 41 | +int CLOCK_RADIUS; |
| 42 | +int POS_12_X, POS_12_Y; |
| 43 | +int POS_3_X, POS_3_Y; |
| 44 | +int POS_6_X, POS_6_Y; |
| 45 | +int POS_9_X, POS_9_Y; |
| 46 | +int S_LENGTH; |
| 47 | +int M_LENGTH; |
| 48 | +int H_LENGTH; |
| 49 | + |
| 50 | +unsigned long lastDraw = 0; |
| 51 | + |
| 52 | +void setup() |
| 53 | +{ |
| 54 | + delay(100); //Give display time to power on |
| 55 | + |
| 56 | + Wire.begin(); //Set up I2C bus |
| 57 | + |
| 58 | + oled.begin(); // Initialize the OLED |
| 59 | + oled.clear(PAGE); // Clear the display's internal memory |
| 60 | + oled.clear(ALL); // Clear the library's display buffer |
| 61 | + oled.display(); // Display what's in the buffer (splashscreen) |
| 62 | + |
| 63 | + initClockVariables(); |
| 64 | + |
| 65 | + oled.clear(ALL); |
| 66 | + drawFace(); |
| 67 | + drawArms(hours, minutes, seconds); |
| 68 | + oled.display(); // display the memory buffer drawn |
| 69 | +} |
| 70 | + |
| 71 | +void loop() |
| 72 | +{ |
| 73 | + // Check if we need to update seconds, minutes, hours: |
| 74 | + if (lastDraw + CLOCK_SPEED < millis()) |
| 75 | + { |
| 76 | + lastDraw = millis(); |
| 77 | + // Add a second, update minutes/hours if necessary: |
| 78 | + updateTime(); |
| 79 | + |
| 80 | + // Draw the clock: |
| 81 | + oled.clear(PAGE); // Clear the buffer |
| 82 | + drawFace(); // Draw the face to the buffer |
| 83 | + drawArms(hours, minutes, seconds); // Draw arms to the buffer |
| 84 | + oled.display(); // Draw the memory buffer |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void initClockVariables() |
| 89 | +{ |
| 90 | + // Calculate constants for clock face component positions: |
| 91 | + oled.setFontType(0); |
| 92 | + CLOCK_RADIUS = min(MIDDLE_X, MIDDLE_Y) - 1; |
| 93 | + POS_12_X = MIDDLE_X - oled.getFontWidth(); |
| 94 | + POS_12_Y = MIDDLE_Y - CLOCK_RADIUS + 2; |
| 95 | + POS_3_X = MIDDLE_X + CLOCK_RADIUS - oled.getFontWidth() - 1; |
| 96 | + POS_3_Y = MIDDLE_Y - oled.getFontHeight() / 2; |
| 97 | + POS_6_X = MIDDLE_X - oled.getFontWidth() / 2; |
| 98 | + POS_6_Y = MIDDLE_Y + CLOCK_RADIUS - oled.getFontHeight() - 1; |
| 99 | + POS_9_X = MIDDLE_X - CLOCK_RADIUS + oled.getFontWidth() - 2; |
| 100 | + POS_9_Y = MIDDLE_Y - oled.getFontHeight() / 2; |
| 101 | + |
| 102 | + // Calculate clock arm lengths |
| 103 | + S_LENGTH = CLOCK_RADIUS - 2; |
| 104 | + M_LENGTH = S_LENGTH * 0.7; |
| 105 | + H_LENGTH = S_LENGTH * 0.5; |
| 106 | +} |
| 107 | + |
| 108 | +// Simple function to increment seconds and then increment minutes |
| 109 | +// and hours if necessary. |
| 110 | +void updateTime() |
| 111 | +{ |
| 112 | + seconds++; // Increment seconds |
| 113 | + if (seconds >= 60) // If seconds overflows (>=60) |
| 114 | + { |
| 115 | + seconds = 0; // Set seconds back to 0 |
| 116 | + minutes++; // Increment minutes |
| 117 | + if (minutes >= 60) // If minutes overflows (>=60) |
| 118 | + { |
| 119 | + minutes = 0; // Set minutes back to 0 |
| 120 | + hours++; // Increment hours |
| 121 | + if (hours >= 12) // If hours overflows (>=12) |
| 122 | + { |
| 123 | + hours = 0; // Set hours back to 0 |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Draw the clock's three arms: seconds, minutes, hours. |
| 130 | +void drawArms(int h, int m, int s) |
| 131 | +{ |
| 132 | + double midHours; // this will be used to slightly adjust the hour hand |
| 133 | + static int hx, hy, mx, my, sx, sy; |
| 134 | + |
| 135 | + // Adjust time to shift display 90 degrees ccw |
| 136 | + // this will turn the clock the same direction as text: |
| 137 | + h -= 3; |
| 138 | + m -= 15; |
| 139 | + s -= 15; |
| 140 | + if (h <= 0) |
| 141 | + h += 12; |
| 142 | + if (m < 0) |
| 143 | + m += 60; |
| 144 | + if (s < 0) |
| 145 | + s += 60; |
| 146 | + |
| 147 | + // Calculate and draw new lines: |
| 148 | + s = map(s, 0, 60, 0, 360); // map the 0-60, to "360 degrees" |
| 149 | + sx = S_LENGTH * cos(PI * ((float)s) / 180); // woo trig! |
| 150 | + sy = S_LENGTH * sin(PI * ((float)s) / 180); // woo trig! |
| 151 | + // draw the second hand: |
| 152 | + oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + sx, MIDDLE_Y + sy); |
| 153 | + |
| 154 | + m = map(m, 0, 60, 0, 360); // map the 0-60, to "360 degrees" |
| 155 | + mx = M_LENGTH * cos(PI * ((float)m) / 180); // woo trig! |
| 156 | + my = M_LENGTH * sin(PI * ((float)m) / 180); // woo trig! |
| 157 | + // draw the minute hand |
| 158 | + oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + mx, MIDDLE_Y + my); |
| 159 | + |
| 160 | + midHours = minutes / 12; // midHours is used to set the hours hand to middling levels between whole hours |
| 161 | + h *= 5; // Get hours and midhours to the same scale |
| 162 | + h += midHours; // add hours and midhours |
| 163 | + h = map(h, 0, 60, 0, 360); // map the 0-60, to "360 degrees" |
| 164 | + hx = H_LENGTH * cos(PI * ((float)h) / 180); // woo trig! |
| 165 | + hy = H_LENGTH * sin(PI * ((float)h) / 180); // woo trig! |
| 166 | + // draw the hour hand: |
| 167 | + oled.line(MIDDLE_X, MIDDLE_Y, MIDDLE_X + hx, MIDDLE_Y + hy); |
| 168 | +} |
| 169 | + |
| 170 | +// Draw an analog clock face |
| 171 | +void drawFace() |
| 172 | +{ |
| 173 | + // Draw the clock border |
| 174 | + oled.circle(MIDDLE_X, MIDDLE_Y, CLOCK_RADIUS); |
| 175 | + |
| 176 | + // Draw the clock numbers |
| 177 | + oled.setFontType(0); // set font type 0, please see declaration in SFE_MicroOLED.cpp |
| 178 | + oled.setCursor(POS_12_X, POS_12_Y); // points cursor to x=27 y=0 |
| 179 | + oled.print(12); |
| 180 | + oled.setCursor(POS_6_X, POS_6_Y); |
| 181 | + oled.print(6); |
| 182 | + oled.setCursor(POS_9_X, POS_9_Y); |
| 183 | + oled.print(9); |
| 184 | + oled.setCursor(POS_3_X, POS_3_Y); |
| 185 | + oled.print(3); |
| 186 | +} |
0 commit comments