Arduino Forum

Using Arduino => Programming Questions => Topic started by: munch15a on Jun 20, 2019, 03:25 am

Title: a question about state changes
Post by: munch15a on Jun 20, 2019, 03:25 am
so im trying to get a button to serial print when the button is pushed or let go off as in only serial print the state of the button at the start and end of the pushing

so I looked at the tutorial below but it seamed to be more about counting the number of button pushes



https://www.arduino.cc/en/Tutorial/StateChangeDetection

so would anyone be able to point me in the right direction as to a sample script of what im looking for ?
Title: Re: a question about state changes
Post by: evanmars on Jun 20, 2019, 03:29 am
Code: [Select]
if (currentState != previousState)
{
     Serial.print(currentState);
     currentState = previousState;
}
Title: Re: a question about state changes
Post by: larryd on Jun 20, 2019, 04:05 am
When you ask your questions you should always let us see what you have tried to solve the problem yourself.

As far as the examples in the IDE, treated these examples as nothing more than techniques.

You will never find an example that does exactly what you want.


Title: Re: a question about state changes
Post by: spareProfile2 on Jun 20, 2019, 06:07 am
so I looked at the tutorial below but it seamed [sic] to be more about counting the number of button pushes
https://www.arduino.cc/en/Tutorial/StateChangeDetection
Just leave the counting part out then. Don't do the ++ increment, and delete the part where it toggles the led at the bottom of loop().

The inner if/else does exactly what you want, with a message for a press and a different message for a release.

(evanmars' solution does what you want the way you described it, and is a bit simpler, but if you ever need to distinguish between a press and a release (as opposed to just seeing a change regardless of which way) then you need the more complete change as in the tutorial.)

Title: Re: a question about state changes
Post by: munch15a on Jun 27, 2019, 02:28 am
sorry for the long delay in posting works been too busy but thank you for all the help going to follow up your advice