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)
Diff: main.cpp
- Revision:
- 7:007d23438920
- Parent:
- 6:d80627e78bfd
- Child:
- 8:6bd70a8697f4
--- a/main.cpp Fri May 26 09:15:06 2017 +0000 +++ b/main.cpp Fri May 26 09:48:57 2017 +0000 @@ -6,6 +6,9 @@ #include "Arial24x23.h" #include "Arial28x28.h" #include "font_big.h" +#include "MMA8451Q.h" + +#define MMA8451_I2C_ADDRESS (0x1d<<1) Ticker timer; // Set up a timer DigitalOut buzzer(PTE30); // Set up inputs and outputs @@ -18,6 +21,7 @@ int clock_minutes, clock_hours, clock_seconds = 0; // Initialise clock at 00:00.00 int alarm_hours = 7; // Initialise alarm hours as 07 int alarm_minutes = 0; // Initialise alarm minutes as 00 so alarm is 07:00 +int previous_state; // Initialise the previous state variable to detect changing rotation void timeup(){ // Function to increment the clock every second clock_seconds++; // Increment the time @@ -70,7 +74,9 @@ int main() { - wait(0.2); // Just wait for set up + MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); // Set up the accelerometer connection + wait(0.5); // Just wait for set up + previous_state = acc.getAccX() >= 0.5; // Initialise the previous state variable TFT.set_orientation(3); // Make the display horizontal TFT.background(Black); // Set background to black @@ -129,8 +135,18 @@ break; } } - - 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 + + float x = acc.getAccX(); // Check the x-orientation + int enable = 1; // Initialise the enable variable + + if (x < 0.5){ // Check if the clock is tilted + enable = 0; // Disable the alarm + } + if (enable != previous_state && !enable && clock_hours == alarm_hours && clock_minutes == alarm_minutes) // If the clock is tilted while the alarm is going off + { + alarm_minutes += 5; // Add 5 minutes (snooze) + } + if (clock_hours == alarm_hours && clock_minutes == alarm_minutes && (clock_seconds % 2) == 0 && enable) // Check if the time is equal to the alarm time, and the seconds is even { buzzer = 1; // Buzz @@ -142,11 +158,17 @@ TFT.foreground(White); // Set text to white TFT.locate(30, 70); // Move the text location TFT.printf("Time: %02d:%02d.%02d", clock_hours, clock_minutes, clock_seconds); // Write the time to the TFT - + + if (enable){ // Check if the alarm is enabled + TFT.foreground(Green); // Write the alarm text in green + } else { + TFT.foreground(Red); // Write the alarm text in red + } TFT.locate(30, 120); // Move the text location TFT.printf("Alarm: %02d:%02d", alarm_hours, alarm_minutes); // Write the alarm time to the TFT drawclock(); // Call a function to draw the analogue clock + previous_state = enable; // Update the state to changes can be detected } }