0
\$\begingroup\$

I have created a simple program in MSP430 using Energia for counting the button presses from 0 up to 9 and displaying them in a Seven Segment Display.

The code including the definitions of the pins, the state of the pins (set as Input or Output) and the trivial displaying routine is omitted as it would be far too long. If there is any need for it tell me and I will edit my question.

The essential code is:

void loop()

{

 ButtonState=digitalRead(Button);

    if (ButtonState!=LastButtonState)
    {
        if (ButtonState==HIGH)
        {
            ButtonCounter++;
        }

    }

    LastButtonState=ButtonState;


   if(ButtonCounter==0)
   {

       Zero();
   }
   else if(ButtonCounter==1)
   {
       ...
   }

I couldn't make the display start from 0 at the beginning when I hadn't pressed the button but it was always beginning displaying 1 until I changed:

if (ButtonState==HIGH)

to

if (ButtonState==LOW)

And it is working perfectly! But it seems to me very weird.

Why is this happening? When I am not pressing the button isn't its state LOW? As it is beginning from 1 it means that its state when not pressed is HIGH. Shouldn't the correct be:

if (ButtonState==HIGH)

so to chech if the button is pressed and then increment the counter? With LOW as the comparison then it is checking when the button is not pressed, so why it works perfectly?

The schematic for the button can be found here.

\$\endgroup\$
  • \$\begingroup\$ how is your button wired in ? do you have a schematic ? Before you entire loop() was lastButtonState defined ? \$\endgroup\$ – efox29 Nov 2 '14 at 6:30
  • \$\begingroup\$ @efox29 I will give you a link to the schematic. Yes there is defined to LOW (or equal to 0) \$\endgroup\$ – Adam Nov 2 '14 at 6:38
  • \$\begingroup\$ just to confirm, your button is a momentary button ? \$\endgroup\$ – efox29 Nov 2 '14 at 8:26
1
\$\begingroup\$

Why is this happening? When I am not pressing the button isn't its state LOW?

I think there's some incorrect documentation. If you followed the schematic from the link you provided, then when the button is not pressed, the state would HIGH.

enter image description here

The code explanation says

If the buttonState is HIGH, it means that push button was pressed else it is not. Whenever the button state is HIGH, the Green LED is accordingly switched ON else it is switched OFF.

But this isnt the case. The hardware says when the button is pressed, the button state will be LOW.

What they did was copy the arduino button tutorial and just modified it slightly for use with the Energia board. If you look at the Arduino schematic, it's more inline with what you are expecting. So their code description is incorrect.

enter image description here

If you use the Energia schematic, then you need to be using LOW and if you are using the Arduino schematic then you need to be using HIGH.

\$\endgroup\$
  • \$\begingroup\$ Nice catch on the typo. Reported to the energia forums. \$\endgroup\$ – Passerby Nov 2 '14 at 9:17
  • \$\begingroup\$ Thank you for your answer! The thing is that it depends on what button you have. There are normally-on and normally-off buttons that is why (I think) there is a difference in the documentation. I have checked mine and is normally-off so when it is not pressed is LOW. LOW and HIGH are simply terms to denote if there is input in the pin so it all depends on what button you have. So I think that your answer is not applicable. But thank you very much. \$\endgroup\$ – Adam Nov 4 '14 at 5:57
1
\$\begingroup\$

You are using a pull-up resistor, on a normally Normally-Open momentary button. A pull-up is a resistor with a fairly weak resistance tied to VCC, used to make the default state of the input pin to a Level High. When the button is pressed, the direct connection to Ground brings the input pin to Level Low.

When you used if (ButtonState==HIGH), the code would automatically read the default physical state of the input pin as High because of the pull-up and the non-pressed button, and add 1 to the counter.

Your code worked exactly as it should.

If you want it to work the other way, you would move the resistor to the ground side of the button, making it a Pull-Down instead of Pull-Up setup.

\$\endgroup\$
  • \$\begingroup\$ Thank you, your suggestion seems quite plausible. I will check it out and let you know! \$\endgroup\$ – Adam Nov 4 '14 at 6:00

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.