2 Pushbutton Micro Switches , Arduino not recognizing state change when released

I have a question about my arduino project. Its a project for use in Airsoft/Paintball. I’m trying to use 2 push down micro switches as seen in picture (these are the exact switches I am using).

Problem is. I cant seem to get them to work properly. The code tells the arduino to look for the state change when both buttons are pressed - this makes a certain function true. When one or both is released, the function should turn false and stop what it is doing, but this second part doesn’t work.

I want the code to make the arduino recognize when both buttons are held, and when they are both or one is released. They are wired separately with external resistors

I know I am missing something here, but I can’t figure out what direction to take to do this. The sketch below is a simplified version of what I have for simplicity in explaining the problem/solution

Thank you for your replies!!

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledPin = 13; // choose the pin for the LED
int inPin = 9; // choose the input pin (for a pushbutton)
int inPin2=11;
int val = 0; // variable for reading the pin status
int val2 = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
pinMode(inPin2, INPUT); //declare pushbutton 2 as input
}

void loop(){

val = digitalRead(inPin); // read input value of button 1
val2= digitalRead(inPin2); //read input value of button 2
if(val == HIGH && val2 == HIGH)
{
defuse=true;
}

else if (val == LOW){
defuse= false;}
else if (val2 == LOW){
defuse=false;}
}

pushbutton_test.ino (748 Bytes)

How are the switches wired? Can you show us a schematic?

Do you have pull-up or pull down resistors? You generally need a resistor to force a high or low state when the switch is off. Then when the switch is closed, the input state gets forced to the opposite state.

[u]Digital Read Serial[/u] (external pull-down resistor).

[u]Input Pullup Serial[/u] (internal pull-up resistor).

DVDdoug: How are the switches wired? Can you show us a schematic?

Do you have pull-up or pull down resistors? You generally need a resistor to force a high or low state when the switch is off. Then when the switch is closed, the input state gets forced to the opposite state.

[u]Digital Read Serial[/u] (external pull-down resistor).

[u]Input Pullup Serial[/u] (internal pull-up resistor).

Thank you for responding to my post. I have updated my original post with a schematic for the pushbuttons. How would I apply the external pull down resistor changes to my code? Sorry, I'm really new to all of this. Thank you!

The easiest way to wire switches is to connect one side of the switch to ground and the other side to the digital input. Then, in setup, use pin mode(pin, INPUT.PULLUP); to enable the internal (on chip) pull up resistor. The switch will read high when not pushed and low when pushed so you need to adjust your logic.

groundfungus: The easiest way to wire switches is to connect one side of the switch to ground and the other side to the digital input. Then, in setup, use pin mode(pin, INPUT.PULLUP); to enable the internal (on chip) pull up resistor. The switch will read high when not pushed and low when pushed so you need to adjust your logic.

The pusbutton micro switches I'm using have 3 "prongs". should I be leaving one out of the circuit (power), and just connecting to ground and the digital input and then using the internal pullup resistor instead of the physical one?

I wold wire the common pin to ground and the normally open (NO) pin to the input pin with the internal pull up enabled. Another option with your switch is to wire common to the digital input, NO to ground and NC to Vcc (no resistor necessary). The important thing is that the input is either high or low, never open (floating). The first option, I think, is better cause with the second there is the chance of killing the pin if it were set to output and low the switch would short the pin to ground when not pushed.

groundfungus: I wold wire the common pin to ground and the normally open (NO) pin to the input pin with the internal pull up enabled. Another option with your switch is to wire common to the digital input, NO to ground and NC to Vcc (no resistor necessary). The important thing is that the input is either high or low, never open (floating). The first option, I think, is better cause with the second there is the chance of killing the pin if it were set to output and low the switch would short the pin to ground when not pushed.

Thank you- I will try that now.

I was exploring the forums on the arduino site and I came accross something that I thought could be useful. Does this "adaptation" of their work make any bit of sense compared to my original code above??

