motor stuck in the micro limit switch

Dear all,

I use two micro limit switches (i.e., counts) to control directions of two motors.

I’ve verified the wiring and electronic components one by one and they are all good.

The problem is that one motor SOMETIMES stuck in one of limit switches.

The normal operation should be one motor triggers the limit switch and then changes the motor direction.

Anything wrong in my code? The code SOMETIMES works fine … I have no idea how to make it right.

Please take a look on my code. Thanks a lot.

#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;

// ----------DECLARED CONSTANTS--------------

const int SwitchPin_left  = 5;  // pin 5 --- Switch on left
const int SwitchPin_right = 6;  // pin 6 --- Switch on right

const int LiDARForward    = 8;  // pin 8  --- LiDAR motor (+)  wire
const int LiDARBackward   = 11; // pin 11 --- LiDAR motor (-) wire
const int LiDARspeed      = 9;  // pin 9  --- LiDAR motor speed signal

const int RailForward     = 12; // pin 12 --- Rail motor (+) wire
const int RailBackward    = 13; // pin 13 --- Rail motor (-) black
const int Railspeed       = 10; // pin 10 --- Rail motor speed signal

// ----------DECLARED VARIABLES--------------

int SwitchCounter_right   = 0;     // number counter for right switch
int SwitchCounter_left    = 0;     // number counter for left switch
int SwitchState_right     = 0;     // current state of right switch
int SwitchState_left      = 0;     // current state of left switch
int LastSwitchState_right = 0;     // previous state of right switch
int LastSwitchState_left  = 0;     // previous state of left switch

byte motorA_state_1 = LOW;         // used to record whether the motots are on or off
byte motorA_state_2 = LOW;         // LOW = off
byte motorB_state_1 = LOW;             
byte motorB_state_2 = LOW;             

unsigned int currentMillis = 0;        // stores the value of millis() in each iteration of loop()
long Timer_1 = 0;                      // will store last time motorA backward start was updated
long Timer_2 = 0;                      // will store last time motorA backward start was updated
long Timer_3 = 0;                      // will store last time motorA backward start was updated
long Timer_4 = 0;                      // will store last time motorA backward start was updated
long Timer_5 = 0;                      // will store last time motorA backward start was updated
long Timer_6 = 0;                      // will store last time motorA backward start was updated
long Timer_7 = 0;                      // will store last time motorA backward start was updated
long Timer_8 = 0;                      // will store last time motorA backward start was updated
long Timer_9 = 0;                      // will store last time motorA backward start was updated
long Timer_10 = 0;                     // will store last time motorA backward start was updated

// LiDAR
unsigned long pulse_width;

//------------INITIALIZATION--------------
void setup()
{
Serial.begin(9600);
delay(10);
myLidarLite.begin();
pinMode(SwitchPin_right, INPUT);   // initialize switch pin as input
pinMode(SwitchPin_left, INPUT);

pinMode(LiDARForward, OUTPUT);     // initialize motor pins as output
pinMode(LiDARBackward, OUTPUT);
pinMode(LiDARspeed, OUTPUT);

pinMode(RailForward, OUTPUT);
pinMode(RailBackward, OUTPUT);
pinMode(Railspeed, OUTPUT);

pinMode(2, OUTPUT);               // pin 2 -- LiDAR trigger
pinMode(3, INPUT);                // pin 3 -- LiDAR monitor
digitalWrite(2, LOW);             // pin 2 -- Set LiDAR trigger LOW for continuous mode
}


//------------MAIN PROGRAM LOOPING-------------
void loop()
{
  SwitchState_right = digitalRead(SwitchPin_right);  // read switch state
  SwitchState_left = digitalRead(SwitchPin_left);
  if (SwitchState_right != LastSwitchState_right) {   // compare state to its previous
    if (SwitchState_right == HIGH) {                 // if the state has changed, increment the counter
        SwitchCounter_right++;
        delay(10);
    }
      LastSwitchState_right = SwitchState_right;
  }

  if (SwitchState_left != LastSwitchState_left) {  // compare state to its previous
    if (SwitchState_left == HIGH) {                
        SwitchCounter_left++;                        // if the state has changed, increment the counter
        delay(10);
    }
      LastSwitchState_left = SwitchState_left;
  }  
     
  if (SwitchCounter_right == 1 && SwitchCounter_left == 0) {
      LiDAR_goLeft();
      Timer_1 = millis();
  }


      
  if (SwitchCounter_right == 1 && SwitchCounter_left == 1) {
     if (millis() - Timer_1 <= 1000) {
         Rail_goBackward();
      }
      else {
            LiDAR_goRight();
            Timer_2 = millis();
    }      
  }


  if (SwitchCounter_right == 2 && SwitchCounter_left == 1) {
     if (millis() - Timer_2 <= 1000) {
         Rail_goBackward();
      }
      else {
            LiDAR_goLeft();
            Timer_3 = millis();
    }
  }



  if (SwitchCounter_right == 2 && SwitchCounter_left == 2) {
     if (millis() - Timer_3 <= 1000) {
         Rail_goBackward();
      }
      else {
            LiDAR_goRight();
            Timer_4 = millis();
    }
  }
   




  if (SwitchCounter_right == 3 && SwitchCounter_left == 2) {
     if (millis() - Timer_4 <= 1000) {
         Rail_goBackward();
      }
      else {
            LiDAR_goLeft();
            Timer_5 = millis();
    }
  }



  if (SwitchCounter_right == 3 && SwitchCounter_left == 3) {
     if (millis() - Timer_5 <= 1000) {
         Rail_goBackward();
      }
      else {
            LiDAR_goRight();
            Timer_6 = millis();
    }
  }


  if (SwitchCounter_right == 4 && SwitchCounter_left == 3) {
     if (millis() - Timer_6 <= 6100) {
         Rail_goForward();
      }
      else {
            Stop();
    }
  } 

}


