Stepper motor light and dark

Hi, I am using Accelstepper.h All works fine but I need the power taken away after the moter did run. Using the software to open a gate at dayligth and closing at dark, bur the stepper stayes on power while doin nothing for the day

TOPIC SPLIT
PLEASE DO NOT HIJACK / NECRO POST !

Could you take a few moments to Learn and Use The Forum

It will help you get the best out of the forum in the future.

  • Your OS and version can be valuable information, please include it along with extra security you are using.

  • Always list the version of the IDE you are using and the board version if applicable.

  • Use quote or add error messages as an attachment NOT a picture.

  • How to insert an image into your post. ( Thanks @sterretje )

  • Add your sketch where applicable but please use CODE TAGS ( </> )

  • Add a SCHEMATIC were needed even if it is hand drawn

  • Add working links to any specific hardware as needed (NOT links to similar items)

  • Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

  • Ensure you have FULLY inserted the USB cables.

  • Check you have a COMMON GROUND where required. ( Thanks @Perry)

  • Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

  • Try other computers where possible.

  • Try other USB leads where possible.

  • You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

  • There may be a problem with the board check or remove your wiring first.

  • Remove any items connected to pins 0 and 1.

COMPUTER RELATED

  • Close any other serial programs before opening the IDE.

  • Ensure you turn off any additional security / antivirus just to test.

  • There may be a problem with the PC try RESTARTING it.

  • You may be selecting the wrong COM port.

  • Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

  • Clear your browsers CACHE.

  • Close the IDE before using any other serial programs.

  • Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

  • CH340/341 based clones do not report useful information to the “get board info” button.

  • NANO (Old Types) some require you to use the OLD BOOTLOADER option.

  • NANO (ALL Types) See the specific sections lower in the forum.

  • NANO (NEW Types) Install your board CORE’s.

  • Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

  • Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

  • On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini

  • On Linux ~/ArduinoCreateAgent-1.1/config.ini

  • On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.

Language problem ?

Try a language closer to your native language:

Thanks to all those who helped and added to this list.

Hi,
Can you please post your code and a circuit diagram?

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

Does your stepper driver have an enable pin. Usually the enable pin will turn off the driver output transistors so the motor is no longer powered.

Will the motor be able to hold its place or does that matter?

If your driver has an enable pin there are functions in the AccelStepper library to deal with the enable.

setEnablePin()

enableOutputs()

disableOutputs()

Hi, Yes I did read how to learn Using the Forum.
As a newbe I did not know how to start on the forum.
Thank you very much and I try my best to obay

Hi, I am aware of other stepperdriver IC ( L293 or L297 ) they have Enable etc pins. this would need to redesign my attached board. I am able to do this with a test, but are unsure if this wil work. thank you for the alternative idee.

Hi @BenBakker

Please post your code and a circuit diagram.

What other IC are you using?

You have the project infront of you, we don't.
You know how it is wired, we don't.
You know how you are powering the motor and arduino, we don't.
You know how you are outputting digital logic to the H-Bridge, we don't.

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

/* This Arduino Schetch wil open (in the morning) and close  ( in the evening ) the gate from a chicken barn. 
   It makes use of an LDR sensor to measure the ligth intensity 
   The sketch is to control a stepper motor with ULN2003 driver board
   controlled by Arduino UNO with use of the AccelStepper library for: acceleration and deceleration.
   The driver board is from Phoenix Contact with the ULN 2003 driver chip
   Modified by Ben Bakker 25-november 2021 */


#include <AccelStepper.h> // Include the AccelStepper library:

// Motor pin definitions:
#define motorPin1  9     // IN1 on the ULN2003 driver
#define motorPin2 10     // IN4 on the ULN2003 driver
#define motorPin3 11     // IN3 on the ULN2003 driver
#define motorPin4 12     // IN2 on the ULN2003 driver

#define MotorInterfaceType 8 // Define the AccelStepper interface type;
                             // type 8 means 4 wire motor in half step mode:

// Initialize the pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library for stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);

