Components and supplies
5 V DC power supply
Real Time Clock (RTC)
Breadboard (generic)
Jumper wires (generic)
Arduino Nano R3
Misc parts for assembly.
SG90 Micro-servo motor
Project description
Code
Servodigit
arduino
Arduino code file
1#include <SPI.h> 2 3#include <Wire.h> 4#include "RTClib.h" 5#include <Servo.h> 6 7Servo myservo1; // single minute servo connects to arduino pin 9 8Servo myservo2; // tem minute servo connects to arduino pin 8 9Servo myservo3; // hour servo connects to arduino pin 7 10 11int pos1 = 0; 12int pos2 = 0; 13int pos3 = 0; 14 15RTC_DS1307 rtc; 16 17 18 19 20 21void setup() { 22 // put your setup code here, to run once: 23 myservo1.attach(9); 24 myservo2.attach(8); 25 myservo3.attach(7); 26 27 Serial.begin(9600); 28 if (! rtc.begin()) { 29 Serial.println("Couldn't find RTC"); 30 while (1); 31 } 32 33 if (! rtc.isrunning()) { 34 Serial.println("RTC is NOT running!"); 35 // following line sets the RTC to the date & time this sketch was compiled 36 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 37 38 } 39} 40 41void loop() { 42 DateTime now = rtc.now(); 43 44 int h=now.hour(); 45 int m=now.minute(); 46 int nm; // variable used to convert to minute single digit 47 48 // following line converts from 24 hour to 12 hour 49 if (h>12) { 50 h=h-12; 51 } 52 53 Serial.print(h); 54 // following lines position the hour disc 55 if(h==1){ 56 pos3=132; 57 } 58 if (h==2){ 59 pos3=120; 60 } 61 if (h==3){ 62 pos3=108; 63 } 64 if (h==4){ 65 pos3=96; 66 } 67 if (h==5){ 68 pos3=84; 69 } 70 if (h==6){ 71 pos3=72; 72 } 73 if (h==7){ 74 pos3=60; 75 } 76 if (h==8){ 77 pos3=48; 78 } 79 if (h==9){ 80 pos3=36; 81 } 82 if (h==10){ 83 pos3=24; 84 } 85 if (h==11){ 86 pos3=12; 87 } 88 if (h==12){ 89 pos3=0; 90 } 91 92 93 94 95 96 Serial.print(":"); 97 Serial.print(m); 98 //following lines position the minutes discs 99 if (m<10){ 100 nm=(m); 101 pos2=120; 102 } 103 if (m>9){ 104 nm=(m-10); 105 pos2=108; 106 } 107 if (m>19){ 108 nm=(m-20); 109 pos2=96; 110 } 111 if (m>29){ 112 nm=(m-30); 113 pos2=84; 114 } 115 if (m>39){ 116 nm=(m-40); 117 pos2=72; 118 } 119 if (m>49){ 120 nm=(m-50); 121 pos2=60; 122 } 123 124 125 Serial.println(); 126 Serial.print(nm); 127 pos1=(nm * 12); // this line determines the servo 1 movement 128 myservo1.write(pos1); 129 myservo2.write(pos2); 130 myservo3.write(pos3); 131 132 Serial.println(); 133 Serial.println(); 134 Serial.print(pos1); 135 Serial.println(); 136 delay (1000); 137 138} 139 140
Servodigit
arduino
Arduino code file
1#include <SPI.h> 2 3#include <Wire.h> 4#include "RTClib.h" 5#include <Servo.h> 6 7Servo myservo1; // single minute servo connects to arduino pin 9 8Servo myservo2; // tem minute servo connects to arduino pin 8 9Servo myservo3; // hour servo connects to arduino pin 7 10 11int pos1 = 0; 12int pos2 = 0; 13int pos3 = 0; 14 15RTC_DS1307 rtc; 16 17 18 19 20 21void setup() { 22 // put your setup code here, to run once: 23 myservo1.attach(9); 24 myservo2.attach(8); 25 myservo3.attach(7); 26 27 Serial.begin(9600); 28 if (! rtc.begin()) { 29 Serial.println("Couldn't find RTC"); 30 while (1); 31 } 32 33 if (! rtc.isrunning()) { 34 Serial.println("RTC is NOT running!"); 35 // following line sets the RTC to the date & time this sketch was compiled 36 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 37 38 } 39} 40 41void loop() { 42 DateTime now = rtc.now(); 43 44 int h=now.hour(); 45 int m=now.minute(); 46 int nm; // variable used to convert to minute single digit 47 48 // following line converts from 24 hour to 12 hour 49 if (h>12) { 50 h=h-12; 51 } 52 53 Serial.print(h); 54 // following lines position the hour disc 55 if(h==1){ 56 pos3=132; 57 } 58 if (h==2){ 59 pos3=120; 60 } 61 if (h==3){ 62 pos3=108; 63 } 64 if (h==4){ 65 pos3=96; 66 } 67 if (h==5){ 68 pos3=84; 69 } 70 if (h==6){ 71 pos3=72; 72 } 73 if (h==7){ 74 pos3=60; 75 } 76 if (h==8){ 77 pos3=48; 78 } 79 if (h==9){ 80 pos3=36; 81 } 82 if (h==10){ 83 pos3=24; 84 } 85 if (h==11){ 86 pos3=12; 87 } 88 if (h==12){ 89 pos3=0; 90 } 91 92 93 94 95 96 Serial.print(":"); 97 Serial.print(m); 98 //following lines position the minutes discs 99 if (m<10){ 100 nm=(m); 101 pos2=120; 102 } 103 if (m>9){ 104 nm=(m-10); 105 pos2=108; 106 } 107 if (m>19){ 108 nm=(m-20); 109 pos2=96; 110 } 111 if (m>29){ 112 nm=(m-30); 113 pos2=84; 114 } 115 if (m>39){ 116 nm=(m-40); 117 pos2=72; 118 } 119 if (m>49){ 120 nm=(m-50); 121 pos2=60; 122 } 123 124 125 Serial.println(); 126 Serial.print(nm); 127 pos1=(nm * 12); // this line determines the servo 1 movement 128 myservo1.write(pos1); 129 myservo2.write(pos2); 130 myservo3.write(pos3); 131 132 Serial.println(); 133 Serial.println(); 134 Serial.print(pos1); 135 Serial.println(); 136 delay (1000); 137 138} 139 140
Downloadable files
servodigit
diagram
servodigit
Comments
Only logged in users can leave comments