I must still be a newbie... button & mega ramps1.4 problem

Hi all,

I thought this was an easy one, but this button has me stumped.

using mega 2560 + ramps1.4

I'm trying to get a two prong tactile button on a breadboard to activate a function called engage(), which turns a nema17 180deg.

So, I placed the tactile button between pin A9/D63 & grnd, named it 'b1'. I've tried both A9 & 63 for pins, plus a few others on the AUX1, 2 & 3. The following happens:

  • the y motor seems to be revving but it doesn't move & the heat sink gets hot
  • if I touch the 63 pin with a wire without even connecting it to anything the sequence begins
  • when pressing the button nothing happens, it seams the button isn't even recognised
  • when I remove the 'if statement' to execute the engage() function the motor sequence is good

I was hoping to just create separate sequences and press a separate button for each to call the function but no go.

Any help much appreciated.... here is the code

  // Define Motor Pins
    const int M1X_stepPin = 54; 
    const int M1X_dirPin = 55;
    const int M1X_enablePin = 38;
    const int M1X_optoMinSen = 3;
    const int M2Y_stepPin = 60; 
    const int M2Y_dirPin = 61;
    const int M2Y_enablePin = 56;
    const int M2Y_optoMinSen = 14;
    const int M3Z_stepPin = 46; 
    const int M3Z_dirPin = 48;
    const int M3Z_enablePin = 62;
    const int M3Z_optoMinSen = 18;
    const int M5E1_stepPin = 36; 
    const int M5E1_dirPin = 34;
    const int M5E1_enablePin = 30;     
    
    // Define Button Pins
    const int b1 = 63;
    const int b2 = 58;
    const int b3 = 59;

    // Define Air Solenoid Pins
    int A1Sol = 57;
    //int A1Sen = ##;

    //M1X = LeverMotor
    float currentM1XAngle = 0;
    int M1XAngle = 0;
    float stepPerM1XAngle = 1.8; // full step = 1.8
    int M1Xnumstep;
    //M2Y = CoreMotor
    float currentM2YAngle = 0;
    int M2YAngle = 0;
    float stepPerM2YAngle = 1.8;
    int M2Ynumstep;
    //M5E1 = RollMotor
    float currentM5E1Angle = 0;
    int M5E1Angle = 0;
    float stepPerM5E1Angle = 1.8;
    int M5E1numstep;   

    // Bring in the functions
    void Engage();
    void Disengage();
    void RemovePape();
            
    void setup() {
      Serial.begin(9600);
      
      // Set Motor Pins
      pinMode(M1X_stepPin,OUTPUT); 
      pinMode(M1X_dirPin,OUTPUT);
      pinMode(M1X_enablePin, OUTPUT); 
      pinMode(M1X_optoMinSen, INPUT);
      digitalWrite(M1X_enablePin, LOW);
      pinMode(M2Y_stepPin,OUTPUT); 
      pinMode(M2Y_dirPin,OUTPUT);
      pinMode(M2Y_enablePin, OUTPUT); 
      pinMode(M2Y_optoMinSen, INPUT);
      digitalWrite(M2Y_enablePin, LOW);
      pinMode(M3Z_stepPin,OUTPUT); 
      pinMode(M3Z_dirPin,OUTPUT);
      pinMode(M3Z_enablePin, OUTPUT); 
      pinMode(M3Z_optoMinSen, INPUT);
      digitalWrite(M3Z_enablePin, LOW);
      pinMode(M5E1_stepPin,OUTPUT); 
      pinMode(M5E1_dirPin,OUTPUT);
      pinMode(M5E1_enablePin, OUTPUT);
      
      // Set Solenoid Pins
      pinMode(A1Sol, OUTPUT);
      //pinMode(A1Sen, INPUT);
      
      // Set Button pins
      pinMode(b1, INPUT);
      pinMode(b2, INPUT);
      pinMode(b3, INPUT);
         }
         
    void loop() {

      if  (digitalRead(b1) == HIGH) {
       Engage();
      }

      //if  (digitalRead(b2) == HIGH) {
       //Disengage();
      //}

      //if  (digitalRead(b3) == HIGH) {
       //RemovePape();
      //}
      
   }

How is b1 ever going to be HIGH if it is wired to take the pin to GND?

Well, I thought he idea was that when the button gets pushed, pin 63 gets HIGH so the button needs to be between 63 & Grnd?

Am I really totally off in how this works?

If the switch is wired with one terminal to an input and the other terminal to ground with the input set to pinMode INPUT_PULLUP (or using an external pullup) the input will read HIGH when open (not pressed) and LOW when closed (pressed). The pullup (internal or external) resistor is necessary.

Ok thank you....... so, I'm going to take a stab at this..... if 63 is input the should one button pin be to 63 and the other 5v? No no, hmmm ugh err....

Yup. I'm still a newbie haha.

Thanks again for any help!

The switch input needs to be held high or low when the switch is open so that the input is not "floating". That is done with a pullup or pulldown resistor. The easiest way is to use the built into the chip internal pullup resistor. That requires that the switch be connected between the input and ground and the pullup be enabled by pinMode(pin, INPUT_PULLUP). Then adjust the program logic to look for a LOW when the switch is closed and HIGH when the switch is opened.

If you want the input to be HIGH when the switch is closed (pressed) and LOW when the switch is open (not pressed) you can wire the switch between 5V and the input, but you will need an external pulldown resistor from the input to ground to keep the switch from floating.

It's time for the best training aid ever:

whoa

thanks so much!

I just did some research on this...

didn't realized there was actually a difference between INPUT and INPUT_PULLUP

normally I see lots of examples with only pinMode (#, INPUT) for buttons that control motor sequences and they seem to work for some reason?

Just tried INPUT_PULLUP, it works.

thanks all! learning more and more each day...

very interesting image regarding INPUT_PULLUP.

Do all the analog/digital pins on the mega ramps1.4 use an INPUT_PULLUP?..... just curious... or would there be more than one pullup resistor on the Mega?

thanks!

normally I see lots of examples with only pinMode (#, INPUT) for buttons that control motor sequences and they seem to work for some reason?

That can work, but if you look into how the switch is wired, the ones that work have an external pull down resistor. Switches that are active high (HIGH when pressed) are more hassle because of the need for the external resistor, but it is a popular way to wire a switch for new users that can't wrap their mind around LOW being ON.

Each of the digital pins have an internal pullup resistor that can be enabled. Bear in mind that the "analog inputs" are first digital pins with the special function of analog input so they have internal pullups as well.

Thats so cool! Thanks again
cant wait to learn more

When would you need to use the pullup resistor?

For a simple LED/resistor/button combo it doesn't seem to be necessary to call for INPUT_PULLUP? Or does it?

Do you understand why a resistor is needed? An input with nothing connected to it is said to be "floating". A floating input's state is indeterminate so cannot be known (it can be low or high depending on whatever noise that it picks up). The resistor, whether internal or external, pullup or pulldown, brings the input to a known state even when nothing else is connected to it. Maybe this tutorial will make it more clear.

LEDs do not use pullup or pulldown resistors, they use current limiting resistors, a totally different thing.

Whats wrong with this picture from https://www.brainy-bits.com/arduino-input-pullup-tutorial/

Wonder how many Arduinos that tutorial has blown?

Whats wrong with this picture from https://www.brainy-bits.com/arduino-input-pullup-tutorial/

(1) When the button is pressed there will be a short between 5V and GND
(2) If the D5 pin ever goes HIGH there will be a short between 5V on the pin and GND

The D5 pin should be connected to GND by a 10K resistor, not a wire