TLDR:
Have gotten digital alarm clock to work but having troubles with attaching and programming 2 SG90 servo's for hours and minutes display.
I am unsure of how I can accurately describe this, so I have attached a couple of videos
Hey all. I am more skilled in 3d design than I am with programming and I am looking for advice/assistance from anyone with insightful knowledge.
So far I have managed to make a digital alarm clock.
I am using:
- a DS1307 rtc
- a 16x2 LCD
- an assortment of buttons
I am now trying to add 2 SG90 servos, 1 for hours and 1 for minutes.
Trouble is, my servos seem to initialize but do not actually move to the times on the RTC (which the arduino has displayed on the LCD).
Here is my code:
Shortened code: (where I suspect the issue is)
void loop () {
DateTime now = rtc.now();
if((digitalRead(P1)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = 86400; // Move time ahead one day (maybe)
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P1)==HIGH)
{
uint32_t deltaT = 3600; // Move time ahead one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P2)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = -86400; // Move time back one day (maybe)
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P2)==HIGH)
{
uint32_t deltaT = -3600; // Move time back one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P3)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = 1; // Move time ahead one second
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P3)==HIGH)
{
uint32_t deltaT = 60; // Move time ahead one minute
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P4)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = -1; // Move time back one second
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P4)==HIGH)
{
uint32_t deltaT = -60; // Move time back one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print(now.day(), DEC);
lcd.print(":");
lcd.print(now.month(), DEC);
lcd.print(":");
lcd.print(now.year(), DEC);
delay(250);
lcd.clear();
lcd.setCursor(0, 0);
hoursdegree = 180-(now.hour()*7.5);
hours.write(hoursdegree);
minutesdegree = 180-(now.minute()*3);
minutes.write(minutesdegree);
//clockfaceHands();
}
void clockfaceHands(){
DateTime now = rtc.now();
hoursdegree = (180-(now.hour()*7.5));
minutesdegree = (180-(now.minute()*3));
minutes.write(minutesdegree);
hours.write(hoursdegree); // tell servo to go to position in variable
}
Full code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <Servo.h>
RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//RTC clock - Matthew Lim
//references
//*OLD* library info https://github.com/davidhbrown/RealTimeClockDS1307/blob/master/RealTimeClockDS1307.h ***ditched this library as it was missing features moved to RTCLIB
//RTCLIB library info https://github.com/adafruit/RTClib/blob/master/RTClib.h
//bare source 1: https://www.instructables.com/Arduino-Real-Time-Clock-DS1307/
//interesting idea to move to different library: https://arduino.stackexchange.com/questions/3354/why-is-my-real-time-clock-getting-the-wrong-time-from-my-pc
//https://www.arduino.cc/en/Reference/LiquidCrystal LCD library
//realtime updating rtc time https://forum.arduino.cc/index.php?topic=555628.0
// concept of adding double action buttons https://forum.arduino.cc/index.php?topic=512477.0
//buttons
int PM=6; //button modifier (using pin 6) - will try to make the other buttons change the days and the seconds when held down.
int P1=7; //button + hours (using pin 7)
int P2=8; //button - hours (using pin 8)
int P3=9; //button + minutes (using pin 9)
int P4=10; //button - minutes (using pin 10)
//servos
Servo hours; // hours is using pin 1
Servo minutes; // minutes is using pin 0
int hoursdegree = 0;
int minutesdegree = 0;
void setup () {
Serial.begin(57600);
lcd.begin(16,2);
pinMode(A3, OUTPUT);
digitalWrite(A3, HIGH);
pinMode(A2, OUTPUT);
digitalWrite(A2, LOW);
pinMode(PM, INPUT);
pinMode(P1, INPUT); //allows program to recognise that we are using buttons
pinMode(P2, INPUT);
pinMode(P3, INPUT);
pinMode(P4, INPUT);
minutes.attach(0);
hours.attach(1);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
if((digitalRead(P1)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = 86400; // Move time ahead one day (maybe)
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P1)==HIGH)
{
uint32_t deltaT = 3600; // Move time ahead one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P2)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = -86400; // Move time back one day (maybe)
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P2)==HIGH)
{
uint32_t deltaT = -3600; // Move time back one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P3)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = 1; // Move time ahead one second
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P3)==HIGH)
{
uint32_t deltaT = 60; // Move time ahead one minute
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
if((digitalRead(P4)==HIGH)&&(digitalRead(PM)==HIGH))
{
uint32_t deltaT = -1; // Move time back one second
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
else{
if(digitalRead(P4)==HIGH)
{
uint32_t deltaT = -60; // Move time back one hour
rtc.adjust(rtc.now() + TimeSpan(deltaT));
}
}
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print(now.day(), DEC);
lcd.print(":");
lcd.print(now.month(), DEC);
lcd.print(":");
lcd.print(now.year(), DEC);
delay(250);
lcd.clear();
lcd.setCursor(0, 0);
hoursdegree = 180-(now.hour()*7.5);
hours.write(hoursdegree);
minutesdegree = 180-(now.minute()*3);
minutes.write(minutesdegree);
//clockfaceHands();
}
void clockfaceHands(){
DateTime now = rtc.now();
hoursdegree = (180-(now.hour()*7.5));
minutesdegree = (180-(now.minute()*3));
minutes.write(minutesdegree);
hours.write(hoursdegree); // tell servo to go to position in variable
}
Here are some videos of the problem(s) I am experiencing:
Video 1:
Caption: Sweep running with one variable between the two servos.
Video 2:
Caption: The program running (with the code pasted above)
Any help would be appreciated.
Thank you - Matt