Reversing motor with limit switch and trigger problem SOLVED

UPDATE: PROBLEM SOLVED
Please check my last post there i have explained a little bit more about how i got it to work.

ORIGINAL POST:
Hi, i am using a generic dc motor with an H bridge, a button as a trigger and 2 limit switches to stop the motor when it reaches either of the switches. i am using bounce2 library and input pullups so i think the high and low states are inverted. so i am using HIGH when its not being pressed and low when it is being pressed.

What i want to do:
assuming motors arm is at one limit switch, and stopped for the time being. when the trigger is pressed with button, only then i want the arduino to calculate which limit switch is pressed so that it can go opposite to that limit switch and keep going until it touches the other limit switch, stop, and wait for the trigger again.

I am going to have a sensor reading data continuously so cant use delay(); but i can spare a few seconds for the motor to perform this action if its impossible to do otherwise.

My Problem:
The code works kinda. when i press the trigger, it determines which way its at but this works for one limit switch only. i dont know why i that is. since i have copied the same method for the other limit switch.
It always registers the limit switches. its tested and no fault there.
but sometimes it works sometimes it doesn't. for example. if i keep pressing it, it will carry out the function sometimes and sometimes it wont.

when the function does work,the motor does not stop when the other limit switch is pressed. it just keeps going.

i know there are too many problems but after having tried for about 1 week. i am tired. and need some guidance.

i attached the current code i am working with. i know i must have mutilated the code there. and i am sorry for that. im a newbie. :slight_smile:

#include <Bounce2.h>
int function = 0;
bool triggerStart = false;   //trigger to start motor
bool triggerStop = false;   // trigger to stop motor
bool goRight = false;
bool goLeft = false;
bool isTime = false;
 
const byte m1 = 4;
const byte m2 = 3;
const byte s1 = 7;
const byte s2 = 8;
const byte trigger = 11;
bool s1State;
bool s2State;
bool triggerState;
bool prevS1State = 0;
bool prevS2State = 0;
bool startMotor = 0;
 
bool wasGoingLeft;
bool wasGoingRight;
bool motorStopped;
 
bool high = LOW; //since switches are inverted. when its low. it will be high
bool low = HIGH;
 
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();  //trigger for motor
 
void setup() {
  // put your setup code here, to run once
 
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
 
  pinMode(s1, INPUT_PULLUP);
  debouncer1.attach(s1);
  debouncer1.interval(25);
 
  pinMode(s2, INPUT_PULLUP);
  debouncer2.attach(s2);
  debouncer2.interval(25);
 
  pinMode(trigger, INPUT_PULLUP);
  debouncer3.attach(trigger);
  debouncer3.interval(25);
 
  Serial.begin(9600);
  //assuming motor is already one one junction
   Serial.println("Incubator starting. motor is now at rest");
   
  delay(2000);
 
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  bool m1State = digitalRead(m1);
  bool m2State = digitalRead(m2);
 
  debouncer1.update();
  debouncer2.update();
  debouncer3.update();
 
  triggerState = debouncer3.read(); //trigger
  s1State = debouncer1.read();   //switch are input pullup. high state is zero
  s2State = debouncer2.read();
  //startMotor = 0;   //turn off this trigger in order to not loop back.
 
 
 if(debouncer3.fell()) //when trigger button is pressed
   {
    startMotor = true;
    Serial.println("button to trigger start motor has been presssed");
   }
 
 if(startMotor == 1)
   {
    switch(function)
    {
    case 0:   //checks if time
      isTime = 1;
      function = 1;
      Serial.println("At case 0. checking if its time");
     
 
    break;
 
    case 1:   //its time so its going to determine where it is
    if(isTime == 1)
    {
      Serial.println("trigger is pressed proceeding to case 1");
      if(s1State == 1 && s2State == 0)   //if one of the switch is high. label is left
      {
       
        goRight =1;   //its left right now. so need to go right
        goLeft = 0;
        Serial.println("At case 1. determind motor is towards left and going right now");
        function = 2;
      }
      if(s1State == 0 && s2State == 1)  //if one of the switch is high. label is right
      {
        goLeft = 1;    //is at right. go left
        goRight = 0;
        Serial.println("At case 1. determind motor is towards right and going left now");
        function = 2;
      }
    }
    break;
 
    case 2:    //once direction to go is determined. go that way
    if(goRight== 1)   //at s1  or left
    {
      Serial.println("At case 2. Going Right");
      digitalWrite(m1, HIGH);
      digitalWrite(m2, LOW);
      function = 3;
     
    }
    if(goLeft == 1)    //at s2 or right
    {
      Serial.println("At case 2. Going left");
      digitalWrite(m1, LOW);
      digitalWrite(m2, HIGH);
      function = 3;
    }
    break;
 
 
 
 
    case 3:    //going to check if the motor has reached the desired switch yet
    if(goRight == 1)   //if motor is going towards right so we are going to monitor the right switch only
    {
      if(s2State == 1)     //go right until s2 has become high
      {
        digitalWrite(m1, LOW);
        digitalWrite(m2, LOW);
        Serial.println("case 3. Motor reached right. motor stopping");
        //stop motor
        function = 4;
      }
    }
 
    if(goLeft == 1)  //if motor is going left
    {
      if(s1State == 0) //check to see if left switch is touched
      {
        digitalWrite(m1, LOW);
        digitalWrite(m2, LOW);
        Serial.println("At case 3.motor has reached left. Motor stopping");
       
        function = 4;
       
      }
    }
    break;
 
    case 4:
    Serial.print("At case 4.motor should have stopped. pressing the trigger will repeat the cycle");
    Serial.println("exiting the switch case and waiting for the trigger");
    isTime = 0;  
    function = 0;
    break;
  }
}
}

