Control DC Motor w. Limit switches

Hy @ all

like the title says i want to control an dc motor with limit switches.
Overall i´m planning to use 3 pushbuttons:

Button 1: Should start the motor

button 2: should stop the motor after it reaches a specific position

button 3: should do the same like button 2 only on the other end of the rotation

It should work like this:

Button 1 is pressed: the motor turns clockwise until it reaches the position of the 2nd button.
By pressing the second button the motor stops.

After hitting button 1 again: the motor spins counterclockwise until it reaches button 3, then the motor stops, a counter for button 1 gets a reset so that the loop is finished and can be restarted....

I already tried to build a sketch for that purpose but i´m not sure if it will work or if i´m missing something:

int enableA = 6;              // Motor A, PWM-Pin 
int out1A = 7;                // Motor A, Steuerleitung 1
int out2A = 2;                // Motor A, Steuerleitung 2 
int buttonPin1 = 8;
int buttonPin2 = 9;
int buttonPin3 = 10;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonPushCounter = 0;      // counter for the number of button presses
int lastButtonState = 0;        // previous state of the button



void setup() { 
  
Serial.begin(9600);

pinMode(enableA, OUTPUT); 
pinMode (out1A, OUTPUT);
pinMode (out2A, OUTPUT);
pinMode(buttonPin1,INPUT);  
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);                       
digitalWrite(enableA, HIGH);
   
}

void loop()
{
buttonState = digitalRead(buttonPin1);                  // read the pushbutton input pin
  if (buttonState != lastButtonState) {                 // compare buttonState to previous state
lastButtonState = buttonState;                          // if the current state is 1 then the button
  if (buttonState == 1) {                               // went from off to on:
                                                        
    buttonPushCounter++;                                // buttoncounter +1
    Serial.println("on");
    Serial.print("  ");
    Serial.println(buttonPushCounter, DEC);

      switch (buttonPushCounter) {



	case 1:		                                              // If the button count is at 1
	Serial.println("1");	                                  // print 1
	buttonState2 = digitalRead(buttonPin2);                 // check limit switch1
  if (buttonState2 == HIGH) {                             // if limit switch1 is pressed
  digitalWrite(out1A, LOW);                               // set pin 2 on L293D low
  digitalWrite(out2A, LOW); }                             // set pin 7 on L293D low
	else {                                                  // else
	digitalWrite(out1A, HIGH);                              // set pin 2 on L293D high
  digitalWrite(out2A, LOW);                               // set pin 7 on L293D low
	}
 
        break;
        

case 2:		                                                  // If the button count is at 2
	Serial.println("2");	                                    // print 1
	buttonState3 = digitalRead(buttonPin3);                   // check limit switch2
  if (buttonState3 == HIGH) {                               // if limit switch2 is pressed
  digitalWrite(out1A, LOW);                                 // set pin 2 on L293D low
  digitalWrite(out2A, LOW); }                               // set pin 7 on L293D low
  else {                                                    // else
	digitalWrite(out1A, LOW);                                 // set pin 2 on L293D low
  digitalWrite(out2A, HIGH);     }                          // set pin 7 on L293D high
  buttonPushCounter = 0;
  
        break;
  }      
  }      }
  }

Are you familiar with state machines? if not I highly recommend doing a bit of study as its how
this sort of coding is usually done.

Are you familiar with state machines? if not I highly recommend doing a bit of study as its how
this sort of coding is usually done.

No not really, i´ve heard from that, but up to now i don´t have any experience with it.

A few weeks ago i made a different attempt on this and worked everything out with blocking delays until the motor reaches the desired positions but i´m always feared that something goes wrong so i want to change the whole system to work with limiting switches. Therefor i took the old code and tried to add the switches but i guess it wasn´t a good attempt....

Guess i have to do a lot of research ..... :slight_smile:

elmar:
Hy @ all

like the title says i want to control an dc motor with limit switches.
Overall i´m planning to use 3 pushbuttons:

Button 1: Should start the motor

button 2: should stop the motor after it reaches a specific position

button 3: should do the same like button 2 only on the other end of the rotation

It should work like this:

