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

http://www.imgur.com/SiyOshx.jpg

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-KL25ZMI0283QT-9A
P3V33.3V, IM1, IM2, IM3
GNDRD, IM0, LEDK
PTD5CS
PTD1RS
PTD0RST
PTD2SDI
PTD3SDO
PTA13WR
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-KL25ZInput
PTA5Minute button
PTC8Hour button
PTC9Time 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)
Committer:
scat6490
Date:
Wed May 24 13:50:26 2017 +0000
Revision:
4:2013f3158cc6
Parent:
3:6a328c7e9b1c
Child:
5:f5fa116b57a1
Display, clock, alarm, time changing, Wednesday 2:50pm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scat6490 0:4fd6c8718206 1
scat6490 0:4fd6c8718206 2 #include "stdio.h"
scat6490 0:4fd6c8718206 3 #include "mbed.h"
scat6490 0:4fd6c8718206 4 #include "SPI_TFT_ILI9341.h"
scat6490 0:4fd6c8718206 5 #include "string"
scat6490 0:4fd6c8718206 6 #include "Arial12x12.h"
scat6490 0:4fd6c8718206 7 #include "Arial24x23.h"
scat6490 0:4fd6c8718206 8 #include "Arial28x28.h"
scat6490 0:4fd6c8718206 9 #include "font_big.h"
scat6490 0:4fd6c8718206 10
scat6490 1:a33f6ca5a5b3 11 Ticker timer;
scat6490 2:21c4f254786f 12 DigitalOut buzzer(PTA12);
scat6490 4:2013f3158cc6 13 DigitalIn input1(PTA4);
scat6490 4:2013f3158cc6 14 DigitalIn input60(PTC8);
scat6490 4:2013f3158cc6 15 DigitalIn input_time_change(PTC9);
scat6490 1:a33f6ca5a5b3 16 Serial pc(USBTX,USBRX);
scat6490 0:4fd6c8718206 17 SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
scat6490 0:4fd6c8718206 18
scat6490 3:6a328c7e9b1c 19 int clock_minutes, clock_hours = 0;
scat6490 4:2013f3158cc6 20 int clock_seconds = 0;
scat6490 3:6a328c7e9b1c 21 int alarm_hours = 7;
scat6490 3:6a328c7e9b1c 22 int alarm_minutes = 0;
scat6490 1:a33f6ca5a5b3 23
scat6490 1:a33f6ca5a5b3 24 void timeup(){
scat6490 4:2013f3158cc6 25 clock_seconds++;
scat6490 4:2013f3158cc6 26 clock_seconds %= 60;
scat6490 4:2013f3158cc6 27
scat6490 4:2013f3158cc6 28 if (clock_seconds==0)
scat6490 4:2013f3158cc6 29 {
scat6490 4:2013f3158cc6 30 clock_minutes++;
scat6490 4:2013f3158cc6 31 clock_minutes %= 60;
scat6490 4:2013f3158cc6 32 }
scat6490 4:2013f3158cc6 33 if (clock_minutes==0 && clock_seconds==0)
scat6490 4:2013f3158cc6 34 {
scat6490 4:2013f3158cc6 35 clock_hours++;
scat6490 4:2013f3158cc6 36 clock_hours %= 24;
scat6490 4:2013f3158cc6 37 }
scat6490 4:2013f3158cc6 38 }
scat6490 4:2013f3158cc6 39
scat6490 4:2013f3158cc6 40 // TODO: Add %02d to time ouputs, try font_big.h, comment every line, make sure the drawclock function works, fix the input_time_change and add seconds/precise timing
scat6490 4:2013f3158cc6 41 void drawclock() // Draws an analogue clock using the clock_minutes and clock_hours variables
scat6490 4:2013f3158cc6 42 {
scat6490 4:2013f3158cc6 43 int hourX, hourY, minuteX, minuteY; // Initialise variables to be used
scat6490 4:2013f3158cc6 44 int circleX = 200; // Circle centre X
scat6490 4:2013f3158cc6 45 int circleY = 200; // Circle centre Y
scat6490 4:2013f3158cc6 46 int circleRadius = 30; // Circle radius
scat6490 4:2013f3158cc6 47
scat6490 4:2013f3158cc6 48 double factorH = (2 * 3.141) / 12; // Convert hour to radians
scat6490 4:2013f3158cc6 49 double factorM = (2 * 3.141) / 60; // Convert minute to radians
scat6490 4:2013f3158cc6 50
scat6490 4:2013f3158cc6 51 hourX = floor(0.9 * circleRadius * sin((double) (clock_hours * factorH))); // Calculate difference in X values of the two ends of the hour hand.
scat6490 4:2013f3158cc6 52 hourY = floor(0.9 * circleRadius * cos((double) (clock_hours * factorH))); // Same for Y of hour hand. The 0.9 means the hands are smaller than the circle
scat6490 4:2013f3158cc6 53 minuteX = floor(0.9 * circleRadius * sin((double) (clock_minutes * factorM))); // Same for X of minute hand
scat6490 4:2013f3158cc6 54 minuteY = floor(0.9 * circleRadius * cos((double) (clock_minutes * factorM))); // Same for Y of minute hand
scat6490 4:2013f3158cc6 55
scat6490 4:2013f3158cc6 56 TFT.fillcircle(circleX, circleY, circleRadius, Red); // Draws a red circle
scat6490 4:2013f3158cc6 57 TFT.line(circleX, circleY, circleX + hourX, circleY - hourY, White); // Draws the hour hand
scat6490 4:2013f3158cc6 58 TFT.line(circleX, circleY, circleX + minuteX, circleY - minuteY, White); // Draws the minute hand
scat6490 1:a33f6ca5a5b3 59 }
scat6490 1:a33f6ca5a5b3 60
scat6490 0:4fd6c8718206 61 int main()
scat6490 0:4fd6c8718206 62 {
scat6490 0:4fd6c8718206 63 //pc.baud(115200);
scat6490 0:4fd6c8718206 64 wait(0.2);
scat6490 3:6a328c7e9b1c 65
scat6490 0:4fd6c8718206 66 TFT.set_orientation(1); // Make it horizontal
scat6490 4:2013f3158cc6 67 TFT.background(Black); // Set background to black
scat6490 0:4fd6c8718206 68 TFT.foreground(White); // Set text to white
scat6490 0:4fd6c8718206 69 TFT.cls(); // Clear screen
scat6490 0:4fd6c8718206 70 TFT.set_font((unsigned char*) Arial12x12); // Set the font
scat6490 0:4fd6c8718206 71
scat6490 0:4fd6c8718206 72 TFT.fillrect(0, 0, 320, 30, Red);
scat6490 4:2013f3158cc6 73 TFT.locate(5, 5);
scat6490 0:4fd6c8718206 74 TFT.printf("Clock by Duncan and Kieran");
scat6490 0:4fd6c8718206 75
scat6490 0:4fd6c8718206 76 wait(0.1);
scat6490 0:4fd6c8718206 77
scat6490 0:4fd6c8718206 78 TFT.background(Black); // Set background to black
scat6490 0:4fd6c8718206 79 TFT.set_font((unsigned char*) Arial28x28); // Set the font
scat6490 1:a33f6ca5a5b3 80
scat6490 1:a33f6ca5a5b3 81 timer.attach(&timeup, 1);
scat6490 1:a33f6ca5a5b3 82
scat6490 3:6a328c7e9b1c 83 while(1) {
scat6490 4:2013f3158cc6 84 if (input_time_change)
scat6490 3:6a328c7e9b1c 85 {
scat6490 3:6a328c7e9b1c 86 switch(input1){
scat6490 3:6a328c7e9b1c 87 case 1:
scat6490 3:6a328c7e9b1c 88 clock_minutes = (clock_minutes++) % 60;
scat6490 3:6a328c7e9b1c 89 wait(0.2);
scat6490 3:6a328c7e9b1c 90 break;
scat6490 3:6a328c7e9b1c 91 case 0:
scat6490 3:6a328c7e9b1c 92 break;
scat6490 3:6a328c7e9b1c 93 }
scat6490 3:6a328c7e9b1c 94 switch(input60){
scat6490 3:6a328c7e9b1c 95 case 1:
scat6490 3:6a328c7e9b1c 96 clock_hours = clock_hours++;
scat6490 3:6a328c7e9b1c 97 wait(0.2);
scat6490 3:6a328c7e9b1c 98 break;
scat6490 3:6a328c7e9b1c 99 case 0:
scat6490 3:6a328c7e9b1c 100 break;
scat6490 3:6a328c7e9b1c 101 }
scat6490 3:6a328c7e9b1c 102 } else
scat6490 3:6a328c7e9b1c 103 {
scat6490 3:6a328c7e9b1c 104 switch(input1){
scat6490 3:6a328c7e9b1c 105 case 1:
scat6490 3:6a328c7e9b1c 106 alarm_minutes = (alarm_minutes++) % 60;
scat6490 3:6a328c7e9b1c 107 wait(0.2);
scat6490 3:6a328c7e9b1c 108 break;
scat6490 3:6a328c7e9b1c 109 case 0:
scat6490 3:6a328c7e9b1c 110 break;
scat6490 3:6a328c7e9b1c 111 }
scat6490 3:6a328c7e9b1c 112 switch(input60){
scat6490 3:6a328c7e9b1c 113 case 1:
scat6490 3:6a328c7e9b1c 114 alarm_hours = alarm_hours++;
scat6490 3:6a328c7e9b1c 115 wait(0.2);
scat6490 3:6a328c7e9b1c 116 break;
scat6490 3:6a328c7e9b1c 117 case 0:
scat6490 3:6a328c7e9b1c 118 break;
scat6490 3:6a328c7e9b1c 119 }
scat6490 1:a33f6ca5a5b3 120 }
scat6490 1:a33f6ca5a5b3 121
scat6490 3:6a328c7e9b1c 122 if (clock_hours == alarm_hours && clock_minutes == alarm_minutes)
scat6490 2:21c4f254786f 123 {
scat6490 2:21c4f254786f 124 buzzer = 1;
scat6490 2:21c4f254786f 125 } else
scat6490 2:21c4f254786f 126 {
scat6490 2:21c4f254786f 127 buzzer = 0;
scat6490 2:21c4f254786f 128 }
scat6490 1:a33f6ca5a5b3 129
scat6490 3:6a328c7e9b1c 130 clock_hours %= 24;
scat6490 3:6a328c7e9b1c 131 alarm_hours %= 24;
scat6490 3:6a328c7e9b1c 132
scat6490 1:a33f6ca5a5b3 133 TFT.locate(50, 50);
scat6490 4:2013f3158cc6 134 TFT.printf("Time: %02d:%02d.%02d", clock_hours, clock_minutes, clock_seconds);
scat6490 3:6a328c7e9b1c 135
scat6490 3:6a328c7e9b1c 136 TFT.locate(50, 100);
scat6490 4:2013f3158cc6 137 TFT.printf("Alarm: %02d:%02d", alarm_hours, alarm_minutes);
scat6490 3:6a328c7e9b1c 138
scat6490 3:6a328c7e9b1c 139 TFT.locate(50, 150);
scat6490 3:6a328c7e9b1c 140 TFT.printf("%d ", (int) input_time_change);
scat6490 4:2013f3158cc6 141
scat6490 4:2013f3158cc6 142 drawclock();
scat6490 0:4fd6c8718206 143 }
scat6490 0:4fd6c8718206 144
scat6490 0:4fd6c8718206 145 }