Arduino push button not responding when in line with resistor

I am new to arduino and working on a small project of my own. I tried making a button which can be pressed to use my step motor. Unfortunately, either the button does not seem to work or (and this is what I am thinking of) the resistor is preventing the button from ever reaching the motor (when pressed) through electricity.

Whenever I wire the white wire in another line, instead of the line where the resistor is set, it does what I expect it to do (but only non-stop). However, when I put the white cable in line with the resistor, it doesn't go off.

See the image below to get an idea of how I set it up:

Note: If you need more or better pictures please let me know. I cannot add more than 1, because my account is new.

How my code looks like:

//Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants

// Number of steps per internal motor revolution 
const float STEPS_PER_REVOLUTION = 30; 
//  Amount of Gear Reduction
const float GEAR_REDUCTION = 15;
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REVOLUTION * GEAR_REDUCTION;


// Define Variables
// Number of Steps Required
int StepsRequired;
int switchPin = 7;

//Extra Boolean variables to keep track of button and motor state
boolean current = LOW;
boolean last = LOW;
boolean isOpen = LOW;

// Specify Pins used for motor coils
// The pins used are 8,9,10,11 
// Connected to ULN2003 Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-3-2-4 for proper step sequencing

Stepper steppermotor(STEPS_PER_REVOLUTION, 8, 10, 9, 11);

void setup()
{
  //Stepper Library sets pins as outputs
  pinMode(switchPin, INPUT);
}

//Corrects push button bounce (not the best debounce function)
boolean debounce(boolean inLast)
{
  boolean inCurrent = digitalRead(switchPin);
  if(inLast != current)
  {
    delay(5);
    inCurrent = digitalRead(switchPin);
  }
  return inCurrent;
}

void garageAction(float factor){
  // Slow - 4-step CW sequence to observe lights on driver board
  
   // Rotate CW 1/2 turn slowly
  StepsRequired  =  STEPS_PER_OUT_REV*factor; 
  steppermotor.setSpeed(1000);   
  steppermotor.step(StepsRequired);
  if(isOpen == LOW)
  {
    delay(1000);
  }else if(isOpen == HIGH)
  {
    delay(5);
  }
}

void loop()
{
  current = debounce(last);
  if(current == HIGH && last == LOW && isOpen == LOW)
  {
    garageAction(2);
    isOpen = !isOpen;
  }else if(current == HIGH && last == LOW && isOpen == HIGH)
  {
    garageAction(-2);
    isOpen = !isOpen;
  }
}

My question is: What am I doing wrong here? Is it my button which I didn't program properly or is it my board?

It looks like you need to move the orange wire up one row.

The more accepted way to wire a switch is to wire one side to ground and the other side to an input with the pinMode set to INPUT_PULLUP. The switch will read HIGH when unpressed and LOW when pressed. See the state change for active low switches thread.

Moved it up one... Didn't do the job, but I still appreciate the advice.

Troubleshoot step by step. Put a print statement right after you read the switch to make sure that it is being read.

Put other prints at strategic locations to monitor program flow and make sure that things are happening as you expect.

Will there ever be a non-integer number of steps per revolution, like 30.073?

When you calculate this:

const float STEPS_PER_OUT_REV = STEPS_PER_REVOLUTION * GEAR_REDUCTION;

You might not end up with the number you expect due to floating point truncation. When I was in school, some people answered exam questions with answers like, "2.9999999" because the calculators the school lent out, didn't round numbers. All those people got half marks.

Also, 5 milliseconds is very near the borderline for some switch bounce, it's really too short for some.

You never need to check a value you've already checked. So this:

  if(isOpen == LOW)
  {
    delay(1000);
  }else if(isOpen == HIGH)
  {
    delay(5);
  }

would be better expressed:

  if(isOpen == LOW)
  {
    delay(1000);
  }
  else
  {
    delay(5);
  }

Everything is hard-coded. So whatever you see is whatever happens. There is nothing dynamic and therefore no possible pitfalls such as commas (or dots) in ints.

Hi,
Can I suggest a simple rule when it comes to these tactile buttons.
Make your two connections to the two diagonally opposite pins, this will ensure you are across the open contacts.

Do you have a DMM to make some resistance measurements?

Tom... :smiley: :+1: :coffee: :australia:

A DMM? I am not too familiar with these terms, but I honestly think I am doing something wrong with the bread bord. Let me show you the complete bread bord. Maybe that will clear things up as to how I have everything set up.



Hi,

A Digital MultiMeter.
Try this code, it will read the button pin and make the on board LED turn ON when you press it.
Also if you open the IDE Monitor window and select 9600 baud it will tell you the button status.


int switchPin = 7;
int LEDPin = 13;
bool switchStatus;

void setup()
{
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  switchStatus = digitalRead(switchPin);
  if (switchStatus == true)
  {
    Serial.println("Button Pressed");
    digitalWrite(LEDPin, HIGH);
  }
  else
  {
    Serial.println("Button NOT Pressed");
    digitalWrite(LEDPin, LOW);
  }
  delay (250);
}

Have you moved the orange lead as shown in my previous post?

Did you write this code all at once or in stages.
If in stages you should have a similar code to test your button, and other codes to test each of your hardware items, before combining them.

Tom... :smiley: :+1: :coffee: :australia:
PS, We need a circuit diagram, please draw one and post an image, include component names and pin labels.

Yes I did change the orange one. However the images are from before. I hadn't made any pictures after update. I'll do the rest of what you said. I'll update you soon. Pretty much 4 AM where I am right now so my brain is fried as well

Hi, @sdandys

Does my code work with your newer button configuration.
Lets get back to basics and check each part of your project separately.

Tom... :smiley: :+1: :coffee: :australia:

Hey Tom, back after the test. Your code does work. Interesting part though: The button itself does @!*@ all. Apparently the button is for the show. As mentioned in my previous comments: When I unplug the white cable from the resistor it returns "button pressed". If I put it in front of the resistor again it says "button not pressed". I'll get busy with the diagram.

In that state you have the input floating so the result is to be expected and not recommended when using it in code.

So when you press the button it says "pressed" and when not pressed it says "not pressed".

Tom.... :smiley: :+1: :coffee::australia:

That's what it should return when I press the button, this isn't the case however. Regardless of what I press, the button doesn't return any responses based on me pressing it or not. It's the fact that I remove the white cable from the resistor that gives me the right responses.

Hi,
Okay remove the button from the circuit.
First connect orange to white, (the other ends, white should go to D7, Orange to 5V) and the 10k between gnd and the orange white connection.
See what is indicated.
Second remove just the orange wire from the connection, leaving the white connected to the 10K.
See what is indicated.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Do you mean with "remove just the orange wire from the connection" the one I connected to the 5V?
Arduino Tutorial Step Motor with Push Button Part II - YouTube (0:10 represents what I have)

Hi,
Like this.

Tom... :smiley: :+1: :coffee: :australia:

Those "tact" buttons often do not actually fit into a breadboard unless you use pliers to straighten out the leads. Even then they may not actually connect into the breadboard. :face_with_raised_eyebrow:

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