How to use two micro-switches to control the direction of a motor

I am trying to control a 12V DC motor with two microswitches. The purpose of the two microswitches is to switch the direction of the motor. The motor is controlled using a h-bridge. To run the motor, the power source is a 9V battery.

Problem: Once I connect the two micro switches (ONLY ONE microswitch works at all times), it gives a random output. The 3 scenarios:

  1. One microswitch works perfectly and the other doesn't. Meaning once the microswitch is pressed, the motor switches direction but when I click the second microswitch, it does not switch direction.

  2. I press the working microswitch and it changes the direction multiple times (I'm assuming it detects multiple presses)

  3. None of the push buttons works...

I have a project due soon (less than one week), I would really appreciate it if you can help me.

Attached is a photo of breadboard and the micro-switches

ALSO, when I use 2 push buttons (located on the breadboard, not in the photo**), the direction of the motor switches perfectly just not with the micro-switches...

EDIT* CLICK LINK FOR SCHEMATIC : schematic — ImgBB

code:

/*
  Arduino Starter Kit example
 Project 10  - Zoetrope
 
 This sketch is written to accompany Project 10 in the
 Arduino Starter Kit
 
 Parts required:
 two 10 kilohm resistors
 2 momentary pushbuttons
 one 10 kilohm potentiometer
 motor
 9V battery
 H-Bridge
 
 Created 13 September 2012
 by Scott Fitzgerald
 Thanks to Federico Vanzati for improvements

 http://arduino.cc/starterKit
 
 This example code is part of the public domain 
 */

const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9;   // connected to pin 1 on the H-bridge
const int directionSwitchPin = 4;  // connected to the switch for direction
const int directionSwitchPin1 = 6; //Connected to the switch1 for direction

// create some variables to hold values from your inputs
//int onOffSwitchState = 0;  // current state of the On/Off switch
//int previousOnOffSwitchState = 0; // previous position of the on/off switch
int directionSwitchState = 0;  // current state of the direction switch
int previousDirectionSwitchState = 0;  // previous state of the direction switch
int directionSwitchState1 = 0;  // current state of the direction switch1
int previousDirectionSwitchState1 = 0;  // previous state of the direction switch1

//int motorEnabled = 0; // Turns the motor on/off
//int motorSpeed = 0; // speed of the motor
int motorDirection = 1; // current direction of the motor

void setup(){
  // intialize the inputs and outputs
  pinMode(directionSwitchPin1, INPUT);
  pinMode(directionSwitchPin, INPUT);
  //pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  //pinMode(enablePin, OUTPUT);

  // pull the enable pin LOW to start
  //digitalWrite(enablePin, LOW);
}

void loop(){
  // read the value of the on/off switch
  //onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);
  
  // read the value of the direction switch
  directionSwitchState = digitalRead(directionSwitchPin);

    // read the value of the direction switch1
  directionSwitchState1 = digitalRead(directionSwitchPin1);
  
  // read the value of the pot and divide by 4 to get 
  // a value that can be used for PWM
  //motorSpeed = analogRead(potPin)/4; 

  // if the on/off button changed state since the last loop()
  //if(onOffSwitchState != previousOnOffSwitchState){
    // change the value of motorEnabled if pressed
   // if(onOffSwitchState == HIGH){
    //  motorEnabled = !motorEnabled;
   // }
//  }

  // if the direction button changed state since the last loop()
  if (directionSwitchState != previousDirectionSwitchState) {
    // change the value of motorDirection if pressed 
    if (directionSwitchState == HIGH) {
      motorDirection = !motorDirection;
     
    }
  }  

    // if the direction button changed state since the last loop()
  if (directionSwitchState1 != previousDirectionSwitchState1) {
    // change the value of motorDirection if pressed 
    if (directionSwitchState1 == HIGH) {
      motorDirection = !motorDirection;
    }
  }  

  // change the direction the motor spins by talking
  // to the control pins on the H-Bridge
  if (motorDirection == 1) {
    digitalWrite(controlPin1, HIGH);
    digitalWrite(controlPin2, LOW);
    
  } 
  else {
    digitalWrite(controlPin1, LOW);
    digitalWrite(controlPin2, HIGH);
    
  }  

  // if the motor is supposed to be on
 // if (motorEnabled == 1) {
    // PWM the enable pin to vary the speed
   // analogWrite(enablePin, motorSpeed);
//  }
 // else { // if the motor is not supposed to be on
    //turn the motor off
   // analogWrite(enablePin, 0);
 // }
  // save the current On/Offswitch state as the previous 
  previousDirectionSwitchState = directionSwitchState;
  // save the current On/Offswitch1 state as the previous 
  previousDirectionSwitchState1 = directionSwitchState1;
  // save the current switch state as the previous
 // previousOnOffSwitchState = onOffSwitchState;
}

The photo is attached here. CLICK LINK FOR SCHEMATIC https://ibb.co/cyagvw

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you wired your limit switches, are you using pull down resisitors.

Thanks Tom... :slight_smile:

Sorry, that picture is no good. It's just a mess, we can't see what is connected to what. Post a schematic. If you use Fritzing, use the schematic view, not the breadboard view.

Sorry guys, as you can tell I'm a rookie. I hope this schematic can help you solve my schematic — ImgBB

Hi,
Thanks for the schematic.


It lookl ike you are using a 9V battery like this;

Its not going to last long at all.

Tom.. :slight_smile:

Thank you for posting it Tom. Yes we have multiple 9V lithium batteries haha

Hi,
Can I suggest you replace all the microswitch debounce checking with this

if sw1 == HIGH drive one direction
if sw2 == HIGH drive the other direction

As you only need to detect a change in the switch from LOW to HIGH, then use it, any contact bounce will not cause the direction to change and the bounce will be well and truly over by the time the other switch is triggered.

simply in psuedo code.

setup

direction = LEFT

loop

if LEFTSWITCH == HIGH
   { direction = RIGHT}

if RIGHTSWITCH == HIGH
  { direction = LEFT }

Simple as that to change your direction.

Hope it helps...

Tom... :slight_smile:

In your photo you are not using the movement of the motor to trigger the switches - for example when the motor moves something all the way left it should start moving all the way to the right.

If you are using the switches for that purpose the code can be different because there will be quite a long time between the left and right switches being pressed. You would just use each switch to change a variable (perhaps called motorDirection) from 'L' to 'R' or vice versa.

If you are just using the micro-switches as push-buttons for a human then you need to debounce the switch action. The simplest way to do that is to leave a short interval (say 50 millisecs) between each time the pair of switches is read. Look at how it is done in Several Things at a Time

...R

Hi,
I don't think debounce is needed, you are using the switches to change the state of a variable.

One switch makes it HIGH, the other makes it LOW, any bounce will just reinforce the HIGH or LOW it triggered.

Tom... :slight_smile:

Sautomatic:
Yes we have multiple 9V lithium batteries haha

Those batteries are ok for smoke alarms where they will last a year or more. But you are going to burn through one every 10 minutes and they are not even rechargeable? Not ha ha. Very bad for the planet.

The picture shows THREE wires soldered to the microswitch. The schematic shows switchs with only TWO poles connected. Show us the real schematic with the microswitches included.

Paul

Paul, we messed up. It should be two. Hope you can still help.

Sautomatic:
Paul, we messed up. It should be two. Hope you can still help.

BUT! which two? The center pin of the switch is always connected to something. The other pins, the end pins, are either NO, normally open, or NC, normally closed. Which one you wired and how it is connected have a large impact on the results you see. That is why I asked for a schematic showing the switches and wiring.

Paul

Hi,
the microswitch terminals should be marked

COM. NC and NO

COM is the common terminal that is switched.

NO is the terminal that you connect to if you want it to close and connect to COM when you press the switch, hence NORMALLY OPEN.

NC is the connected to COM until you press the switch and it is then disconnected, hence NORMALLY CLOSED.

You need to use COM and NO terminals only.

Tom.. :slight_smile: