# Peculiar behaviour of code displaying button presses using MSP430

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()

{

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.

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

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.

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.

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.

• Nice catch on the typo. Reported to the energia forums. – Passerby Nov 2 '14 at 9:17
• 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. – Adam Nov 4 '14 at 5:57

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.

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