MY CURRENT CODE:
attached in last post in the thread. couldn't attach it here.

Dude, Waaaaaay to many variables. If goLeft is true, then !goLeft would be go right, etc. If you take the time to name your variables better (s1 vs. limitSwithLeftPin) then the code reads a lot better.

Here is a simplified version that I think does what you want...

#include <Bounce2.h>

//int function = 0;
//bool triggerStart = false;   //trigger to start motor
//bool triggerStop = false;   // trigger to stop motor
//bool goRight = false;
//bool goLeft = false;
//bool isTime = false;

const byte motorRightPin = 4;
const byte motorLeftPin = 3;
const byte limitSwitchLeftPin = 7;
const byte limitSwitchRightPin = 8;
const byte triggerPin = 11;

//bool limitSwitchLeftState;
//bool limitSwitchRightState;
//bool triggerState;
//bool prevS1State = 0;
//bool prevS2State = 0;
//bool startMotor = 0;

//bool wasGoingLeft;
//bool wasGoingRight;
//bool motorStopped;

//bool high = LOW; //since switches are inverted. when its low. it will be high
//bool low = HIGH;

Bounce limitSwitchLeft = Bounce();
Bounce limitSwitchRight = Bounce();
Bounce triggerButton = Bounce();  //trigger for motor

void setup() {
  // put your setup code here, to run once

  pinMode(motorRightPin, OUTPUT);
  pinMode(motorLeftPin, OUTPUT);
  digitalWrite(motorRightPin, LOW);
  digitalWrite(motorLeftPin, LOW);

  limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  limitSwitchLeft.interval(25);

  limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  limitSwitchRight.interval(25);

  triggerButton.attach(triggerPin, INPUT_PULLUP);
  triggerButton.interval(25);

  Serial.begin(9600);
  //assuming motor is already one one junction
  Serial.println("Incubator starting. motor is now at rest");

  delay(2000);
}

enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };

int currentState = STATE_IDLE;

