Simple Motor with 2 Limit switches

I am trying to design a simple sketch with a DC motor to run constantly, then switch directions when one of two limit switches are pressed. Basically, a constant back and forth movement that changes directions when the respective limit switch is pressed. I attempted to look for a sketch that was already designed, but there are a bazillion hits and all seem to have people yelling at the OP for some nonsense and end up at a dead end. Imaging the idea is a "moving target" that moves back and forth on a sliding rail. The closest thing I have found is a "camera slider" but these seem to be adding too many features. I simply need back and forth with speed adjustment.

I have access to just about anything, but currently have a mechanism that is not arduino controlled. It's a 12v motor driver with a respective motor connected to a DPDT switch (6pin) and a 9v battery that has a B10K pot adjusting the speed. Currently I have to manually flip the switch back and forth to change movement, but someone had mentioned to me that I could program an arduino to automatically change the direction with a limit switch on both sides of the track instead of me having to keep flipping the switch.

Has anyone come across something like this? I have to believe there is something out there...?

You might be interested in Tim Hunkins latest video as I seem to remember he does just this with just the limit switches

Failing that it is probably a "H bridge" which you need to be able to do this using the Arduino

Awesome, Thank You!

Thanks for a well written description of the project.
You'll never find exactly the code Your project needs.
Reading 2 end switches should be an easy task.
Using a motor driver looks like needed to me. I use a double H-bridge good for some 20 Amps. (43 Chinese Amps...)

The code snippet for just running forward looks like:

   if (dir)
    { //                           Fwd
      analogWrite(PWM1_pin, 0);
      analogWrite(PWM2_pin, speed);
    }
    else
    { //                            Bwd
      analogWrite(PWM1_pin, speed);
      analogWrite(PWM2_pin, 0);
    }

Definitions:

#define PWM1_pin 5
#define PWM2_pin 6
int speed = 127; // Min 0, maximum 255. Half speed used

A link to the H bridge I use.

Thank you so much! This info will greatly help. In the video link above, at about 24:00 is almost exactly what I am attempting to make, however, my plan was for continuous movement until the mechanism is powered off. I am going to wire it in as the video show just to get some hands-on education, however, I believe what Railroader posted may be my necessary route. Thanks again for the link to the video above; that guys seems to have all of the projects that I wish to build!

Railroader:
Thanks for a well written description of the project.
You'll never find exactly the code Your project needs.
Reading 2 end switches should be an easy task.
Using a motor driver looks like needed to me. I use a double H-bridge good for some 20 Amps. (43 Chinese Amps...)

The code snippet for just running forward looks like:

   if (dir)

{ //                           Fwd
     analogWrite(PWM1_pin, 0);
     analogWrite(PWM2_pin, speed);
   }
   else
   { //                            Bwd
     analogWrite(PWM1_pin, speed);
     analogWrite(PWM2_pin, 0);
   }




Definitions:




#define PWM1_pin 5
#define PWM2_pin 6
int speed = 127; // Min 0, maximum 255. Half speed used

43 Chinese Amps... :slight_smile: :slight_smile: :slight_smile: YMMD

Normally we don't offer coding right here but Your project is technically limited and would only need a few lines of operational code.

Have You tried any coding on Your controller, learned about Setup() and loop() You are safe in my opinion. Get moving!

int dir = 0;
#define fwd = 2;
#define bwd = 3;

setup(){//beginning of setup
  digitalWrite(PWM1_pin, 0);
  pinMode (PWM1_pin, OUTPUT);
  digitalWrite(PWM2_pin, 0);
  pinMode (PWM2_pin, OUTPUT);

  pinMode(leftLimitSwitch, INPUT_PULLUP);
  pinMode(rightLimitSwitch, INPUT_PULLUP);
}//end of setup

loop(){//beginning of  loop
  if(leftLimitSwitch) dir = fwd;
  if(rightLimitSwitch) dir = bwd;

  runMotor();
}// end of loop

The end switches connects between GND and the resp. pins for the LimitSwitches.

@paulpaulson

I bought some Chines SSRs listed to handle 25 Amps. Then, on Youtube, a guy was opening up exactly such an SSR and inspected the Triac used. The Triac was rated 12 Amps, with heat sinking!