const int startPin = A2;                    // Read the value of the LDR
const int sensorPin1 = 2;                   // Read the sensor ( read contact sensor ) (gate in closed position), for safety after power reset.
                                            // An power reset will make the system trying to re close the gate.
                                            // even when the gate already is in the closed position.
const int sensorPin2 = 3;                   // Read the ( reed contact sensor ) (gate in open position), for safety after power reset.
                                            // An power reset will make the system trying to re open the gate.
                                            // even when the gate already is in the open position.
void setup(){                                              
stepper.setMaxSpeed(256);                   // maximum speed if more then 768 stepmotor becomes noisy
stepper.setAcceleration(200);               // Set the maximum acceleration in steps per second^2:

 Serial.begin(115200);                      // Set Serial port Baudrate to 115200
   pinMode(startPin, INPUT_PULLUP);         // Make pin A2 input with internal pullup, LDR  
   pinMode(sensorPin1, INPUT_PULLUP);       // Make pin 2 input with internal pullup
   pinMode(sensorPin2, INPUT_PULLUP);       // Make pin 3 input with internal pullup 
}                                           // End of Setup

void loop (){


 while (analogRead(startPin) <= 400){         // The value from the LDR sensor min/max is 0..1024
    delay(100);                             // wait until button is released
       stepper.moveTo(5800);                // 5880 steps are needed for 3,8 revolution for a distance of 38 cm  
         stepper.runToPosition();           // Run to position with set speed and acceleration:  
          delay(1000);                      // To make it less sensative for very small interuptions 
          break;        
           }                                // End of opening the gate ( up )          
    // Power on steppermotor stayes consuming 0.5 Amp
   

    
   while (analogRead(startPin) >= 600){
    // reverse direction, run back to original position:   
      delay(100);                           // Not needed but avoids contact bouncing in case of mechanical contacts
        stepper.moveTo(0);                  // Steps back 5800 steps to come back to start position
          stepper.runToPosition();          // Run back to start position with set speed and acceleration:  
           delay(1000);                     // To make it less sensative for very small interuptions
           break;
            }                               // End of closing the gate ( down )
        }                                   // End of Loop
         // Power on steppermotor stayes consuming 0.5 Amp