void loop() {
  // put your main code here, to run repeatedly:

  limitSwitchLeft.update();
  limitSwitchRight.update();
  triggerButton.update();

  bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  bool limitSwitchRightState = limitSwitchRight.read();

  switch (currentState) {
    case STATE_IDLE:
      // motor is off, waiting for trigger button to be pressed
      if (triggerButton.fell())
      {
        Serial.println("button to trigger start motor has been presssed");
        // figure out which way to move
        if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
        {
          Serial.println("Moving right now");
          digitalWrite(motorRightPin, HIGH);
          digitalWrite(motorLeftPin, LOW);
          currentState = STATE_MOVING_RIGHT;
        }
        else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
        {
          Serial.println("Moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else if (limitSwitchLeftState == HIGH && limitSwitchRightState == HIGH)
        {
          // neither limit switch is tripped, arbitrarily move left
          Serial.println("No limit switches detected, moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else
        {
          Serial.println("Both limit switches detected, program halted");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, LOW);
          while (1); // loop forever
        }
      }
      break;

    case STATE_MOVING_RIGHT:
      // moving right so only check right limit switch
      if (limitSwitchRightState == LOW)
      {
        Serial.println("Motor reached right. motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;

    case STATE_MOVING_LEFT:
      // moving left so only check left limit switch

      if (limitSwitchLeftState == LOW)
      {
        Serial.println("Motor has reached left. Motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
  }
}

First of all Thank you so much for taking the time to reply. you basically wrote the whole code for me and i respect and appreciate that. the code you wrote was so well written that it compiled without any tweaking. you are the man!

anyway. so upon trying the code i faced the following issue;

the reversal logic works great. everything is fine. but when i let go off the switch (simulating the actual motion since its first going to leave the switch for a few seconds to get to the other switch all the while keeping the trigger btn pressed) the motor stops instantly.

i am currently looking at the code and see if i can come up with anything. but i will share my findings so that someone could follow or pitch in if they want.

These are the console prints i am getting.

log no#1 on pressing switch RIGHT

Incubator starting. motor is now at rest
button to trigger start motor has been presssed
Moving left now after letting go of the same button almost instantly stops
Motor has reached left. Motor stopping

log no#2 on pressing switch LEFT

Incubator starting. motor is now at rest
button to trigger start motor has been presssed
Moving right now after letting go of the same button that was pressed
Motor reached right. motor stopping

The code you wrote looks completely fine to me. and it should work. however its not. i thought switches might not be working right or debouncing right. so i increased the debounce interval to 100ms but its the same.

I thought switches might be faulty. so i changed the switches. still same result.
It just stops as soon as i let go of the switch.

How are your limit switches wired? One wire to input pin, the other to ? Are they normally open and close when activated or vice versa? How is the trigger switch wired?

all the switches are wired in the following way;

one pin to ground the other to digitalinput/output pin and using inputpullup in the code

I think a state machine would work well here.

Your tutorial was definitely helpful and helped clear some concepts. but if you look at the code i think i am using state machine. here is the current code.

#include <Bounce2.h>
 
//int function = 0;
//bool triggerStart = false;   //trigger to start motor
//bool triggerStop = false;   // trigger to stop motor
//bool goRight = false;
//bool goLeft = false;
//bool isTime = false;
 
const byte motorRightPin = 4;
const byte motorLeftPin = 3;
const byte limitSwitchLeftPin = 7;
const byte limitSwitchRightPin = 8;
const byte triggerPin = 11;
 
//bool limitSwitchLeftState;
//bool limitSwitchRightState;
//bool triggerState;
//bool prevS1State = 0;
//bool prevS2State = 0;
//bool startMotor = 0;
 
//bool wasGoingLeft;
//bool wasGoingRight;
//bool motorStopped;
 
//bool high = LOW; //since switches are inverted. when its low. it will be high
//bool low = HIGH;
 
Bounce limitSwitchLeft = Bounce();
Bounce limitSwitchRight = Bounce();
Bounce triggerButton = Bounce();  //trigger for motor
 
void setup() {
  // put your setup code here, to run once
 
  pinMode(motorRightPin, OUTPUT);
  pinMode(motorLeftPin, OUTPUT);
  digitalWrite(motorRightPin, LOW);
  digitalWrite(motorLeftPin, LOW);
 
  limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  limitSwitchLeft.interval(25);
 
  limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  limitSwitchRight.interval(25);
 
  triggerButton.attach(triggerPin, INPUT_PULLUP);
  triggerButton.interval(25);
 
  Serial.begin(9600);
  //assuming motor is already one one junction
  Serial.println("Incubator starting. motor is now at rest");
 
  delay(2000);
}
 
enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
 
int currentState = STATE_IDLE;
 
void loop() {
  // put your main code here, to run repeatedly:
 
  limitSwitchLeft.update();
  limitSwitchRight.update();
  triggerButton.update();
 
  bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  bool limitSwitchRightState = limitSwitchRight.read();
 
  switch (currentState) {
    case STATE_IDLE:
      // motor is off, waiting for trigger button to be pressed
      if (triggerButton.fell())
      {
        Serial.println("button to trigger start motor has been presssed");
        // figure out which way to move
        if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
        {
          Serial.println("Moving right now");
          digitalWrite(motorRightPin, HIGH);
          digitalWrite(motorLeftPin, LOW);
          currentState = STATE_MOVING_RIGHT;
        }
        else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
        {
          Serial.println("Moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else if (limitSwitchLeftState == HIGH && limitSwitchRightState == HIGH)
        {
          // neither limit switch is tripped, arbitrarily move left
          Serial.println("No limit switches detected, moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else
        {
          Serial.println("Both limit switches detected, program halted");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, LOW);
          while (1); // loop forever
        }
      }
      break;
 
    case STATE_MOVING_RIGHT:
      // moving right so only check right limit switch
      if (limitSwitchRightState == LOW)
      {
        Serial.println("Motor reached right. motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
 
    case STATE_MOVING_LEFT:
      // moving left so only check left limit switch
 
      if (limitSwitchLeftState == LOW)
      {
        Serial.println("Motor has reached left. Motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
  }
}

Having said that. i am looking at your post right now. and i think i can salvage some things from it.

UPDATE: PROBLEM SOLVED

when i added if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)

to both state_moving_left and right. it solved the problem.

debug attempt 1: i did some debugging. i thought it was not able to read switch states. so i put them inside if statements. that didnt work

debug attempt 2: i replaced ' if'with a 'while'. while that solved the motor stopping when letting go of the same switch problem yet it was not able to stop when the other switch was pressed.

debug attempt 3: i added this if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW) in the state moving left and right. and it worked.

however i dont even know why it was not working. i thought maybe when the switch got released it was momentarily pressed because of bad debounce. but that couldnt be the case cause i increased the debounce interval. and i also changed the switch.

Special thanks to @blh64 for simplifying the code and having the patience to go through my code.

blh64:
Here is a simplified version that I think does what you want...

Thanks to everyone who pitched in. this is what makes this community so great. forever thankful guys!

this is the current code right now. ill update the original post as well

#include <Bounce2.h>
 
//int function = 0;
//bool triggerStart = false;   //trigger to start motor
//bool triggerStop = false;   // trigger to stop motor
//bool goRight = false;
//bool goLeft = false;
//bool isTime = false;
 
const byte motorRightPin = 4;
const byte motorLeftPin = 3;
const byte limitSwitchLeftPin = 7;
const byte limitSwitchRightPin = 8;
const byte triggerPin = 11;
 
//bool limitSwitchLeftState;
//bool limitSwitchRightState;
//bool triggerState;
//bool prevS1State = 0;
//bool prevS2State = 0;
//bool startMotor = 0;
 
//bool wasGoingLeft;
//bool wasGoingRight;
//bool motorStopped;
 
//bool high = LOW; //since switches are inverted. when its low. it will be high
//bool low = HIGH;
 
Bounce limitSwitchLeft = Bounce();
Bounce limitSwitchRight = Bounce();
Bounce triggerButton = Bounce();  //trigger for motor
 
void setup() {
  // put your setup code here, to run once
 
  pinMode(motorRightPin, OUTPUT);
  pinMode(motorLeftPin, OUTPUT);
  digitalWrite(motorRightPin, LOW);
  digitalWrite(motorLeftPin, LOW);
 
  limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  limitSwitchLeft.interval(50);
 
  limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  limitSwitchRight.interval(50);
 
  triggerButton.attach(triggerPin, INPUT_PULLUP);
  triggerButton.interval(25);
 
  Serial.begin(9600);
  //assuming motor is already one one junction
  Serial.println("Incubator starting. motor is now at rest");
 
  delay(2000);
}
 
enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };
 
int currentState = STATE_IDLE;
 
void loop() {
  // put your main code here, to run repeatedly:
 
  limitSwitchLeft.update();
  limitSwitchRight.update();
  triggerButton.update();
 
  bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  bool limitSwitchRightState = limitSwitchRight.read();
  bool startMotor = false;
  
  switch (currentState) {
    case STATE_IDLE:
      // motor is off, waiting for trigger button to be pressed
      if (triggerButton.fell())
      {
        Serial.println("button to trigger start motor has been presssed");
        // figure out which way to move
        if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
        {
          Serial.println("Moving right now");
          digitalWrite(motorRightPin, HIGH);
          digitalWrite(motorLeftPin, LOW);
          currentState = STATE_MOVING_RIGHT;
        }
        else if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
        {
          Serial.println("Moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else if (limitSwitchLeftState == HIGH && limitSwitchRightState == HIGH)
        {
          // neither limit switch is tripped, arbitrarily move left
          Serial.println("No limit switches detected, moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
         }
        
        else
        {
          Serial.println("Both limit switches detected, program halted");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, LOW);
          while (1); // loop forever
        }
      }
      break;
 
    case STATE_MOVING_RIGHT:
      // moving right so only check right limit switch
      if (limitSwitchRightState == HIGH && limitSwitchLeftState == LOW)   
      {
        Serial.println("Motor reached right. motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
 
    case STATE_MOVING_LEFT:
      // moving left so only check left limit switch
 
      if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
      {
        Serial.println("Motor has reached left. Motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
  }
}

The logic was reversed when figuring out which way to move (If left limit switch is LOW -> move right)

#include <Bounce2.h>

const byte motorRightPin = 4;
const byte motorLeftPin = 3;
const byte limitSwitchLeftPin = 7;
const byte limitSwitchRightPin = 8;
const byte triggerPin = 11;

Bounce limitSwitchLeft = Bounce();
Bounce limitSwitchRight = Bounce();
Bounce triggerButton = Bounce();  //trigger for motor

void setup() {
  // put your setup code here, to run once

  pinMode(motorRightPin, OUTPUT);
  pinMode(motorLeftPin, OUTPUT);
  digitalWrite(motorRightPin, LOW);
  digitalWrite(motorLeftPin, LOW);

  limitSwitchLeft.attach(limitSwitchLeftPin, INPUT_PULLUP);
  limitSwitchLeft.interval(25);

  limitSwitchRight.attach(limitSwitchRightPin, INPUT_PULLUP);
  limitSwitchRight.interval(25);

  triggerButton.attach(triggerPin, INPUT_PULLUP);
  triggerButton.interval(25);

  Serial.begin(9600);
  //assuming motor is already one one junction
  Serial.println("Incubator starting. motor is now at rest");

  delay(2000);
}

enum states { STATE_IDLE, STATE_MOVING_RIGHT, STATE_MOVING_LEFT };

int currentState = STATE_IDLE;

void loop() {
  // put your main code here, to run repeatedly:

  limitSwitchLeft.update();
  limitSwitchRight.update();
  triggerButton.update();

  bool limitSwitchLeftState = limitSwitchLeft.read();   //switch are input pullup. high state is zero
  bool limitSwitchRightState = limitSwitchRight.read();

  switch (currentState) {
    case STATE_IDLE:
      // motor is off, waiting for trigger button to be pressed
      if (triggerButton.fell())
      {
        Serial.println("button to trigger start motor has been presssed");
        // figure out which way to move
        if (limitSwitchLeftState == LOW && limitSwitchRightState == HIGH)
        {
          Serial.println("Moving right now");
          digitalWrite(motorRightPin, HIGH);
          digitalWrite(motorLeftPin, LOW);
          currentState = STATE_MOVING_RIGHT;
        }
        else if (limitSwitchLeftState == HIGH && limitSwitchRightState == LOW)
        {
          Serial.println("Moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else if (limitSwitchLeftState == HIGH && limitSwitchRightState == HIGH)
        {
          // neither limit switch is tripped, arbitrarily move left
          Serial.println("No limit switches detected, moving left now");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, HIGH);
          currentState = STATE_MOVING_LEFT;
        }
        else
        {
          Serial.println("Both limit switches detected, program halted");
          digitalWrite(motorRightPin, LOW);
          digitalWrite(motorLeftPin, LOW);
          while (1); // loop forever
        }
      }
      break;

    case STATE_MOVING_RIGHT:
      // moving right so only check right limit switch
      if (limitSwitchRightState == LOW)
      {
        Serial.println("Motor reached right. motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;

    case STATE_MOVING_LEFT:
      // moving left so only check left limit switch

      if (limitSwitchLeftState == LOW)
      {
        Serial.println("Motor has reached left. Motor stopping");
        digitalWrite(motorRightPin, LOW);
        digitalWrite(motorLeftPin, LOW);
        currentState = STATE_IDLE;
      }
      break;
  }
}

i see. so i guess using the debounce2 library complicated the code. and debounce library is to be blamed :smiley:

manhoosbilli1:
i see. so i guess using the debounce2 library complicated the code. and debounce library is to be blamed :smiley:

No, not really. It was looking at someone else's code and renaming a lot of the variables and not being able to actually test it.

Having switches/buttons go LOW when activated is quite common.

ah! i see now. thanks for clearing it up tho.