Components and supplies
Breadboard (generic)
Arduino Nano R3
Real Time Clock (RTC)
Nokia 5110 128 X 64 display
Jumper wires (generic)
Project description
Code
Analog_digital clock
arduino
1 2 3#include <SPI.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_PCD8544.h> 6#include <Wire.h> 7#include "RTClib.h" 8 9RTC_DS1307 rtc; 10 11 12// Software SPI (slower updates, more flexible pin options): 13// pin 8 - Serial clock out (SCLK) 14// pin 9 - Serial data out (DIN) 15// pin 10- Data/Command select (D/C) 16// pin 12- LCD chip select (CS) 17// pin 11- LCD reset (RST) 18Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 10, 12, 11); 19 20 21void setup() { 22 // put your setup code here, to run once: 23 display.begin(); 24 display.setContrast(50); 25 display.clearDisplay(); 26 display.display(); // show splashscreen 27 28 Serial.begin(9600); 29 if (! rtc.begin()) { 30 Serial.println("Couldn't find RTC"); 31 while (1); 32 } 33 34 if (! rtc.isrunning()) { 35 Serial.println("RTC is NOT running!"); 36 // following line sets the RTC to the date & time this sketch was compiled 37 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 38 39 } 40} 41 42void loop() { 43 DateTime now = rtc.now(); 44 int h=now.hour(); 45 int m=now.minute(); 46 int s=now.second(); 47 48 for(int i=0;i<10;i++) 49 { 50 drawClock(h,m,s); 51 delay(1000); 52 s++; 53 if(s>=60) m++; 54 s=s%60; 55 if(m>=60) h++; 56 m=m%60; 57 h=h%24; 58 } 59} 60 61void drawClock(int h, int m, int s) 62{ 63const int r=15; 64const double rot=-M_PI/2; 65double x,y,x0,y0,anglerad; 66 67 display.clearDisplay(); 68 //display.drawPixel(0,0,BLACK); 69 display.drawPixel(24,24,BLACK); 70 display.drawCircle(24,24,r,BLACK); 71 for(int i=0;i<12;i++) 72 { 73 int angled=360/12*i; 74 anglerad=2*M_PI/12*i+rot; 75 x=r*cos(anglerad); 76 y=r*sin(anglerad); 77 x0=(r-3)*cos(anglerad); 78 y0=(r-3)*sin(anglerad); 79 Serial.print(h); 80 Serial.print(":"); 81 Serial.print(m); 82 Serial.print(":"); 83 Serial.print(s); 84 85 Serial.println(""); 86 87 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 88 } 89 90 // hour 91 display.setCursor(19,1); 92 display.print("12"); 93 display.setCursor(2,20); 94 display.print("9"); 95 display.setCursor(43,20); 96 display.print("3"); 97 display.setCursor(21,41); 98 display.print("6"); 99 100 101 102 103 104 display.setCursor(54,4); 105 display.print("TIME"); 106 display.setCursor(54,14); 107 108 display.print(h); 109 display.print(":"); 110 display.print(m); 111 112 anglerad=2*M_PI/12*(h%12)+2*M_PI/12/60*m+rot; 113 x=(r-7)*cos(anglerad); 114 y=(r-7)*sin(anglerad); 115 x0=0; 116 y0=0; 117 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 118 119 // minute 120 anglerad=2*M_PI/60*m+rot; 121 x=(r-5)*cos(anglerad); 122 y=(r-5)*sin(anglerad); 123 x0=0; 124 y0=0; 125 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 126 127 // second 128 display.setCursor(63,22); 129 130 display.print(s); 131 132 anglerad=2*M_PI/60*s+rot; 133 x=(r-2)*cos(anglerad); 134 y=(r-2)*sin(anglerad); 135 x0=0; 136 y0=0; 137 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 138 139 140 display.display(); 141} 142 143
Analog_digital clock
arduino
1 2 3#include <SPI.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_PCD8544.h> 6#include 7 <Wire.h> 8#include "RTClib.h" 9 10RTC_DS1307 rtc; 11 12 13// Software 14 SPI (slower updates, more flexible pin options): 15// pin 8 - Serial clock out 16 (SCLK) 17// pin 9 - Serial data out (DIN) 18// pin 10- Data/Command select (D/C) 19// 20 pin 12- LCD chip select (CS) 21// pin 11- LCD reset (RST) 22Adafruit_PCD8544 display 23 = Adafruit_PCD8544(8, 9, 10, 12, 11); 24 25 26void setup() { 27 // put your 28 setup code here, to run once: 29 display.begin(); 30 display.setContrast(50); 31 32 display.clearDisplay(); 33 display.display(); // show splashscreen 34 35 Serial.begin(9600); 36 37 if (! rtc.begin()) { 38 Serial.println("Couldn't find RTC"); 39 while 40 (1); 41 } 42 43 if (! rtc.isrunning()) { 44 Serial.println("RTC is NOT 45 running!"); 46 // following line sets the RTC to the date & time this sketch 47 was compiled 48 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 49 50 } 51} 52 53void 54 loop() { 55 DateTime now = rtc.now(); 56 int h=now.hour(); 57 int m=now.minute(); 58 59 int s=now.second(); 60 61 for(int i=0;i<10;i++) 62 { 63 drawClock(h,m,s); 64 65 delay(1000); 66 s++; 67 if(s>=60) m++; 68 s=s%60; 69 if(m>=60) 70 h++; 71 m=m%60; 72 h=h%24; 73 } 74} 75 76void drawClock(int h, int 77 m, int s) 78{ 79const int r=15; 80const double rot=-M_PI/2; 81double x,y,x0,y0,anglerad; 82 83 84 display.clearDisplay(); 85 //display.drawPixel(0,0,BLACK); 86 display.drawPixel(24,24,BLACK); 87 88 display.drawCircle(24,24,r,BLACK); 89 for(int i=0;i<12;i++) 90 { 91 int 92 angled=360/12*i; 93 anglerad=2*M_PI/12*i+rot; 94 x=r*cos(anglerad); 95 96 y=r*sin(anglerad); 97 x0=(r-3)*cos(anglerad); 98 y0=(r-3)*sin(anglerad); 99 100 Serial.print(h); 101 Serial.print(":"); 102 Serial.print(m); 103 Serial.print(":"); 104 105 Serial.print(s); 106 107 Serial.println(""); 108 109 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 110 111 } 112 113 // hour 114 display.setCursor(19,1); 115 display.print("12"); 116 117 display.setCursor(2,20); 118 display.print("9"); 119 display.setCursor(43,20); 120 121 display.print("3"); 122 display.setCursor(21,41); 123 display.print("6"); 124 125 126 127 128 129 130 display.setCursor(54,4); 131 display.print("TIME"); 132 133 display.setCursor(54,14); 134 135 display.print(h); 136 display.print(":"); 137 138 display.print(m); 139 140 anglerad=2*M_PI/12*(h%12)+2*M_PI/12/60*m+rot; 141 142 x=(r-7)*cos(anglerad); 143 y=(r-7)*sin(anglerad); 144 x0=0; 145 y0=0; 146 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 147 148 149 // minute 150 anglerad=2*M_PI/60*m+rot; 151 x=(r-5)*cos(anglerad); 152 153 y=(r-5)*sin(anglerad); 154 x0=0; 155 y0=0; 156 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 157 158 159 // second 160 display.setCursor(63,22); 161 162 display.print(s); 163 164 165 anglerad=2*M_PI/60*s+rot; 166 x=(r-2)*cos(anglerad); 167 y=(r-2)*sin(anglerad); 168 169 x0=0; 170 y0=0; 171 display.drawLine(24+x0,24+y0,24+x,24+y,BLACK); 172 173 174 175 display.display(); 176} 177 178
Downloadable files
analog/digital clock using nokia 5110
analog/digital clock using nokia 5110
Comments
Only logged in users can leave comments
garysat
0 Followers
•0 Projects
+1
Work attribution
Table of contents
Intro
8
0