const int inPin = 9; // the pin number for the button const int buttonInterval = 300; // number of millisecs between button readings unsigned long previousButtonMillis = 0; // time when button press last checked unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop() boolean defuse; void setup() { pinMode(buttonPin, INPUT_PULLUP); } void loop() { currentMillis = millis(); readButton(); // call the functions that do the work }

void readButton() {

if (millis() - previousButtonMillis >= buttonInterval) {

if (digitalRead(inPin) == LOW) { defuse = !defuse; // this changes it to LOW if it was HIGH // and to HIGH if it was LOW previousButtonMillis += buttonInterval; } }

}

I was exploring the forums on the arduino site and I came accross something that I thought could be useful. Does this “adaptation” of their work make any bit of sense compared to my original code above??

You of course need to have two buttons.
I see no effort to deal with bounce suppression which I like to recommend.

You may want to look at Gammon Forum : Electronics : Microprocessors : Switches tutorial

Also see Nick’s SwitchManager lib at the bottom of his write up.

LarryD: You of course need to have two buttons. I see no effort to deal with bounce suppression which I like to recommend.

You may want to look at http://www.gammon.com.au/forum/?id=11955

Also see Nick's SwitchManager lib at the bottom of his write up.

Thank you for this link, I am looking at it now. I'll get back to you but this looks really hopeful

Oh that is rich! Brand name "Highlywell" - a sort of pun on "Honeywell". :grinning:

LarryD: You of course need to have two buttons. I see no effort to deal with bounce suppression which I like to recommend.

You may want to look at http://www.gammon.com.au/forum/?id=11955

Also see Nick's SwitchManager lib at the bottom of his write up.

I downloaded the library for the Switch Manager, and I also went through his whole tutorial and I couldn't get it to work- it will not turn the function to false when the buttons are released. I tried his tutorial with both buttons (didnt work) so I tried it with only 1 and I still couldn't get it to change. SideNote: Every new code I have tried along the way, I have tried in a simple program that is coded to turn on and off an LED, and each one works for that, but when I implement it into the large code, it doesn't do what it is supposed to. Any ideas?? Thank you

I do not have access to my PC right now but here is an example that might help you understand the library a bit. Remember to put Nicks lib in your documents library folder

#include <SwitchManager.h>
SwitchManager modeSW;  // create the object

const byte ModeSW = 2;              //input from the Mode push button switch     

void setup ()
{
  Serial.begin(9600);  
  modeSW.begin (ModeSW, modeSwitchManager);
}
//======================== END OF setup ==========================

void loop (){
  //check to see what's happening with the Mode switch
  modeSW.check();

  // put other loop() code here



}
//======================== END OF loop ===========================



//****************************************************************
//*                          FUNCTIONS                           
//****************************************************************

//                M O D E   S W I T C H   M A N A G E R
//****************************************************************
// function looks after the Mode switch functions
void modeSwitchManager (const byte newState, const unsigned long interval)
{
  if (newState == HIGH )
  {
    // N E W S A T E   I S   H I G H
    Serial.print ( "the switch was LOW for: ");
    Serial.print (interval);
    Serial.println( " milli seconds");

    return;

  }

  // N E W S A T E   I S   L O W
  Serial.print ( "the switch was HIGH for:");
  Serial.print (interval);
  Serial.println( " milli seconds");

  return;



}  //                  END of modeSwitchManager()

Another example:
If you had three switches then you could do, example:

SwitchManager modeSW;
SwitchManager incSwitch;
SwitchManager decSwitch;

In setup
modeSW.begin (ModeSW,modeSwitchManager);
incSwitch.begin (incSW, incSwitchManager);
decSwitch.begin (decSW, decSwitchManager);

In loop
modeSW.check();
incSwitch.check();
decSwitch.check();

Of course you would have to have three functions, one for each switch

void incSwitchManager(const byte newState, const unsigned long interval)
{
}

void decSwitchManager(const byte newState, const unsigned long interval)
{
}

void modeSwitchManager(const byte newState, const unsigned long interval)
{
}