Button 1 is pressed: the motor turns clockwise until it reaches the position of the 2nd button.
By pressing the second button the motor stops.

After hitting button 1 again: the motor spins counterclockwise until it reaches button 3, then the motor stops, a counter for button 1 gets a reset so that the loop is finished and can be restarted....

I already tried to build a sketch for that purpose but i´m not sure if it will work or if i´m missing something:

int enableA = 6; // Motor A, PWM-Pin
int out1A = 7; // Motor A, Steuerleitung 1
int out2A = 2; // Motor A, Steuerleitung 2
int buttonPin1 = 8;
int buttonPin2 = 9;
int buttonPin3 = 10;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonPushCounter = 0; // counter for the number of button presses
int lastButtonState = 0; // previous state of the button
void setup() {

Serial.begin(9600);
pinMode(enableA, OUTPUT);
pinMode (out1A, OUTPUT);
pinMode (out2A, OUTPUT);
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);
digitalWrite(enableA, HIGH);

}
void loop()
{
buttonState = digitalRead(buttonPin1); // read the pushbutton input pin
if (buttonState != lastButtonState) { // compare buttonState to previous state
lastButtonState = buttonState; // if the current state is 1 then the button
if (buttonState == 1) { // went from off to on:

buttonPushCounter++; // buttoncounter +1
Serial.println("on");
Serial.print(" ");
Serial.println(buttonPushCounter, DEC);
switch (buttonPushCounter) {
case 1: // If the button count is at 1
Serial.println("1"); // print 1
buttonState2 = digitalRead(buttonPin2); // check limit switch1
if (buttonState2 == HIGH) { // if limit switch1 is pressed
digitalWrite(out1A, LOW); // set pin 2 on L293D low
digitalWrite(out2A, LOW); } // set pin 7 on L293D low
else { // else
digitalWrite(out1A, HIGH); // set pin 2 on L293D high
digitalWrite(out2A, LOW); // set pin 7 on L293D low
}

break;

case 2: // If the button count is at 2
Serial.println("2"); // print 1
buttonState3 = digitalRead(buttonPin3); // check limit switch2
if (buttonState3 == HIGH) { // if limit switch2 is pressed
digitalWrite(out1A, LOW); // set pin 2 on L293D low
digitalWrite(out2A, LOW); } // set pin 7 on L293D low
else { // else
digitalWrite(out1A, LOW); // set pin 2 on L293D low
digitalWrite(out2A, HIGH); } // set pin 7 on L293D high
buttonState = 0;

break;
}
} }
}

@elmar, Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

@cesar77muse, I guess you should also read that link.

...R

@Robin2 thank you for your help!, i´ve changed that.

If you use the AutoFormat tool to lay out your code with consistent indents it is much much easier to read and understand.

You are basing your SWITCH on the variable buttonPushCounter and you have code to increment that when the button is pressed. But you have no code to prevent it exceeding 2 which is the max of your SWITCH/CASE system

I reckon you need code something like

if (buttonPushCounter > 2) {
   buttonPushCounter = 1;
}

...R

You are basing your SWITCH on the variable buttonPushCounter and you have code to increment that when the button is pressed. But you have no code to prevent it exceeding 2 which is the max of your SWITCH/CASE system

I reckon you need code something like
Code: [Select]
if (buttonPushCounter > 2) {
buttonPushCounter = 1;

yeah i have just recognized that i made a mistake in the second case, normally the last line before the break would be:

buttonPushCounter = 0;

then the break command, that worked fine for me as long as i was using delays...

elmar:
then the break command, that worked fine for me as long as i was using delays...

If you are expecting a follow-up response to that I don't know what the question is.

...R

I just tested this code, when press button#1, DC motor ran CW, when press button(limit Switch)#2, or #3, motor didn't stop, press button#1 again, motor ran CCW.

Can somebody check this code error? This code function is exactly what I need.

Thanks a lot in advance.

Richard905:
I just tested this code,

What code?

You have just resurrected a Thread that has been dead for almost a year.

If you want help please post the code that you have uploaded to your Arduino.

...R

PS... please post your program using the code button </> so it looks like this. See How to use the Forum. It makes it much easier for people to help you