//----- "Sub-rutine" Voids called by the main loop

void LiDAR_goRight()
{
//delay(1);
pulse_width = pulseIn(3, HIGH);    // Count how long the pulse is high in microseconds
  if(pulse_width != 0){            // If we get a reading that isn't zero, let's print it
    pulse_width = pulse_width;     // 10usec = 1 cm of distance for LIDAR-Lite
    Serial.print(pulse_width);     // Print the distance
    Serial.print('\n');                    
  }
analogWrite(LiDARspeed,100);       // Enable LiDAR motor by setting speed
digitalWrite(Railspeed,LOW);
digitalWrite(LiDARBackward,LOW);   // Drives LOW outputs down first to avoid damage
digitalWrite(RailForward,LOW);     // Drives LOW outputs down first to avoid damage
digitalWrite(RailBackward,LOW);    // Drives LOW outputs down first to avoid damage
digitalWrite(LiDARForward,HIGH);   // Drives LiDAR to go right
}

void LiDAR_goLeft()
{
//delay(1); 
pulse_width = pulseIn(3, HIGH);    // Count how long the pulse is high in microseconds
  if(pulse_width != 0){            // If we get a reading that isn't zero, let's print it
    pulse_width = pulse_width;     // 10usec = 1 cm of distance for LIDAR-Lite
    Serial.print(pulse_width);     // Print the distance
    Serial.print('\n');                    
  }  
analogWrite(LiDARspeed,100);        
digitalWrite(Railspeed,LOW);
digitalWrite(LiDARForward,LOW);    
digitalWrite(RailForward,LOW);     
digitalWrite(RailBackward,LOW);    
digitalWrite(LiDARBackward,HIGH);   // Drives LiDAR to go left
}

void Rail_goBackward()
{
//delay(1);  
analogWrite(Railspeed,255);
digitalWrite(LiDARspeed,LOW);
digitalWrite(LiDARForward,LOW);
digitalWrite(LiDARBackward,LOW);
digitalWrite(RailForward,LOW);
digitalWrite(RailBackward,HIGH);    // Drives Rail to go backward
}

void Rail_goForward()
{
//delay(1);  
analogWrite(Railspeed,255);
digitalWrite(LiDARspeed,LOW);
digitalWrite(LiDARForward,LOW);
digitalWrite(LiDARBackward,LOW);
digitalWrite(RailBackward,LOW);
digitalWrite(RailForward,HIGH);    // Drives Rail to go forward
}

void Stop()  // Sets speed pins to LOW disabling both motors
{
//delay(1);  
digitalWrite(LiDARspeed,LOW);
digitalWrite(Railspeed,LOW);
}
pinMode(SwitchPin_right, INPUT);   // initialize switch pin as input
pinMode(SwitchPin_left, INPUT);

You are not using the internal pullup resistors. So, how are the external resistors and switches wired?

    pulse_width = pulse_width;     // 10usec = 1 cm of distance for LIDAR-Lite

The comment has nothing to do with the useless code.

What is connected to pin 3?

What are all the timers for?

Have you never heard of arrays?

PaulS:

pinMode(SwitchPin_right, INPUT);   // initialize switch pin as input

pinMode(SwitchPin_left, INPUT);



You are not using the internal pullup resistors. So, how are the external resistors and switches wired?

Ans.: I forget how it wired and resistors were wrapped by heat shrink last year. It was tested using the Arduino code and it worked fine. 




pulse_width = pulse_width;    // 10usec = 1 cm of distance for LIDAR-Lite



The comment has nothing to do with the useless code. What is connected to pin 3?

Ans.: Pin 3 is connected with one pin of a LiDAR sensor using PWM mode. 

What are all the timers for? Have you never heard of arrays?

Al the timers were used to determine how many round trip of LiDAR scan. This section keeps updating a lot, so I think that it makes me earlier to modify it. I agree I should use array instead. 


Should I worry about adding delay() to get the count of two micro switches? Should I add delay() to each subroutines?

Hi,

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

Thanks.. Tom.. :slight_smile: