Building a Time delayed winch

Hi team,

I'm looking to tap into a 4x4 winch and have it turn on and off at certain intervals.

The winch will be moving a break for my sheep (don't worry its all safe). I have built a schematics to how I think to do it but I have run into struggles coding it.

I am thinking to have a potentiometer that controls the run time i.e 30 seconds then another to set the delay time between the running intervals. i.e 12 hours.

I hope this is satisfactory information,
Looking forward to your help.
Mac


bBPxXfJAzJeQq.png)

Hello macwilliams9825

Welcome to the worldbest Arduino forum ever.

Post your sketch, well formated, with well-tempered comments and in so called
code tags "< code >" to see how we can help.

Have a nice day and enjoy coding in C++.

I moved your topic to an appropriate forum category @macwilliams9825.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Please include details of your motor, driver and power supplies

what do you mean by "break"?

do you need a start button?
do you need to reverse the motor?

/me giggles at the sight of that little Fritzing motor representing an actual winch motor :slight_smile:

Pretty simple, but I'd reuse the winch solenoid or get something like a starter solenoid that can handle the current. Make sure you have an emergency switch inline with the relay/solenoid coil to shut off in case something goes wrong.

You will need two timers: one for the every 12 hours and the other for 30 second delay. In a simple world you would just loop: delay 12 hours, turn on relay, delay 30 seconds, turn off relay and repeat.
I assume that the 12 hour delay doesn't have to be dead accurate and being off by a few seconds is fine.

The complication is that you want to read two inputs while waiting so you need to

loop()
{
  delay 1 second
  add 1 second to 12-hour accumulator
  if (accumulated 12 hours)
  {
     turn on relay
     delay relay on time
     turn off relay
     reset 12-hour accumulator
   }
   else
   {
      read potentiometers and update relay on time and 12-hour accumulator
   }
}

Note, you will be told "delay() bad, use millis()" It's up to you. Delay is far simpler to understand and only introduces a 1-second lag between reading potentiometers and updating the values.

At what intervals do you want to re-calibrate this timing system?
Using millis() or delay() over weeks can result in a quite big deviation from real time.

So if you re-start the timing by hand all mornings millis() or delay() will be precise enough.
If this should run automatically over weeks or months you should use a real-time-clock module
of type DS3231 (do not use DS1307! The DS1307 is not worth the name real-time-clock)

Expecting the users here to write all the code for you
in combination with you refusing to learn anything
would end up in a lot of follow-up questions
can you please modify the code to ....
can you please modify the code to ....
can you please modify the code to ....
....

So you would fully depend on the grace of the other users to do all these modifications.

What is very common is to answer follow-up questions if it can be seen that you are learning.
There are threads with dozens some with hundreds of postings.

So as a first step post your attempt how you started writing code.
Post the code as a code-section

best regards Stefan

Thanks I will try and put this information up now!

I have made some progress since I first put this up, after a few weeks of struggling (first time doing this) I had some break throughs.

The problems I am now having are.
I have moved to an LCD screen so I can see what the settings are currently on. but first when I put this together I couldn't change the settings while it was in delay for 12 hours, I think I fixed that problem however now I cant get the motor to turn off.
I understand it is a coding problem not a wiring as I am using Pin 13 so can see the LCD on the board.
I hope this is enough information

I have ended up with this code;

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Initialize the LCD object

const int Button = A0; // Runtime button on the shield
// Delay button on the shield

int runtime = 10; // Default runtime in seconds
int delayTime = 12; // Default delay time in hours
unsigned long motorStartTime;
unsigned long motorDuration;
int delayRangeMax = 420;
int delayRangeMin = 405;
bool settingRuntime = true;

void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Runtime: ");
lcd.print(runtime);
lcd.setCursor(0, 1);
lcd.print("Delay: ");
lcd.print(delayTime);
pinMode(13, OUTPUT);

Serial.begin (9600);
}

void loop() {
int ButtonState = analogRead(Button);
if (ButtonState < 600){
Serial.println ("waiting");
if (ButtonState < 10) {
runtime++;
if (runtime > 60) {
runtime = 1;
}
updateLCD();
delay(200); // Debounce
Serial.println ("Runtime");
}

if (ButtonState > delayRangeMin && ButtonState < delayRangeMax) {
delayTime++;
if (delayTime > 12) {
delayTime = 1;
}
updateLCD();
delay(200); // Debounce
Serial.println ("delay");
}
}
if (millis() - motorStartTime >= motorDuration) {
// Motor action is complete, update settings for the next loop
if (settingRuntime) {
motorDuration = runtime * 1000; // Convert runtime to milliseconds
} else {
motorDuration = delayTime * 3600000; // Convert delayTime to milliseconds
}
digitalWrite(13, HIGH);
motorStartTime = millis();
}

// Let the motor action complete and continue to the next loop
if (millis() - motorStartTime >= motorDuration) {
digitalWrite(13, LOW);
settingRuntime = !settingRuntime; // Toggle between runtime and delay settings
}

}
void updateLCD() {
lcd.setCursor(9, 0);
lcd.print(" "); // Clear previous runtime value
lcd.setCursor(9, 0);
lcd.print(runtime);

lcd.setCursor(7, 1);
lcd.print(" "); // Clear previous delay value
lcd.setCursor(7, 1);
lcd.print(delayTime);
}

Thanks Stefan, I have been working tirelessly on this, how would Include a clock interface? or can i get away with the delay settings for now? timing is not massively important .

Thanks for your input it is appreciated.

Hi @macwilliams9825 ,

please re-edit your posting with your code like described here

I was asking in what time-intervals will you personally adjust the time?

If you want to run this device for weeks the deviation will be hours
example:
gate shall open at 6:00 o'clock in the morning
one week later without a realtime-clock-module it will open at 6:30 am
two weeks later without a realtime-clock-module it will open at 7:00 am
three weeks ater without a realtime-clock-module it will open at 7:30 am
if this is OK for you go with millis() or delay()

If it is not OK for you to have the clock 90 minutes too early or too late after 3 weeks
buy a DS3221-RTC-module
https://eckstein-shop.de/RTCDS3231I2CEchtzeituhrAT24C32RTCModulforArduino2COhneBatterie_1

As you have a display included do wish to show and update the time on the display?
If yes a simple delay(for hours) will not work
As long as the delay() is active the display will not be updated.
The minimum then is to use a pure software RTC.

As you can see from this. A DIY-project with an arduino is not done within 3 or 4 hours.
If you don't want to learn programming buy a ready to use product off the shelf
or
try to find a fab-lab or maker-space near to where you are living
and ask there if somebody finds your project interesting and will do the programming for you.

best regards Stefan

Great job getting started. Can I suggest that you do one thing at a time though. I think the reason your code isn't working as expected is because you have a lot going on. First get the periodic on/off working. You can use hardcoded values for runtime and delayTime. Once that's working, then you can add the time adjustment code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.