I need help creating a simple code with a ds3231 to only run my program during between 8am and 9pm i've been trying with alarms and haven't been able to make anything.
- Show us your attempts, also show us good images of the actual circuit wiring.
A common problem.
Read here
a7
I think i've finally got code that will work i'll post it below, if youd still like to see the wiring i can show you but its messy lol
//welcome to the original code for the ET405 display case project, if you edit this code in any way,
//please update the comments to reflect the changes you have made to the system.
// -Thank you, Gabe Bush ET405 SP25.
//GENERAL WARNINGS FOR CODE UPDATES AND POWERING THE ARDUINO.
// DO NOT UNDER ANY CIRCUMSTANCES EVER REVERSE THE POLARITY OF THE 5V INPUT POWER AND GROUND,
// DOING SO WILL DAMAGE THE POWER CIRCUIT AND POSSIBLY THE ENTIRE ARDUINO IN A SPLIT SECOND.
// IF UPLOADING NEW CODE DISCONNECT 5V POWER AND GROUND FROM THE ARDUINO, AND THE MAIN POWER TO DC POWER SUPPLY,
// IF POWER AND GROUND ARE LEFT CONNECTED DAMAGE TO THE BOARD MAY OCCUR.
//MAKE SURE THE MB102 POWERSUPPLY POWER SWITCH IS ALWAYS ON, POWERING THE SYSTEM WITH IT OFF WILL DAMAGE COMPONENTS
// 7-20v VIN and barrel jack input will not work, powering circuit will cause the amber lcd to blink rapidly,
// and the lcd screen and motors will not run.
//DO NOT PLUG IN THE USB CONNECTOR TO UPLOAD CODE WHILE POWER IS CONNECTED IT WILL DAMAGE THE ARDUINO
//IF LOOK THROUGH SERIAL MONITOR MAKE SURE BAUD IS SET TO 9600, AND THE RTC IS POWERED, DO NOT RUN IT FROM THE POWERSUPPLY,
//INSTEAD RUN IT FROM THE ARDUINOS 5V OR 3.3V PIN AND GROUND.
int motor1pin1 = 53; //signal for motors. 53 is the digital i/o pin number.
int motor1pin2 = 52;
int motor2pin1 = 51;
int motor2pin2 = 50;
int motor3pin1 = 49;
int motor3pin2 = 48;
int motor4pin1 = 47;
int motor4pin2 = 46;
int motor5pin1 = 45;
int motor5pin2 = 44;
#include <Wire.h> //I2C comm library
#include "RTClib.h" //includes rtc library
RTC_DS3231 rtc; //move this line if program doesnt work.
#include <LiquidCrystal.h>// Includes lcd library,
// if a different lcd screen is used, a different library with different syntax may need to be used.
//removing any of these libraries will cause errors
LiquidCrystal lcd(35, 34, 33, 32, 31, 30); // lcd pinout
//setup code only run on intital power up or reset
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
//if (rtc.lostPower()) {
// Serial.println("RTC lost power, setting the time!");
// // Set the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// { rtc.adjust(DateTime(2025, 5, 11, 18, 26, 0)); Set time, Comment this code and the } once time is initially set,
//battery backup will keet time if power is lost folllows, Y M D H M S, in 24hr format.
// }
//pins for dc motor control will always be output.
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(motor3pin1, OUTPUT);
pinMode(motor3pin2, OUTPUT);
pinMode(motor4pin1, OUTPUT);
pinMode(motor4pin2, OUTPUT);
pinMode(motor5pin1, OUTPUT);
pinMode(motor5pin2, OUTPUT);
//pwm pins will always be output for motor control.
pinMode(13, OUTPUT); //pwm m1 ENA
pinMode(12, OUTPUT); //pwm m2 ENB
pinMode(11, OUTPUT); //pwm m3 ENA
pinMode(10, OUTPUT); //pwm m4 ENB
pinMode(9, OUTPUT); //pwm m5 ENA
lcd.begin(16, 2); //initializes lcd screen, 16 character rows, 2 total rows,
// update appropriately if using a larger screen.
lcd.clear(); // clear screen on bootup.
}
//FULL CODE RUN EVERY CYCLE, INCLUDES RTC LOGIC AND MOTOR/LCD CODE
void loop() {
DateTime now = rtc.now();
int currentHour = now.hour();
if (currentHour >= 8 && currentHour < 21) { //hours of operation, 24hr format
//Logic to run program
Serial.print("Running main program at ");
Serial.println(now.timestamp());
delay(1000);
// MAIN ROUTINE
// IMPORTANT PLEASE READ, all delay commands are in milliseconds, so below is a delay of 3 seconds.
delay(3000);
lcd.print("Welcome to");
// always use zero for first number ex. (0,x) otherwise text will start midscreen,
// second value ex. (x, 0)top row is 0 bottom row is 1.
lcd.setCursor(0, 1);
// parantheses are needed at the start and end of text for syntax.
lcd.print("UW-Stout,");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Wisconsin's");
lcd.setCursor(0,1);
lcd.print("Polytechnic.");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enjoy your tour");
lcd.setCursor(0, 1);
lcd.print("of Fryklund Hall");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("and our CSTEMM");
lcd.setCursor(0,1);
lcd.print("display case!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("- ET405 Display");
lcd.setCursor(0, 1);
lcd.print("Case Group :D");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);//if text is edited, make sure final lines are clear screen and set cursor to 0,0.
//Follow pattern of print top, set cursor 0,1 print bottom, delay, clear, set cursor (0,0) if adding additional text.
// if the lcd screen prints gibberish on subsequent cycles it is typically caused by incorrect digital output pinout,
// for motors or the lcd screen itself, you'll know what I mean if you see it.
// following lines of code are repeat of first lcd cycle.
delay(1000);
lcd.print("Welcome to");
lcd.setCursor(0, 1);
lcd.print("UW-Stout,");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Wisconsin's");
lcd.setCursor(0,1);
lcd.print("Polytechnic.");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enjoy your tour");
lcd.setCursor(0, 1);
lcd.print("of Fryklund Hall");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("and our CSTEMM");
lcd.setCursor(0,1);
lcd.print("display case!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("- ET405 Display");
lcd.setCursor(0, 1);
lcd.print("Case Group :D");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
//below is the dwell time between stopping this module and starting the next module.
delay(2000);
//MOTOR 1 turntable
// speed (0 = off and 255 = max speed):
//Because of the rotational inertia of the turntable, higher power is needed for start-up to avoid stalling,
// start with higher power and smoothly lower power for smooth transition between startup and running.
analogWrite(13, 125); //ENA pin
//control direction, High sends power, low sends ground, reverse polarity to reverse direction,
// this applies to all the motors used in this code.
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
delay(100);
// this is the code to slow the motor to operating speed after starting.
analogWrite(13,120);
delay(25);
analogWrite(13,115);
delay(25);
analogWrite(13,110);
delay(25);
analogWrite(13,105);
delay(25);
analogWrite(13,100);
delay(25);
analogWrite(13,95);
delay(25);
analogWrite(13,90);
delay(25);
analogWrite(13,85);
delay(25);
analogWrite(13,80);
delay(25);
analogWrite(13,75);
delay(17000); // this is the time the turntable will run, ignoring startup and slowdown times.
//slow down code below.
analogWrite(13,70);
delay(200);
analogWrite(13,65);
delay(200);
analogWrite(13,60);
delay(200);
analogWrite(13,55);
delay(200);
analogWrite(13,50);
delay(200);
// to stop dc motors set both pins to low or high, or set pwm signal to 0.
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
//below is the dwell time between stopping this module and starting the next.
delay(500);
// MOTOR2 gear train
//start with a low speed and smoothly increase speed to operating speed and decrease speed on stop.
// lower speed prolongs longevity and decreases component stress.
// (0 = off and 255 = max speed):
analogWrite(12, 100); //ENB pin
// direction
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
// below is start up cycle.
delay(125);
analogWrite(12,150);
delay(125);
analogWrite(12,160);
// cycle time at desired speed below.
delay(18500);
//slow down cycle below.
analogWrite(12,150);
delay(250);
analogWrite(12,100);
delay(250);
analogWrite(12,50);
delay(250);
analogWrite(12,0);
delay(250);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
//below is the dwell time between stopping this module and starting the next module.
delay(500);
//MOTOR3 marble track
//start with a low speed and smoothly increase speed, and decrease speed on stop.
// ENSURE POLARITY IS CORRECT, ROTATING THE MOTOR COUNTER CLOCKWISE WILL DAMAGE THE ASSEMBLY,
//PROPER POLARITY IS HIGH/POSITIVE TO RED WIRE AND LOW/GROUND TO BLACK WIRE TO ROTATE MOTOR CLOCKWISE.
// (0 = off and 255 = max speed):
analogWrite(11, 100); //ENA pin
// direction
digitalWrite(motor3pin1, HIGH);
digitalWrite(motor3pin2, LOW);
//start up cycle below.
delay(125);
analogWrite(11, 125);
delay(125);
analogWrite(11,150);
delay(125);
analogWrite(11,175);
delay(125);
//main cycle below.
analogWrite(11,200);
delay(18500);
//slow down cycle below
analogWrite(11,175);
delay(125);
analogWrite(11,150);
delay(125);
analogWrite(11,125);
delay(125);
analogWrite(11,100);
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, LOW);
//below is the dwell time between stopping this module and starting the next module.
delay(500);
//MOTOR4 INj. mold die, start with a low speed and smoothly increase speed, and decrease speed on stop
// because of the deflection of the pins in the bores of the die, running this assembly at lower speeds can cause
// a chattering/rubbing noise.
// (0 = off and 255 = max speed):
analogWrite(10, 100); //ENB pin
// direction
digitalWrite(motor4pin1, HIGH);
digitalWrite(motor4pin2, LOW);
//start up cycle below.
delay(50);
analogWrite(10, 125);
delay(50);
analogWrite(10, 150);
delay(50);
analogWrite(10, 175);
//main cycle below.
delay(18500);
//slow down cycle below.
analogWrite(10, 100);
delay(250);
analogWrite(10, 50);
delay(250);
analogWrite(10,25);
delay(250);
analogWrite(10,0);
delay(250);
digitalWrite(motor4pin1, LOW);
digitalWrite(motor4pin2, LOW);
//below is the dwell time between stopping this module and starting the next module.
delay(500);
//MOTOR5 Box because of the very low load of this motor, pwm ramp up and ramp down are not needed
// ensure that the set time is correct for the desired amount of opening on the box, otherwise damage can occur from
//opening the box too much.
// (0 = off and 255 = max speed):
analogWrite(9, 100); //ENA pin
//opens box
digitalWrite(motor5pin1, HIGH);
digitalWrite(motor5pin2, LOW);
delay(4000);//cycle time
//stops motor
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, LOW);
delay(2000); //dwell
//closes box
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, HIGH);
delay(4000);//cycle time
//stops motor
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, LOW);
delay(2000);//dwell
analogWrite(9, 100); //ENA pin
//opens box
digitalWrite(motor5pin1, HIGH);
digitalWrite(motor5pin2, LOW);
delay(4000);//cycle time
//stops motor
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, LOW);
delay(2000);//dwell
//closes box
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, HIGH);
delay(4000);//cycle time
//stops motor
digitalWrite(motor5pin1, LOW);
digitalWrite(motor5pin2, LOW);
// dwell time before the entire cycle restarts at lcd screen.
delay(2000);
} else {
// idle behavior outside of hours of operation
Serial.print("Outside of allowed time (");
Serial.print(now.hour());
Serial.println("h), skipping main logic.");
delay(60000); // Wait a minute before checking again
}
just a suggestion to compact the long analogWrite list ![]()
int stepdown = 150;
while(stepdown > 50) {
analogWrite(13, stepdown);
if(stepdown > 75) {
delay(25);
}
elseif (stepdown = 75) {
delay (17000);
}
else {
delay (200);
}
stepdown -= 5;
}
good luck
sorry correct a small typo with fatal consequence... ![]()
elseif (stepdown == 75) {
Referring to Post #4.
Your code could use helper functions to reduce size and increase human readability. Another improvement would be to make the motor speeds change at the same rate. I kept those similar, but added a little simplification.
In the simulation (code and wokwi diagram below), I used LEDs as the motorPlus, motorMinus and enable.
// https://forum.arduino.cc/t/ds3231-rtc-help/1380780/5
#include <Wire.h> //I2C comm library
#include "RTClib.h" //includes rtc library
RTC_DS3231 rtc; //move this line if program doesnt work.
#include <LiquidCrystal.h>// Includes lcd library,
LiquidCrystal lcd(35, 34, 33, 32, 31, 30); // RS, EN, D4, D5, D6, D7
int motorpin[] = {53, 52, 51, 50, 49, 48, 47, 46, 45, 44};
int motorpins = sizeof(motorpin) / sizeof(motorpin[0]);
int enablepin[] = {13, 12, 11, 10, 9};
int enablepins = sizeof(enablepin) / sizeof(enablepin[0]);
char words[][16] = {
{"Welcome to \0"}, {"UW-Stout, \0"},
{"Wisconsin's \0"}, {"Polytechnic. \0"},
{"Enjoy your tour\0"}, {"at Fryklund Hall\0"},
{"and our CSTEMM \0"}, {"display case! \0"},
{"ET405 Display \0"}, {"Case Group :D \0"}
};
void setup() {
Serial.begin(1115200);
rtc.begin();
lcd.begin(16, 2);
lcd.clear();
for (int i = 0; i < motorpins; i++)
pinMode(motorpin[i], OUTPUT);
for (int i = 0; i < enablepins; i++)
pinMode(enablepin[i], OUTPUT);
}
void loop() {
DateTime now = rtc.now();
int currentHour = now.hour();
if (currentHour >= 8 && currentHour < 21) {
Serial.print("Running program");
Serial.println(now.timestamp());
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 5; i++) {
lcd.setCursor(0, 0);
lcd.print(words[i * 2]);
lcd.setCursor(0, 1);
lcd.print(words[i * 2 + 1]);
delay(2000);
lcd.clear();
}
delay(2000);
}
analogWrite(enablepin[0], 125); //ENA pin
motorCommand(motorpin[0], LOW, motorpin[1], LOW, 100);
for (int i = 0; i < 10; i++) {
analogWrite(enablepin[0], 120 - i * 5);
delay(25);
}
delay(17000);
for (int i = 0; i < 5; i++) {
analogWrite(enablepin[0], 70 - i * 5);
delay(200);
}
motorCommand(motorpin[0], LOW, motorpin[1], LOW, 500);
analogWrite(enablepin[1], 100); //ENB pin
motorCommand(motorpin[2], HIGH, motorpin[3], LOW, 125);
speedChange(enablepin[1], 150, 125);
speedChange(enablepin[1], 160, 18500);
speedChange(enablepin[1], 150, 250);
speedChange(enablepin[1], 100, 250);
speedChange(enablepin[1], 50, 250);
speedChange(enablepin[1], 0, 250);
motorCommand(motorpin[2], LOW, motorpin[3], LOW, 500);
analogWrite(enablepin[2], 100); //ENA pin
motorCommand(motorpin[4], HIGH, motorpin[5], LOW, 125);
speedChange(enablepin[2], 125, 125);
speedChange(enablepin[2], 150, 125);
speedChange(enablepin[2], 175, 125);
speedChange(enablepin[2], 200, 18500);
speedChange(enablepin[2], 175, 125);
speedChange(enablepin[2], 150, 125);
speedChange(enablepin[2], 125, 125);
speedChange(enablepin[2], 100, 125);
analogWrite(enablepin[2], 100);
motorCommand(motorpin[4], LOW, motorpin[5], LOW, 500);
analogWrite(enablepin[3], 100); //ENB pin
motorCommand(motorpin[6], HIGH, motorpin[7], LOW, 50); // MOTOR3
speedChange(enablepin[3], 125, 50);
speedChange(enablepin[3], 150, 50);
speedChange(enablepin[3], 175, 18500);
speedChange(enablepin[3], 100, 250);
speedChange(enablepin[3], 50, 250);
speedChange(enablepin[3], 25, 250);
speedChange(enablepin[3], 0, 250);
motorCommand(motorpin[6], LOW, motorpin[7], LOW, 500); // MOTOR3
for (int i = 0; i < 2; i++) {
analogWrite(enablepin[4], 100); //ENA pin MOTOR4
motorCommand (motorpin[8], LOW, motorpin[9], HIGH, 4000); // cycle time
motorCommand (motorpin[8], LOW, motorpin[9], LOW, 2000); // dwell
motorCommand (motorpin[8], LOW, motorpin[9], HIGH, 4000); // cycle time
motorCommand (motorpin[8], LOW, motorpin[9], LOW, 2000); // dwell
}
} else {
Serial.print("Outside of allowed time (");
Serial.print(now.hour());
Serial.println("h), skipping main logic.");
delay(60000); // Wait a minute before checking again
}
}
void motorCommand (int motpin0, bool state0, int motpin1, bool state1, int delaytime) {
digitalWrite(motpin0, state0);
digitalWrite(motpin1, state1);
delay(delaytime);
}
void speedChange(int enaPin, int speed, int delaytime) {
analogWrite(enaPin, speed);
delay(delaytime);
}
diagram.json for wokwi
{
"version": 1,
"author": "foreignpigdog x",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-mega", "id": "mega", "top": -9, "left": -3.6, "attrs": {} },
{ "type": "wokwi-ds1307", "id": "rtc1", "top": 186.6, "left": 268.9, "attrs": {} },
{ "type": "wokwi-lcd1602", "id": "lcd1", "top": -150.17, "left": 169.6, "attrs": {} },
{
"type": "wokwi-led-bar-graph",
"id": "bargraph1",
"top": 4.8,
"left": 43.6,
"rotate": 180,
"attrs": { "color": "lime" }
},
{
"type": "wokwi-led-bar-graph",
"id": "bargraph2",
"top": 100.8,
"left": 43.6,
"rotate": 180,
"attrs": { "color": "lime" }
}
],
"connections": [
[ "rtc1:GND", "mega:GND.3", "black", [ "h0" ] ],
[ "rtc1:5V", "mega:5V", "red", [ "h0" ] ],
[ "rtc1:SDA", "mega:A4", "blue", [ "h0" ] ],
[ "rtc1:SCL", "mega:A5", "gold", [ "h0" ] ],
[ "mega:35", "lcd1:RS", "limegreen", [ "v1.2", "h-21.8" ] ],
[ "mega:34", "lcd1:E", "limegreen", [ "v1.2", "h-117.4" ] ],
[ "mega:33", "lcd1:D4", "limegreen", [ "v1.1", "h-60.2" ] ],
[ "mega:32", "lcd1:D5", "limegreen", [ "v1.1", "h-59.8" ] ],
[ "mega:31", "lcd1:D6", "limegreen", [ "v1", "h-41" ] ],
[ "mega:30", "lcd1:D7", "limegreen", [ "v1", "h-31" ] ],
[ "mega:5V.1", "lcd1:A", "red", [ "v1", "h-11.8" ] ],
[ "mega:GND.4", "lcd1:K", "black", [ "v0.95", "h-11.8" ] ],
[ "mega:13", "bargraph1:A9", "gold", [ "v0" ] ],
[ "mega:12", "bargraph1:A6", "gold", [ "v0" ] ],
[ "mega:11", "bargraph1:A3", "gold", [ "v0" ] ],
[ "mega:10", "bargraph2:A10", "gold", [ "v0" ] ],
[ "mega:9", "bargraph2:A7", "gold", [ "v0" ] ],
[ "mega:53", "bargraph2:A6", "violet", [ "v0" ] ],
[ "mega:52", "bargraph2:A8", "violet", [ "v1.1", "h-251.8", "v-19.2" ] ],
[ "mega:51", "bargraph2:A9", "violet", [ "v1", "h-194.6", "v-28.8" ] ],
[ "mega:50", "bargraph1:A1", "violet", [ "v1", "h-184.6", "v-19.2" ] ],
[ "mega:49", "bargraph1:A2", "violet", [ "v1.15", "h-185", "v-38.4" ] ],
[ "mega:48", "bargraph1:A4", "violet", [ "v1.15", "h-175", "v-38.4" ] ],
[ "mega:47", "bargraph1:A5", "violet", [ "v1.05", "h-175.4", "v-48" ] ],
[ "mega:46", "bargraph1:A7", "violet", [ "v1.05", "h-165.4", "v-57.6" ] ],
[ "mega:45", "bargraph1:A8", "violet", [ "v1.2", "h-165.8", "v-76.8" ] ],
[ "mega:44", "bargraph1:A10", "violet", [ "v1.2", "h-155.8", "v-76.8" ] ],
[ "bargraph1:C10", "bargraph1:C9", "green", [ "h0" ] ],
[ "bargraph1:C9", "bargraph1:C8", "green", [ "h0" ] ],
[ "bargraph1:C8", "bargraph1:C7", "green", [ "h0" ] ],
[ "bargraph1:C7", "bargraph1:C6", "green", [ "h0" ] ],
[ "bargraph1:C6", "bargraph1:C5", "green", [ "h0" ] ],
[ "bargraph1:C5", "bargraph1:C4", "green", [ "h0" ] ],
[ "bargraph1:C4", "bargraph1:C3", "green", [ "h0" ] ],
[ "bargraph1:C3", "bargraph1:C2", "green", [ "h0" ] ],
[ "bargraph1:C2", "bargraph1:C1", "green", [ "h0" ] ],
[ "bargraph1:C1", "bargraph2:C10", "green", [ "v0" ] ],
[ "bargraph2:C10", "bargraph2:C9", "green", [ "h0" ] ],
[ "bargraph2:C9", "bargraph2:C8", "green", [ "h0" ] ],
[ "bargraph2:C8", "bargraph2:C7", "green", [ "h0" ] ],
[ "bargraph2:C7", "bargraph2:C6", "green", [ "h0" ] ],
[ "bargraph2:C6", "bargraph2:C5", "green", [ "h0" ] ],
[ "bargraph2:C5", "bargraph2:C4", "green", [ "h0" ] ],
[ "bargraph2:C4", "bargraph2:C3", "green", [ "h0" ] ],
[ "bargraph2:C3", "bargraph2:C2", "green", [ "h0" ] ],
[ "bargraph2:C2", "bargraph2:C1", "green", [ "h0" ] ],
[ "bargraph2:C1", "mega:GND.3", "black", [ "v9.6", "h18.98" ] ]
],
"dependencies": {}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
