DC motor lift problems

Hey,

I'm building a small lift to carry a marble sized ball up and down a timing belt. Everything works ok but I am running into one issue that is causing some problems. I'm using a timing belt attached to a pulley on top and bottom of the lift length. On top is a limit switch that is pressed when the ball reaches the end of its travel. The problem I am having is that when the limit switch is pressed, instead of the motor shutting itself off, it seems to stall for about one second and then turn off. I've measured the current and there is a definite peak of about 1.2 amps at the end of the balls travel. The load current of the motor is 600mA so I'm worried about the peak at the end. The motor does stall at the end and hold the belt tight for a second and then lets go. I really want to be able to have the ball press the limit switch and instantly cut power. I'm attaching my code to see if anything shows up there that seems obvious. Thanks

int motOne = 2;   // pin 2 feeding L298n driver in one direction
int motTwo = 4;    // pin 4 feeding L298n driver in one direction
int limitOne = 3;  // pin 3 as input to read limit switch with pull down
int solTrig = 5;   // pin 5 feeding base of transistor triggering solenoid



void setup() {
pinMode (motOne, OUTPUT);     
pinMode(motTwo, OUTPUT);
pinMode(solTrig, OUTPUT);
pinMode (limitOne, INPUT);
digitalWrite (motOne, LOW);
digitalWrite(motTwo, LOW);
digitalWrite(solTrig, LOW);   


}

void loop() {

delay(1000);                   // wait for one second to begin motion
digitalWrite(motOne, HIGH);    // turn motor on moving up

int limitOneValue = digitalRead (limitOne);
if (limitOneValue == HIGH) {    // if limit switch is pressed
  digitalWrite (motOne, LOW);   // turn motor off
  delay(1000);                  // wait for one second
  digitalWrite(solTrig, HIGH); // turn on solenoid
  delay(500);                    // hold solenoid for 0.5 seconds
  digitalWrite(solTrig, LOW);    // turn off solenoid
  delay(200);                    // wait 0.2 seconds
  digitalWrite(motTwo, HIGH);     // turn motor on in opposite direction
  delay(1000);                   // keep motor moving for 1 second
  digitalWrite(motTwo, LOW);       // turn motor off
  delay(2000);                     // wait for two seconds
  
}

}

Drop the very first delay(1000). That is one reason for the stalling. Suppose the code just has reached the top of loop() when the switch is operated. How will Your code pic that up?

So your “problem” is the motor still tries to move the ball for a second too long.

In your code, you “start” your loop with a 1 second delay before it turns on the motor and then checks the limit switch..

Issue becomes after it checkes the limit switch, the motor is still running and then it loops back to the start.

Now your at beginning of the loop, motor is still running and your waiting for delay to complete.

Maybe move that delay and motor start into setup so it only runs once and then have your loop “only” check for the limit switch to trigger your sequence?

Take a look at the "Blink without delay" tutorial;

It will show you how to have a delay without stopping your sketch processing other tasks.

How do You secure that the return travel is complete? Having a another switch there to indicate? Now You run blindly for one second.

I don't see a facepalm emoji haha. Thanks! All good now

Thanks, I do plan to switch to using millis. I'm more familiar with delay so as a quick mock up I just wanted to get things working for a test run

Railroader:
How do You secure that the return travel is complete? Having a another switch there to indicate? Now You run blindly for one second.

Yeah same deal there's a limit switch at the bottom. I just haven't implemented it yet. The one second runtime for the lift is just to reset it

Just wait for an "end/return" switch to go active. You'll fix it, get Your mock up working and then think of using "millis()". It makes it possible to do several things in the loop and awoids loosing computer power by "sleeping".

By the way, why not use INPUT_PULLUP and swiches making it to GND? Floating inputs are really unreliable if nothing is feeding the input.

Railroader:
By the way, why not use INPUT_PULLUP and swiches making it to GND? Floating inputs are really unreliable if nothing is feeding the input.

The input pin for the switch has a pull-down resistor in the circuit

That is safe. It only cost You an external resistor.