Programing limit switches

Here is my set up:
Arduino Mega
Adafruit Motor Shields (x2 with independent addresses)
4 steppers, and 3 limit switches (2 wire).
I am currently trying to use the ezButton Library as it has some useful features like denouncing.

I am NOT using any firmware , this set up will NOT be able to use Gcode and will need to be programmed from the base up. So I am taking it one step at a time as I am relatively new to this stuff.

The limits are set to my Arduino Mega pins, 2, 3,4.
I have a buzzer set to pin 5, which I am using , along with the serial monitor, to simulate motor movement of the different axis's using different buzzer frequencies. If I had been using the actual motors, I would have fried several dozen motors by now...

The goal is simple; get a motor to run until the motor runs the apparatus into a limit switch, then shut off the motor. Then proceed to the next axis and do the same. then move to the third axis and repeat. Then Stop.

Later I'll program more things to happen after I reach a Home position, but for now, I am just trying to establish a "home" position where all three switches are "pressed" and the code is waiting for an input to proceed, or otherwise stopped.

#include <ezButton.h>
int buzzer =5;
ezButton button1(2);  // create ezButton object that attach to pin 2;
ezButton button2(3);  // create ezButton object that attach to pin 3;
ezButton button3(4);  // create ezButton object that attach to pin 4;

void setup() {
  Serial.begin(9600);
  button1.setDebounceTime(5); // set debounce time to 50 milliseconds
  button2.setDebounceTime(5); // set debounce time to 50 milliseconds
  button3.setDebounceTime(5); // set debounce time to 50 milliseconds
  pinMode(buzzer,OUTPUT);
}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first

  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();

  
  
delay (5);
//PAxis test
tone(buzzer,1000);
  if(button1.isReleased()){
    Serial.println("The Pbutton is released");
     }
 
 if(button1.isPressed()){
    Serial.println("The Pbutton is pressed");
noTone (buzzer);
}
//XAxis test
tone(buzzer,1200);
  if(button2.isReleased()){
    Serial.println("The Xbutton is released");
    }
 
 if(button2.isPressed()){
    Serial.println("The Xbutton is pressed");
noTone (buzzer);
}
//YAxis test
tone(buzzer,1400);
  if(button3.isReleased()){
    Serial.println("The Ybutton is released");
    }
 
 if(button3.isPressed()){
    Serial.println("The Ybutton is pressed");
noTone (buzzer);
}
}

Right now, it is running all three buzzer frequencies simultaneously, and it will not shut any of them off if I hold down a limit switch. What did I miss?

For limit switches, why do you care about bouncing contacts? Are you going to continue to try to move into the switch, over and over once they are hit. Probably want to immediately stop or reverse direction, or what ever. Just do it on the FIRST hit on the switch, subsequent contacts will never be noticed.
Paul

I was a little worried about chatter... that said, I discovered a new issue. I wrote a very simple code:

int LimitP= 2;

int buzzer = 5;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int p=digitalRead(LimitP);
Serial.println (p);
delay(5);
}

the serial monitor displays a string of "1"s. then if I hit the limit, it shows "0"s so far, so good. then, if I release the switch, it still shows zeros... do I need to reverse the wires or something here?

So very, very, very, much depends on how you have the switch wired to 5 volts and to ground.

This may be the root of the issue as it is returning really odd results. It is actually a 3 wire switch, and I have the middle wire (NO) connected to pin 2 and the left wire (C) connected to ground.

But you don't have pin 2 set to input pullup, so there is NEVER any way for the pin to be held high.

FYI, use the S3 wiring (S2 in noisy environments) for a switch, look for a LOW for switch closed.

Use INPUT_PULLUP

Ahhhhhh! Thank you! That solved it. I also ditched the library I had been using and just stuck with the standard arduino commands and now I've actually got all three switches working. Again, thanks I was banging my head against the wall.

Good for you! Now on to the next problem!!!

oh yes, there will be plenty more, I have no doubt.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.