Help with Electronic Timing Motor Project

Hi guys, I am working on a project that uses a RTC module to activate a motor (3-6V DC Motor) after a certain set time has been past (which is determined and set by using a potentiometer). A button is also used to start the timing process. So the components I am using is a ZS-042 RTC time module with a 50K potentiometer and a button. My project aims on reducing current draw when the arduino is "sleeping" because it needs to be able to last underwater for at least 7 days (ropeless fishing trap project). I also looked into other circuit components like the XY-LJ02 but for now, I just want to work with the other three components mentioned including the motor.

So the idea is, the person would first turn the potentiometer to a certain position (we know it outputs analog readings 0-1023 or in voltage: 0-5V with 5V power output from arduino). The arduino will still be asleep (drawing minimal power during this process). The person would then press a button which is connected to the center pin of the potentiometer and Analog pin A0, which causes an interrupt to wake the arduino up. At the same time the arduino will capture the corresponding analogRead signal from the potentiometer and translate that to a time. For example, a 2.5V reading on the potentiometer will tell the RTC module to run for 3.5 days before actuating the motor. The arduino should be able to sleep until the motor is actuator and then turn off again. The goal of this project is around 7 days of timing (person should be free to choose whatever set time). I noticed each gentle turn of the potentiometer knob results in a resolution of +0.01V. There will be a knob attached to the potentiometer with markings indicating different set times. I want to develop a code where the arduino is first asleep, and then turns on once the button is click and remains asleep until the button is reset again. RTC module should be keeping time during this process. I appreciate any help. Here is my code so far:

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino
int pos =0;

// Convert normal decimal numbers to binary coded decimal
volatile int buttonState = 0;
byte decToBcd(byte val)
{
return( (val/1016) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16
10) + (val%16) );
}
void setup()
{
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(30,12,18,1,5,5,19);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());

pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void loop()
{
//Going_To_Sleep();
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(1000); // every second
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
void Going_To_Sleep(){
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full
digitalWrite(LED_BUILTIN,LOW);//turning LED off
sleep_cpu();//activating sleep mode
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}

Welcome to the forums! First off, I'd use the RTC library (RTCLib) rather than writing your own to communicate with the DS3231. Second, I'd use a power library (Arduino Low Power, etc.) rather than write your own.

Then, I'd focus on what you are trying to accomplish...

It would also help to read the sticky posts at the top of this forum about how to post code (use code tags) and provide circuit diagrams, etc. so that people can more easily help you

blh64:
Welcome to the forums! First off, I'd use the RTC library (RTCLib) rather than writing your own to communicate with the DS3231. Second, I'd use a power library (Arduino Low Power, etc.) rather than write your own.

Then, I'd focus on what you are trying to accomplish...

It would also help to read the sticky posts at the top of this forum about how to post code (use code tags) and provide circuit diagrams, etc. so that people can more easily help you

Hi, thanks for your timely response. Is there a way I could do it without purchasing a component that utilizes the Arduino Low Power library? I am trying to minimize cost as much as possible. Thanks

Arduino Libraries are free. A lot of them come with the IDE. Open the IDE, go to Sketch -> Include Library -> Manage Libaries.. and then search for them and then install them. If they aren't packaged with the IDE, just search using google and you most likely will get a link to some github repository where you can download the .zip file and then do Sketch->Include Library -> Add .ZIP library to install it that way.