1

I've tried this code below but it doesn't stop it just rapidly switch direction that it looks like it's trembling I think because it always reads the void startF/R thats why it's trembling .

I need help on how to completely stop the stepper if it reads both sensor LOW

if (sensLval== LOW || sensRval == LOW){
     steppStop(); 
}

this is my void loop code below

    void startF()
  {
    motor1.step(10, FORWARD,SINGLE);
    
  }

    void startR()
  {
    motor1.step(10, BACKWARD,SINGLE);
    
  }

    void steppStop(){
  motor1.step(0, BACKWARD, SINGLE);
 
 motor1.step(0, FORWARD, SINGLE);
}

 
    void loop() {
  sensRval=digitalRead(sensR);
  
sensLval=digitalRead(sensL);


  if (sensLval == LOW)

{
    startF();
  }
  
if (sensLval== LOW && sensRval == LOW)

{
     steppStop();
  }

  if (sensRval == LOW)

{
    startR();
  }
  
  
  
}
0
1

Isolate your conditions.

You currently check for:

  • Both sensors LOW at the same time and
  • Either sensor LOW on its own

If both are low—don't check for either being LOW (which triggers startX.)

if (sensLval == LOW && sensRval == LOW) {
    steppStop();
} else if (sensLval == LOW) {
    startF();
} else if (sensRval == LOW) {
    startR();
}

There may be other considerations as well, but this is enough to get past that issue.


Unrelated, but consistent indentation goes a long way towards code readability and helping others be able to reason about code in general. When posting code it's a good idea to format your code as code, described here and when you are editing the question.

1
  • 1
    Im sorry bout the format , im still learning but i'll be sure to do well next time. The code works now thanks alot! I ddnt know u could code it that way. That's cool!
    – Exes
    Jun 8 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.