Arduino timed actuator

Hey there,
I just made this script:

int motorPin1 = 8;
int motorPin2 = 9;
int openTime = 6*60*60 + 30*60; // 6:30 AM in seconds
int closeTime = 18*60*60; // 6:00 PM in seconds
unsigned long currentTime;
unsigned long openTimeMillis = openTime*1000;
unsigned long closeTimeMillis = closeTime*1000;
unsigned long startMillis;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  startMillis = millis();
}

void loop() {
  currentTime = millis() - startMillis;
  // check if it's time to open
  if (currentTime >= openTimeMillis) {
    // move the linear actuator out
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    delay(10000);
    // stop the motor
    digitalWrite(motorPin1, LOW);
  }
  // check if it's time to close
  if (currentTime >= closeTimeMillis) {
    // move the linear actuator in
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    delay(10000);
    // stop the motor
    digitalWrite(motorPin2, LOW);
  }
}

I have a linear actuator connected to pins 8 and 9 on an uno. I want it to retract at 6:30 am and Extend at 6:00 pm. also, what time is set when i power on the arduino currently? will it extend and retract at the proper times?

im kinda new to this, dont really know what im doing..

Thanks!

Future time stamps yield unpredictable results when millis() 32 bit counter rolls over past zero.

You have to follow the rule of using past time stamps and obtaining intervals by comparing the present time with those.

Also, your design makes millis start at midnight, so it won't work unless that is when you boot the system.

Also, there are no "scripts" in Arduino, only "sketches" or programs.

Invest in an RTC, DS3234 is an excellent choice.

Depending on how big an int is, this may not yield the result you expect.

1 Like

Alright maybe I'll get an rtc. If i did have one, can you give me an example of the code that i would use? Also that one was generated my chatgpt

Hi, @rostabunny
Welcome to the forum.

Can you please post a link to specs/data of your actuator?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

What is the application, would sunrise and sunset be just a good?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Ill get back to you in about 2 hours.... busy currently. It is for an automatic chicken door. I was going to use photoresistor but decided timed would be easier.

RTC libraries come with example sketches. You can see the code online.

Also put "chicken coop door" into the "Search Forum" box above and be prepare to be amazed.

A photoresistor would be much easier on sunny days. Maybe not so much on stormy days.

However, with a photoresistor you can open at sunrise, or a half hour later, start a 12 hour counter, or however long and then close.

Here ia the actuator:

I am using a 2ah 12v battery with this solar panel:
https://www.amazon.com/your-orders/pop/ref=ppx_yo_mob_b_pop?_encoding=UTF8&gen=canonical&lineItemId=ojpkmpqoljrtony&orderId=111-4146987-3440244&packageId=1&returnSummaryId=&returnUnitIndices=&returnUnitMappingId=&shipmentId=Mf48fvBtk

Do not overlook a commercial unit. :thinking:


And this solar charge controller:
https://www.amazon.com/your-orders/pop/ref=ppx_yo_mob_b_pop?_encoding=UTF8&gen=canonical&lineItemId=ojpkmpqoljrvony&orderId=111-9365375-0447450&packageId=1&returnSummaryId=&returnUnitIndices=&returnUnitMappingId=&shipmentId=MthCSy7dk

I would prefer to use a clock not a photoresistor. I have the actuator connected to 8 and 9 digital imput. That is it for connections.
Btw, i am currently only running it off a usb on my pc for testing. Final will be the dc connector connected to the solar charge controller.

I would rather make my own.

Why not ask chatGPT to fix it?

I probably will, just wanted to check with humans to make sure that it doesnt have some obvious problem.

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