Go Down

Topic: a question about state changes  (Read 214 times) previous topic - next topic

munch15a

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 ?

evanmars

Code: [Select]
if (currentState != previousState)
{
     Serial.print(currentState);
     currentState = previousState;
}

larryd

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.


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

spareProfile2

#3
Jun 20, 2019, 06:07 am Last Edit: Jun 20, 2019, 06:11 am by spareProfile2
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.)


munch15a

sorry for the long delay in posting works been too busy but thank you for all the help going to follow up your advice

Go Up