![Schematic Arduino Uno + Steppermotor|690x470](upload://uwoHZQh9hTxbnmJBXuBq7QdbENq.jpeg)

Please move the schematic out of the code tags.

What is the problem with the new code?

Hi, Sorry for the included schematic. I used the upload from the menu thinking it would make 2 seperate items. Should I do it again but then as 2 reply's ?
This code runs fine but when using the reed contacts it only in the first part after the loop started it failed. I tryed to use it with do and val but the error given is "startpin does not name a type, did you mean static"

/* This Arduino Schetch wil open (in the morning) and close  ( in the evening ) the gate from a chicken barn. 
   It makes use of an LDR sensor to measure the ligth intensity 
   The sketch is to control a stepper motor with ULN2003 driver board
   controlled by Arduino UNO with use of the AccelStepper library for: acceleration and deceleration.
   The driver board is from Phoenix Contact with the ULN 2003 driver chip
   Modified by Ben Bakker 25-november 2021 */


#include <AccelStepper.h>                  // Include the AccelStepper library:

                                            // Motor pin definitions:
#define motorPin1  9                        // IN1 on the ULN2003 driver
#define motorPin2 10                        // IN4 on the ULN2003 driver
#define motorPin3 11                        // IN3 on the ULN2003 driver
#define motorPin4 12                        // IN2 on the ULN2003 driver

#define MotorInterfaceType 8                // Define the AccelStepper interface type;
//#define MotorInterfaceType 4                // Define the AccelStepper interface type;
                                            // type 8 means 4 wire motor in half step mode:
startPin val 1;
sensorPin2 val 2;                                           

// Initialize the pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library for stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);

const int startPin = A2;                    // Read the value of the LDR
const int sensorPin1 = 2;                   // Read the sensor ( read contact sensor ) (gate in closed position), for safety after power reset.
                                            // when the power for whatever reason is reset, the system is trying to re close the gate.
                                            // when the gate is already in the closed position, this creates mechanical problems.
const int sensorPin2 = 3;                   // Read the sensor ( reed contact sensor ) (gate in open position), for safety after power reset.
                                            // when the power for whatever reason is reset, the system is trying to re open the gate.
                                            // when the gate is already in the open position, this creates mechanical problems.

                                            
void setup(){ 

 


stepper.setMaxSpeed(256);                   // maximum speed if more then 768 stepmotor becomes noisy
stepper.setAcceleration(200);               // Set the maximum acceleration in steps per second^2:


 Serial.begin(115200);                      // Set Serial port Baudrate to 115200
   pinMode(startPin, INPUT_PULLUP);         // Make pin A2 input with internal pullup, LDR  
   pinMode(sensorPin1, INPUT_PULLUP);       // Make pin 2 input with internal pullup
   pinMode(sensorPin2, INPUT_PULLUP);       // Make pin 3 input with internal pullup 
}                                           // End of Setup

void loop (){

do {
  val 1 =(analogRead(startPin) <= 400);          // Run steppermotor 5800 steps forward 
       stepper.moveTo(5800);                // 5880 steps are needed for 3,8 revolution for a distance of 38 cm  
         stepper.runToPosition();           // Run to position with set speed and acceleration:  
          delay(1000);                      // To make it less sensative for small interuptions 
         
         
     val 2 =  (digitalRead(sensorPin2) < 400);    // Read the sensor,for safety after power reset.
        stepper.moveTo(0);                  // When past Position, run back to zero
     if (val 2 == LOW) {
      break;
        {{ while (val 1 != LOW);
 }                                        // Power on steppermotor stayes consuming 0.5 Amp
 
   
  if (analogRead(startPin) >= 600){        // reverse direction, run back to original position:   
      delay(100);                           // Not needed but avoids contact bouncing in case of mechanical contacts
        stepper.moveTo(0);                  // Steps back 5800 steps to come back to start position
          stepper.runToPosition();          // Run back to start position with set speed and acceleration:  
           delay(1000);                     // To make it less sensative for very small interuptions
           }
           else
           {
     if (digitalRead(sensorPin1) < 400);    // Read the sensor,for safety after power reset.
        stepper.moveTo(5800);               // When past Position, run back to 5800
         //  return;
             }                               // End of closing the gate ( down )
          }                                       // End of Loop
      
           
                                            // Power on steppermotor stayes consuming 0.5 Amp

The posted code is incomplete. Use the IDE autoformat tool to format the code in a standard manner and you will see that the loop() is not closed.

I am having a hard time understanding what that means, sorry.

Use what to do what?

Post the code that give you the error. Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

What do the last 2 posts have to do with the original question of how to disable the steppers during the daytime? Using the 4 wire motor in half step mode, when the motor stops, one coil in the motor stays energized. If you use the stepper.disableOutputs() function, that coil is turned off. You will need to call the stepper.enableOutputs() function to be able to move the motor again.

/* This Arduino Schetch wil open (in the morning) and close  ( in the evening ) the gate from a chicken barn. 
   It makes use of an LDR sensor to measure the ligth intensity 
   The sketch is to control a stepper motor with ULN2003 driver board
   controlled by Arduino UNO with use of the AccelStepper library for: acceleration and deceleration.
   The driver board is from Phoenix Contact with the ULN 2003 driver chip
   Modified by Ben Bakker 25-november 2021 */


#include <AccelStepper.h>                  // Include the AccelStepper library:

                                            // Motor pin definitions:
#define motorPin1  9                        // IN1 on the ULN2003 driver
#define motorPin2 10                        // IN4 on the ULN2003 driver
#define motorPin3 11                        // IN3 on the ULN2003 driver
#define motorPin4 12                        // IN2 on the ULN2003 driver

#define MotorInterfaceType 8                // Define the AccelStepper interface type;
//#define MotorInterfaceType 4                // Define the AccelStepper interface type;
                                            // type 8 means 4 wire motor in half step mode:

                                           

// Initialize the pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library for stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);

const int startPin = A2;                    // Read the value of the LDR
const int sensorPin1 = 2;                   // Read the sensor ( read contact sensor ) (gate in closed position), for safety after power reset.
                                            // when the power for whatever reason is reset, the system is trying to re close the gate.
                                            // when the gate is already in the closed position, this creates mechanical problems.
const int sensorPin2 = 3;                   // Read the sensor ( reed contact sensor ) (gate in open position), for safety after power reset.
                                            // when the power for whatever reason is reset, the system is trying to re open the gate.
                                            // when the gate is already in the open position, this creates mechanical problems.
val 1 (sensorPin1);
  val 2 (sensorPin2); 

                                             
void setup(){ 

 


stepper.setMaxSpeed(256);                   // maximum speed if more then 768 stepmotor becomes noisy
stepper.setAcceleration(200);               // Set the maximum acceleration in steps per second^2:


 Serial.begin(115200);                      // Set Serial port Baudrate to 115200
   pinMode(startPin, INPUT_PULLUP);         // Make pin A2 input with internal pullup, LDR  
   pinMode(sensorPin1, INPUT_PULLUP);       // Make pin 2 input with internal pullup
   pinMode(sensorPin2, INPUT_PULLUP);       // Make pin 3 input with internal pullup 
}                                           // End of Setup

void loop (){

do {
  val 1 =(analogRead(startPin) <= 400);          // Run steppermotor 5800 steps forward 
       stepper.moveTo(5800);                // 5880 steps are needed for 3,8 revolution for a distance of 38 cm  
         stepper.runToPosition();           // Run to position with set speed and acceleration:  
          delay(1000);                      // To make it less sensative for small interuptions 
         
         
     val 2 =  (digitalRead(sensorPin2) < 400);    // Read the sensor,for safety after power reset.
        stepper.moveTo(0);                  // When past Position, run back to zero
     if (val 2 == LOW) {
      break;
        {{ while (val 1 != LOW);
 }                                        // Power on steppermotor stayes consuming 0.5 Amp
 
   
  if (analogRead(startPin) >= 600){        // reverse direction, run back to original position:   
      delay(100);                           // Not needed but avoids contact bouncing in case of mechanical contacts
        stepper.moveTo(0);                  // Steps back 5800 steps to come back to start position
          stepper.runToPosition();          // Run back to start position with set speed and acceleration:  
           delay(1000);                     // To make it less sensative for very small interuptions
           }
           else
           {
     if (digitalRead(sensorPin1) < 400);    // Read the sensor,for safety after power reset.
        stepper.moveTo(5800);               // When past Position, run back to 5800
         //  return;
             }                               // End of closing the gate ( down )
          }                                       // End of Loop
      
           
                                            // Power on steppermotor stayes consuming 0.5 Amp
Arduino:1.8.13 (Windows 10), Board:"Arduino Uno"





















Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:33:1: error: 'val' does not name a type

 val 1 (sensorPin1);

 ^~~

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:34:3: error: 'val' does not name a type

   val 2 (sensorPin2);

   ^~~

C:\Users\Ben\Documents\Arduino\Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile\Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile.ino: In function 'void loop()':

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:55:3: error: 'val' was not declared in this scope

   val 1 =(analogRead(startPin) <= 400);          // Run steppermotor 5800 steps forward

   ^~~

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:61:10: error: expected ';' before numeric constant

      val 2 =  (digitalRead(sensorPin2) < 400);    // Read the sensor,for safety after power reset.

          ^

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:63:14: error: expected ')' before numeric constant

      if (val 2 == LOW) {

              ^

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:65:23: error: expected ')' before numeric constant

         {{ while (val 1 != LOW);

                       ^

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:65:31: error: expected ';' before ')' token

         {{ while (val 1 != LOW);

                               ^

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected '}' at end of input

           }                                       // End of Loop

           ^

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected '}' at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected 'while' at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected '(' at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected primary-expression at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected ')' at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected ';' at end of input

Kippenluik_Stepper_accel_decel_and_position_1_dec-2021-werkfile:81:11: error: expected '}' at end of input

exit status 1

'val' does not name a type



Dit rapport zou meer informatie bevatten met
"Uitgebreide uitvoer weergeven tijden compilatie"
optie aan in Bestand -> Voorkeuren.

Here is your code from reply #13 after autoformatting. You can see that the loop() is not closed and some curly brackets ({, }) are missing and, maybe, misplaced.

/* This Arduino Schetch wil open (in the morning) and close  ( in the evening ) the gate from a chicken barn.
   It makes use of an LDR sensor to measure the ligth intensity
   The sketch is to control a stepper motor with ULN2003 driver board
   controlled by Arduino UNO with use of the AccelStepper library for: acceleration and deceleration.
   The driver board is from Phoenix Contact with the ULN 2003 driver chip
   Modified by Ben Bakker 25-november 2021 */

#include <AccelStepper.h>                  // Include the AccelStepper library:

// Motor pin definitions:
#define motorPin1  9                        // IN1 on the ULN2003 driver
#define motorPin2 10                        // IN4 on the ULN2003 driver
#define motorPin3 11                        // IN3 on the ULN2003 driver
#define motorPin4 12                        // IN2 on the ULN2003 driver

#define MotorInterfaceType 8                // Define the AccelStepper interface type;
//#define MotorInterfaceType 4                // Define the AccelStepper interface type;
// type 8 means 4 wire motor in half step mode:

// Initialize the pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library for stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);

const int startPin = A2;                    // Read the value of the LDR
const int sensorPin1 = 2;                   // Read the sensor ( read contact sensor ) (gate in closed position), for safety after power reset.
// when the power for whatever reason is reset, the system is trying to re close the gate.
// when the gate is already in the closed position, this creates mechanical problems.
const int sensorPin2 = 3;                   // Read the sensor ( reed contact sensor ) (gate in open position), for safety after power reset.
// when the power for whatever reason is reset, the system is trying to re open the gate.
// when the gate is already in the open position, this creates mechanical problems.
val 1 (sensorPin1);
val 2 (sensorPin2);

void setup()
{
   stepper.setMaxSpeed(256);                   // maximum speed if more then 768 stepmotor becomes noisy
   stepper.setAcceleration(200);               // Set the maximum acceleration in steps per second^2:


   Serial.begin(115200);                      // Set Serial port Baudrate to 115200
   pinMode(startPin, INPUT_PULLUP);         // Make pin A2 input with internal pullup, LDR
   pinMode(sensorPin1, INPUT_PULLUP);       // Make pin 2 input with internal pullup
   pinMode(sensorPin2, INPUT_PULLUP);       // Make pin 3 input with internal pullup
}                                           // End of Setup

void loop ()
{
   do
   {
      val 1 = (analogRead(startPin) <= 400);         // Run steppermotor 5800 steps forward
      stepper.moveTo(5800);                // 5880 steps are needed for 3,8 revolution for a distance of 38 cm
      stepper.runToPosition();           // Run to position with set speed and acceleration:
      delay(1000);                      // To make it less sensative for small interuptions


      val 2 =  (digitalRead(sensorPin2) < 400);    // Read the sensor,for safety after power reset.
      stepper.moveTo(0);                  // When past Position, run back to zero
      if (val 2 == LOW)
      {
         break;
         {
            {
               while (val 1 != LOW);
            }                                        // Power on steppermotor stayes consuming 0.5 Amp


            if (analogRead(startPin) >= 600)         // reverse direction, run back to original position:
            {
               delay(100);                           // Not needed but avoids contact bouncing in case of mechanical contacts
               stepper.moveTo(0);                  // Steps back 5800 steps to come back to start position
               stepper.runToPosition();          // Run back to start position with set speed and acceleration:
               delay(1000);                     // To make it less sensative for very small interuptions
            }
            else
            {
               if (digitalRead(sensorPin1) < 400);    // Read the sensor,for safety after power reset.
               stepper.moveTo(5800);               // When past Position, run back to 5800
               //  return;
            }                               // End of closing the gate ( down )
         }                                       // End of Loop
val 1 (sensorPin1);
val 2 (sensorPin2);

What are those 2 lines supposed to do? They make no sense as well as being syntax errors. You cannot have spaces in a variable or function name. Are those (val 1 and val 2) functions or do you mean to assign the sensor pins to the val variables?

 val 1 = (analogRead(startPin) <= 400);
 val 2 =  (digitalRead(sensorPin2) < 400); 

Variable names have spaces. Not allowed.

The way that the code is, there is no while for the do. The while is buried so that it is not seen by the compiler as the while that should accompany the do.

The code is hard to follow with the variable names as they are. Using names like ldrPin, openSensorPin, closedSensorPin, ldrValue (val 1), openSensorState (val 2), etc. would make it easier to follow.

Hi,
To post your jpg schematic.
JUST DRAG the file into the post window.

Just do it on its own in a new post.

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

Hi,
I'm not sure why you need a do loop???
This flowchart should be what you are trying to do.

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

Here is a simple program to open the gate at sunrise and close it an sunset. Adjust pin numbers for your setup. The LDR is wired to ground and an analog input set to INPUT_PULLUP to avoid needing an external load resistor. The gate position sensors and close switch are wired to ground and connected to digital inputs set to INPUT_PULLUP.

#include <AccelStepper.h>                  // Include the AccelStepper library:

#define closed 0
#define open 1

// Motor pin definitions:
const byte motorPin1 = 7;                        // IN1 on the ULN2003 driver
const byte motorPin2 = 6;                        // IN4 on the ULN2003 driver
const byte motorPin3 = 5;                        // IN3 on the ULN2003 driver
const byte motorPin4 = 4;                        // IN2 on the ULN2003 driver

const byte closedSensorPin = 9;
const byte openSensorPin = 8;

const byte closeButtonPin = 3;

const byte ldrPin = A0;

bool isHomed = false;

bool position;

#define MotorInterfaceType 8                // Define the AccelStepper interface type
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{
   Serial.begin(115200);
   pinMode(closedSensorPin, INPUT_PULLUP);
   pinMode(openSensorPin, INPUT_PULLUP);
   pinMode(closeButtonPin, INPUT_PULLUP);
   pinMode(ldrPin, INPUT_PULLUP);  // use the internal pullup for LDR load resistor
   stepper.setMaxSpeed(2000);
   stepper.setAcceleration(200);
   stepper.setSpeed(20);
   delay(1000);
   Serial.println("Must close (home) gate to start");
   Serial.println("press the gate close button");
   while (digitalRead(closeButtonPin) == HIGH);
   home();
}

void loop()
{
   if (isHomed == true)
   {
      int ldrValue = analogRead(ldrPin);
      if (ldrValue > 500 && position == open)
      {
         closeGate();
      }
      else if (ldrValue < 300 && position == closed)
      {
         openGate();         
      }
   }
}

void closeGate()
{
   stepper.enableOutputs();
   while (digitalRead(closedSensorPin) == HIGH)
   {
      stepper.move(-1);
      stepper.run();
   }
   stepper.disableOutputs();
   position = closed;
}

void openGate()
{
   stepper.enableOutputs();
   while (digitalRead(openSensorPin) == HIGH)
   {
      stepper.move(1);
      stepper.run();
   }
   stepper.disableOutputs();
   position = open;
}

void home()
{
   while (digitalRead(closedSensorPin) == HIGH)
   {
      stepper.move(-1);
      stepper.run();
   }
   isHomed = true;
   position = closed;
}

To all on this topic. I thank you all very much for all the help and suggestions. A bit more about me:
I am 71 years old and come from another ( analog ) era about electronicsthe . there are still thousand of components to work with in my hobby room ( I will never become old enough to use them all ). Started to play around with Arduino and try to program them. Try finding information and landed for questioning on this forum not knowing how to use it in the proper way.
Great thanks to all again, it is very useful getting help from people who I don't no and they don't no me.

Yes the flowchart looks exact as I think my system should work.
I will for sure try the sketch offered, will change some of the port numbers because my hardware is already connected in this way.
If needed I wil rebuild the hardware to use another control and driver chip.
Thanks again.