Final version, with changing analogue clock colours, Friday 11:20am
Dependencies: MMA8451Q SPI_TFT_ILI9341 TFT_fonts mbed
Smart clock by Duncan and Kieran
Photo
Equipment
- PCB: FRDM-KL25Z
- TFT Color Display: MI0283QT-9A
- Buzzer
- 3 x buttons
- 3 x 2kΩ resistors and 1 x 100Ω resistor
- Stripboard
- Wires (stranded is preferable due to their flexibility)
- USB to Mini-USB cable, and computer
Setup
PCB to TFT (Output)
FRDM-KL25Z | MI0283QT-9A |
---|---|
P3V3 | 3.3V, IM1, IM2, IM3 |
GND | RD, IM0, LEDK |
PTD5 | CS |
PTD1 | RS |
PTD0 | RST |
PTD2 | SDI |
PTD3 | SDO |
PTA13 | WR |
P5V_USB through 100Ω resistor (approx. 20mA) | LEDA |
The buzzer should be connected between PTA12 and ground.
Note: The resistance between P5V_USB and LEDA can be reduced to increase the brightness of the screen. However, resistors lower than 30Ω have not been tested, and may result in the current causing damage to the PCB.
PCB to Buttons (Inputs)
FRDM-KL25Z | Input |
---|---|
PTA5 | Minute button |
PTC8 | Hour button |
PTC9 | Time change button - toggles between changing the alarm time and the clock time |
Each button should be connected in series with a 2kΩ resistor between 3.3V and 0V, so that pressing the button switches between these two voltages.
Features
- A digital clock, displayed in 24-hour format
- An analogue clock, displaying the time in standard 12-hour format
- Alarm can be changed using the minute and hour buttons
- Time can be changed by holding the third button
- Alarm will sound for one minute at the displayed time
- Putting the alarm on its side will snooze the alarm if the alarm is going off (or just disable it if the alarm is not sounding)
main.cpp@5:f5fa116b57a1, 2017-05-24 (annotated)
- Committer:
- scat6490
- Date:
- Wed May 24 14:57:37 2017 +0000
- Revision:
- 5:f5fa116b57a1
- Parent:
- 4:2013f3158cc6
- Child:
- 6:d80627e78bfd
Display, clock, alarm, time changing, Wednesday 3:55pm; Commented and small changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scat6490 | 0:4fd6c8718206 | 1 | #include "stdio.h" |
scat6490 | 0:4fd6c8718206 | 2 | #include "mbed.h" |
scat6490 | 0:4fd6c8718206 | 3 | #include "SPI_TFT_ILI9341.h" |
scat6490 | 0:4fd6c8718206 | 4 | #include "string" |
scat6490 | 0:4fd6c8718206 | 5 | #include "Arial12x12.h" |
scat6490 | 0:4fd6c8718206 | 6 | #include "Arial24x23.h" |
scat6490 | 0:4fd6c8718206 | 7 | #include "Arial28x28.h" |
scat6490 | 0:4fd6c8718206 | 8 | #include "font_big.h" |
scat6490 | 0:4fd6c8718206 | 9 | |
scat6490 | 5:f5fa116b57a1 | 10 | Ticker timer; // Set up a timer |
scat6490 | 5:f5fa116b57a1 | 11 | DigitalOut buzzer(PTA12); // Set up inputs and outputs |
scat6490 | 5:f5fa116b57a1 | 12 | DigitalIn input1(PTA4); // Input for the minute button |
scat6490 | 5:f5fa116b57a1 | 13 | DigitalIn input60(PTC8); // Input for the hour button |
scat6490 | 5:f5fa116b57a1 | 14 | DigitalIn input_time_change(PTC9); // Input for the button which switches from changing the alarm and changing the time |
scat6490 | 5:f5fa116b57a1 | 15 | Serial pc(USBTX,USBRX); // Connection to the desktop, currently only used for debugging |
scat6490 | 0:4fd6c8718206 | 16 | SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z |
scat6490 | 0:4fd6c8718206 | 17 | |
scat6490 | 5:f5fa116b57a1 | 18 | int clock_minutes, clock_hours, clock_seconds = 0; // Initialise clock at 00:00.00 |
scat6490 | 5:f5fa116b57a1 | 19 | int alarm_hours = 7; // Initialise alarm at 7am |
scat6490 | 3:6a328c7e9b1c | 20 | int alarm_minutes = 0; |
scat6490 | 1:a33f6ca5a5b3 | 21 | |
scat6490 | 5:f5fa116b57a1 | 22 | void timeup(){ // Function to increment the clock every second |
scat6490 | 5:f5fa116b57a1 | 23 | clock_seconds++; // Increment the time |
scat6490 | 5:f5fa116b57a1 | 24 | clock_seconds %= 60; // Reset seconds to 0 if it gets to 60 |
scat6490 | 4:2013f3158cc6 | 25 | |
scat6490 | 4:2013f3158cc6 | 26 | if (clock_seconds==0) |
scat6490 | 4:2013f3158cc6 | 27 | { |
scat6490 | 5:f5fa116b57a1 | 28 | clock_minutes++; // Add a minute if the seconds got reset |
scat6490 | 5:f5fa116b57a1 | 29 | clock_minutes %= 60; // Reset the minutes to 0 if it gets to 60 |
scat6490 | 4:2013f3158cc6 | 30 | } |
scat6490 | 4:2013f3158cc6 | 31 | if (clock_minutes==0 && clock_seconds==0) |
scat6490 | 4:2013f3158cc6 | 32 | { |
scat6490 | 5:f5fa116b57a1 | 33 | clock_hours++; // Add an hour if the minutes got reset |
scat6490 | 5:f5fa116b57a1 | 34 | clock_hours %= 24; // Reset the hour to 0 if it gets to 24 |
scat6490 | 4:2013f3158cc6 | 35 | } |
scat6490 | 4:2013f3158cc6 | 36 | } |
scat6490 | 4:2013f3158cc6 | 37 | |
scat6490 | 4:2013f3158cc6 | 38 | void drawclock() // Draws an analogue clock using the clock_minutes and clock_hours variables |
scat6490 | 4:2013f3158cc6 | 39 | { |
scat6490 | 5:f5fa116b57a1 | 40 | int hourX, hourY, minuteX, minuteY, secondX, secondY; // Initialise variables to be used |
scat6490 | 5:f5fa116b57a1 | 41 | int circleX = 250; // Circle centre X |
scat6490 | 4:2013f3158cc6 | 42 | int circleY = 200; // Circle centre Y |
scat6490 | 4:2013f3158cc6 | 43 | int circleRadius = 30; // Circle radius |
scat6490 | 4:2013f3158cc6 | 44 | |
scat6490 | 4:2013f3158cc6 | 45 | double factorH = (2 * 3.141) / 12; // Convert hour to radians |
scat6490 | 4:2013f3158cc6 | 46 | double factorM = (2 * 3.141) / 60; // Convert minute to radians |
scat6490 | 5:f5fa116b57a1 | 47 | double factorS = (2 * 3.141) / 60; // Convert second to radians |
scat6490 | 4:2013f3158cc6 | 48 | |
scat6490 | 5:f5fa116b57a1 | 49 | hourX = floor(0.6 * circleRadius * sin((double) (clock_hours * factorH))); // Calculate difference in X values of the two ends of the hour hand. |
scat6490 | 5:f5fa116b57a1 | 50 | hourY = floor(0.6 * circleRadius * cos((double) (clock_hours * factorH))); // Same for Y of hour hand. The 0.9 means the hands are smaller than the circle |
scat6490 | 5:f5fa116b57a1 | 51 | minuteX = floor(0.95 * circleRadius * sin((double) (clock_minutes * factorM))); // Same for X of minute hand |
scat6490 | 5:f5fa116b57a1 | 52 | minuteY = floor(0.95 * circleRadius * cos((double) (clock_minutes * factorM))); // Same for Y of minute hand |
scat6490 | 5:f5fa116b57a1 | 53 | secondX = floor(0.85 * circleRadius * sin((double) (clock_seconds * factorS))); // Same for X of second hand |
scat6490 | 5:f5fa116b57a1 | 54 | secondY = floor(0.85 * circleRadius * cos((double) (clock_seconds * factorS))); // Same for Y of second hand |
scat6490 | 4:2013f3158cc6 | 55 | |
scat6490 | 4:2013f3158cc6 | 56 | TFT.fillcircle(circleX, circleY, circleRadius, Red); // Draws a red circle |
scat6490 | 5:f5fa116b57a1 | 57 | TFT.circle(circleX, circleY, circleRadius, White); // Draws a red circle |
scat6490 | 4:2013f3158cc6 | 58 | TFT.line(circleX, circleY, circleX + hourX, circleY - hourY, White); // Draws the hour hand |
scat6490 | 4:2013f3158cc6 | 59 | TFT.line(circleX, circleY, circleX + minuteX, circleY - minuteY, White); // Draws the minute hand |
scat6490 | 5:f5fa116b57a1 | 60 | TFT.line(circleX, circleY, circleX + secondX, circleY - secondY, Black); // Draws the minute hand |
scat6490 | 5:f5fa116b57a1 | 61 | |
scat6490 | 5:f5fa116b57a1 | 62 | for (int i = 0; i < 12; i++) |
scat6490 | 5:f5fa116b57a1 | 63 | { |
scat6490 | 5:f5fa116b57a1 | 64 | int timeX = floor(circleRadius * sin((double) (i * factorH))); // Calculate the X for the hour indicators around the edge |
scat6490 | 5:f5fa116b57a1 | 65 | int timeY = floor(circleRadius * cos((double) (i * factorH))); // Same for Y of the hour indicators |
scat6490 | 5:f5fa116b57a1 | 66 | |
scat6490 | 5:f5fa116b57a1 | 67 | TFT.line(circleX + 0.8 * timeX, circleY - 0.8 * timeY, circleX + timeX, circleY - timeY, White); // Draws the hour indicators |
scat6490 | 5:f5fa116b57a1 | 68 | } |
scat6490 | 1:a33f6ca5a5b3 | 69 | } |
scat6490 | 1:a33f6ca5a5b3 | 70 | |
scat6490 | 0:4fd6c8718206 | 71 | int main() |
scat6490 | 0:4fd6c8718206 | 72 | { |
scat6490 | 5:f5fa116b57a1 | 73 | wait(0.2); // Just wait for set up |
scat6490 | 3:6a328c7e9b1c | 74 | |
scat6490 | 5:f5fa116b57a1 | 75 | TFT.set_orientation(1); // Make the display horizontal |
scat6490 | 5:f5fa116b57a1 | 76 | TFT.background(Black); // Set background to black |
scat6490 | 5:f5fa116b57a1 | 77 | TFT.foreground(White); // Set text to white |
scat6490 | 5:f5fa116b57a1 | 78 | TFT.cls(); // Clear screen |
scat6490 | 0:4fd6c8718206 | 79 | |
scat6490 | 5:f5fa116b57a1 | 80 | TFT.set_font((unsigned char*) Arial12x12); // Set the font to Arial 12x12 |
scat6490 | 5:f5fa116b57a1 | 81 | TFT.fillrect(0, 0, 320, 32, Red); // Draw a red rectangle at the top |
scat6490 | 5:f5fa116b57a1 | 82 | TFT.locate(10, 10); // Move the text location |
scat6490 | 5:f5fa116b57a1 | 83 | TFT.printf("Clock by Duncan and Kieran"); // Write some text in the box at the top |
scat6490 | 0:4fd6c8718206 | 84 | |
scat6490 | 5:f5fa116b57a1 | 85 | TFT.background(Black); // Set background to black |
scat6490 | 5:f5fa116b57a1 | 86 | TFT.set_font((unsigned char*) Arial28x28); // Set the font to Arial 28x28 |
scat6490 | 0:4fd6c8718206 | 87 | |
scat6490 | 5:f5fa116b57a1 | 88 | timer.attach(&timeup, 1); // Set up the timer so it increments every second |
scat6490 | 1:a33f6ca5a5b3 | 89 | |
scat6490 | 3:6a328c7e9b1c | 90 | while(1) { |
scat6490 | 5:f5fa116b57a1 | 91 | if (input_time_change) // Check if the toggle switch is pressed to change which time the other buttons adjust (pressed changes the time, unpressed changes the alarm) |
scat6490 | 3:6a328c7e9b1c | 92 | { |
scat6490 | 5:f5fa116b57a1 | 93 | switch(input1){ // Check if the minute button is pressed |
scat6490 | 3:6a328c7e9b1c | 94 | case 1: |
scat6490 | 5:f5fa116b57a1 | 95 | clock_minutes++; // Increment the time minutes |
scat6490 | 5:f5fa116b57a1 | 96 | clock_minutes %= 60; // Set the minutes back to 0 if it's 60 |
scat6490 | 5:f5fa116b57a1 | 97 | wait(0.2); // Wait so the time doesn't increase too fast |
scat6490 | 5:f5fa116b57a1 | 98 | break; |
scat6490 | 3:6a328c7e9b1c | 99 | case 0: |
scat6490 | 5:f5fa116b57a1 | 100 | break; |
scat6490 | 3:6a328c7e9b1c | 101 | } |
scat6490 | 5:f5fa116b57a1 | 102 | switch(input60){ // Check if the hour button is pressed |
scat6490 | 3:6a328c7e9b1c | 103 | case 1: |
scat6490 | 5:f5fa116b57a1 | 104 | clock_hours++; // Increment the time hours |
scat6490 | 5:f5fa116b57a1 | 105 | clock_hours %= 24; // Set the hours back to 0 if it's 24 |
scat6490 | 5:f5fa116b57a1 | 106 | wait(0.2); // Wait so the time doesn't increase too fast |
scat6490 | 3:6a328c7e9b1c | 107 | break; |
scat6490 | 3:6a328c7e9b1c | 108 | case 0: |
scat6490 | 3:6a328c7e9b1c | 109 | break; |
scat6490 | 3:6a328c7e9b1c | 110 | } |
scat6490 | 3:6a328c7e9b1c | 111 | } else |
scat6490 | 3:6a328c7e9b1c | 112 | { |
scat6490 | 5:f5fa116b57a1 | 113 | switch(input1){ // Check if the minute button is pressed |
scat6490 | 3:6a328c7e9b1c | 114 | case 1: |
scat6490 | 5:f5fa116b57a1 | 115 | alarm_minutes++; // Increment the alarm minutes |
scat6490 | 5:f5fa116b57a1 | 116 | alarm_minutes %= 60; // Set the minutes back to 0 if it's 60 |
scat6490 | 5:f5fa116b57a1 | 117 | wait(0.2); // Wait so the alarm time doesn't increase too fast |
scat6490 | 3:6a328c7e9b1c | 118 | break; |
scat6490 | 3:6a328c7e9b1c | 119 | case 0: |
scat6490 | 3:6a328c7e9b1c | 120 | break; |
scat6490 | 3:6a328c7e9b1c | 121 | } |
scat6490 | 5:f5fa116b57a1 | 122 | switch(input60){ // Check if the hour button is pressed |
scat6490 | 3:6a328c7e9b1c | 123 | case 1: |
scat6490 | 5:f5fa116b57a1 | 124 | alarm_hours++; // Increment the alarm hours |
scat6490 | 5:f5fa116b57a1 | 125 | alarm_hours %= 24; // Set the hours back to 0 if it's 24 |
scat6490 | 5:f5fa116b57a1 | 126 | wait(0.2); // Wait so the alarm time doesn't increase too fast |
scat6490 | 3:6a328c7e9b1c | 127 | break; |
scat6490 | 3:6a328c7e9b1c | 128 | case 0: |
scat6490 | 3:6a328c7e9b1c | 129 | break; |
scat6490 | 3:6a328c7e9b1c | 130 | } |
scat6490 | 1:a33f6ca5a5b3 | 131 | } |
scat6490 | 1:a33f6ca5a5b3 | 132 | |
scat6490 | 5:f5fa116b57a1 | 133 | if (clock_hours == alarm_hours && clock_minutes == alarm_minutes && (clock_seconds % 2) == 0) // Check if the time is equal to the alarm time, and the seconds is even |
scat6490 | 2:21c4f254786f | 134 | { |
scat6490 | 5:f5fa116b57a1 | 135 | buzzer = 1; // Buzz |
scat6490 | 5:f5fa116b57a1 | 136 | |
scat6490 | 2:21c4f254786f | 137 | } else |
scat6490 | 2:21c4f254786f | 138 | { |
scat6490 | 5:f5fa116b57a1 | 139 | buzzer = 0; // Don't buzz |
scat6490 | 2:21c4f254786f | 140 | } |
scat6490 | 1:a33f6ca5a5b3 | 141 | |
scat6490 | 5:f5fa116b57a1 | 142 | TFT.locate(30, 70); // Move the text location |
scat6490 | 5:f5fa116b57a1 | 143 | TFT.printf("Time: %02d:%02d.%02d", clock_hours, clock_minutes, clock_seconds); // Write the time to the TFT |
scat6490 | 3:6a328c7e9b1c | 144 | |
scat6490 | 5:f5fa116b57a1 | 145 | TFT.locate(30, 120); // Move the text location |
scat6490 | 5:f5fa116b57a1 | 146 | TFT.printf("Alarm: %02d:%02d", alarm_hours, alarm_minutes); // Write the alarm time to the TFT |
scat6490 | 3:6a328c7e9b1c | 147 | |
scat6490 | 5:f5fa116b57a1 | 148 | drawclock(); // Call a function to draw the analogue clock |
scat6490 | 0:4fd6c8718206 | 149 | } |
scat6490 | 0:4fd6c8718206 | 150 | |
scat6490 | 0:4fd6c8718206 | 151 | } |