Components and supplies
Arduino Nano ESP32
Apps and platforms
arduino IDE cloud
Project description
Code
SlidingTileClock
1/* 2 WiFi connected Sliding Tile Clock. 3 This sketch gets current time from NTP server. 4 On start or reset all sliding tiles should be zero 00:00 5*/ 6 7#include <Arduino.h> 8#include <WiFi.h> 9#include <WiFiUdp.h> 10#include <Time.h> 11#include <NTP.h> 12 13// NTP settings 14 15char ssid[] = "network"; // Your network SSID name here 16char password[] = "password"; // Your network password here 17 18 19WiFiUDP wifiUdp; 20NTP ntp(wifiUdp); 21 22const char *ntpServer = "0.nl.pool.ntp.org"; 23 24 25// Stepper setings 26int delaytime = 2; // wait for a single step of stepper 27 28// ports used to control the stepper motor 29// if your motor rotate to the opposite direction, 30// change the order as {4, 3, 2, 1}; 31int hourunitdigitmotor[4] = {10,11,12,13}; // minute motor units 32int minutetenthdigitmotor[4] = {6,7,8,9}; // minute motor tenth 33int minuteunitdigitmotor[4] = {2,3,4,5}; // hour motor units 34int hourtenthdigitmotor[4] = {A0,A1,A2,A3}; // hour motor tenth 35 36// sequence of stepper motor control 37int seq[8][4] = { 38 { LOW, HIGH, HIGH, LOW}, 39 { LOW, LOW, HIGH, LOW}, 40 { LOW, LOW, HIGH, HIGH}, 41 { LOW, LOW, LOW, HIGH}, 42 { HIGH, LOW, LOW, HIGH}, 43 { HIGH, LOW, LOW, LOW}, 44 { HIGH, HIGH, LOW, LOW}, 45 { LOW, HIGH, LOW, LOW} 46}; 47 48// number steps to turn one Tile 49const int HALFSTEPS = 2048; 50 51int actualMinuteTileUnit = 0; 52int actualMinuteTileTenth = 0; 53int actualHourTileUnit = 0; 54int actualHourTileTenth = 0; 55 56int newMinuteTileUnit = 0; 57int newMinuteTileTenth = 0; 58int newHourTileUnit = 0; 59int newHourTileTenth = 0; 60 61// functions 62 63 64void rotate(int step, int motorport[4]) { 65 static int phase = 0; 66 int i, j; 67 int delta = (step > 0) ? 1 : 7; 68 69 step = (step > 0) ? step : -step; 70 for(j = 0; j < step; j++) { 71 phase = (phase + delta) % 8; 72 for(i = 0; i < 4; i++) { 73 digitalWrite(motorport[i], seq[phase][i]); 74 } 75 delay(delaytime); 76 } 77 // power cut 78 for(i = 0; i < 4; i++) { 79 digitalWrite(motorport[i], LOW); 80 } 81} 82 83void rotateTile(int steps, int motorport[4]) { 84 for (int i = 0; i < steps; i++) { 85 rotate(HALFSTEPS, motorport); 86 } 87} 88 89void setup() { 90 Serial.begin(9600); 91 WiFi.begin(ssid, password); 92 while (WiFi.status() != WL_CONNECTED) { 93 Serial.println("Connecting ..."); 94 delay(500); 95 } 96 Serial.println("Connected"); 97 ntp.updateInterval(60000); // update every minute 98 ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timezone +120min (+1 GMT + 1h summertime offset) 99 ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT) 100 ntp.begin(ntpServer); 101 Serial.println("start NTP"); 102 delay (500); 103 104 pinMode(hourunitdigitmotor[0], OUTPUT); 105 pinMode(hourunitdigitmotor[1], OUTPUT); 106 pinMode(hourunitdigitmotor[2], OUTPUT); 107 pinMode(hourunitdigitmotor[3], OUTPUT); 108 pinMode(minutetenthdigitmotor[0], OUTPUT); 109 pinMode(minutetenthdigitmotor[1], OUTPUT); 110 pinMode(minutetenthdigitmotor[2], OUTPUT); 111 pinMode(minutetenthdigitmotor[3], OUTPUT); 112 pinMode(minuteunitdigitmotor[0], OUTPUT); 113 pinMode(minuteunitdigitmotor[1], OUTPUT); 114 pinMode(minuteunitdigitmotor[2], OUTPUT); 115 pinMode(minuteunitdigitmotor[3], OUTPUT); 116 pinMode(hourtenthdigitmotor[0], OUTPUT); 117 pinMode(hourtenthdigitmotor[1], OUTPUT); 118 pinMode(hourtenthdigitmotor[2], OUTPUT); 119 pinMode(hourtenthdigitmotor[3], OUTPUT); 120 digitalWrite(A0, LOW); 121 digitalWrite(A1, LOW); 122 digitalWrite(A2, LOW); 123 digitalWrite(A3, LOW); 124 } 125 126void loop() { 127 ntp.update(); 128 Serial.println(ntp.formattedTime("%d. %B %Y")); // dd. Mmm yyyy 129 Serial.println(ntp.formattedTime("%A %T")); // Www hh:mm:ss 130 131 if (ntp.hours()!=hour() || ntp.minutes()!=minute()) { 132 setTime(ntp.hours(),ntp.minutes(),ntp.seconds(),ntp.day(),ntp.month(),ntp.year());}; // synchroniseer clock when ther is a time diverence 133 134 newMinuteTileUnit = minute()%10; 135 newMinuteTileTenth = minute()/10; 136 newHourTileUnit = hour()%10; 137 newHourTileTenth = hour()/10; 138 139 Serial.print("New hours : "); Serial.print(newHourTileTenth); Serial.println(newHourTileUnit); 140 Serial.print("New minutes: "); Serial.print(newMinuteTileTenth); Serial.println(newMinuteTileUnit); 141 142 //set minute unit tile 143 if (newMinuteTileUnit > actualMinuteTileUnit) { 144 rotateTile((newMinuteTileUnit-actualMinuteTileUnit), hourunitdigitmotor); 145 } 146 else if (newMinuteTileUnit < actualMinuteTileUnit){ 147 rotateTile((newMinuteTileUnit + 10 - actualMinuteTileUnit), hourunitdigitmotor); 148 } 149 150 //set minute tenth tile 151 if (newMinuteTileTenth > actualMinuteTileTenth) { 152 rotateTile((newMinuteTileTenth - actualMinuteTileTenth), minutetenthdigitmotor); 153 } 154 else if (newMinuteTileTenth < actualMinuteTileTenth) { 155 rotateTile((newMinuteTileTenth + 6 - actualMinuteTileTenth), minutetenthdigitmotor); 156 } 157 158 //set hour unit tile 159 if (newHourTileUnit > actualHourTileUnit){ 160 rotateTile((newHourTileUnit-actualHourTileUnit), minuteunitdigitmotor); 161 } 162 else if (newHourTileUnit < actualHourTileUnit){ 163 rotateTile((newHourTileUnit + 10 - actualHourTileUnit), minuteunitdigitmotor); 164 } 165 166 //set hour tenth tile 167 if (newHourTileTenth > actualHourTileTenth) { 168 rotateTile((newHourTileTenth - actualHourTileTenth), hourtenthdigitmotor); 169 } 170 else if (newHourTileTenth < actualHourTileTenth) { 171 rotateTile((newHourTileTenth + 3 - actualHourTileTenth), hourtenthdigitmotor); 172 } 173 174 actualMinuteTileUnit = newMinuteTileUnit; 175 actualMinuteTileTenth = newMinuteTileTenth; 176 actualHourTileUnit = newHourTileUnit; 177 actualHourTileTenth = newHourTileTenth; 178 179 delay(1000); 180 }
Documentation
Instructables
https://www.instructables.com/Sliding-Tile-Clock/
Comments
Only logged in users can leave comments