0

I am very new to arduino and I've been scratching my brain on how to make one button print out three different sentences. When I press it the first time, a " I am " is suppose to appear, when I press it the second time " very" and then the third time " late". I can't have three different buttons because a tone follows this button as well.

5
  • You can start with the arduino.cc/en/Tutorial/StateChangeDetection and try to count to three and use the count to display different messages. – Jot May 1 '19 at 14:09
  • but how do I do that with a text? – Chase May 1 '19 at 16:32
  • When the counter is incremented, then you can do a few if-statements to show text according to the value of the counter. – Jot May 1 '19 at 18:03
  • @chase if you want me to break it down more in the answer, post a follow up note under my answer below. But regardless of what you call it, you will eventually create a state machine. If the code is nice an clean it will be easy to maintain and add to. If you go about it piecemeal then it might turn out to be difficult to manage. Remember to start simple, test then add more features. Don't try and do it all at once. Hence why I said make a 3 state machine w/o the messages the 1st go around. – st2000 May 1 '19 at 23:37
  • I have no idea where to start. – Chase May 4 '19 at 15:05
1

Consider using what is commonly called in programming as a Finite-state machine. Advance to the next state each time the button is pressed. Start out with 3 states and advance to the next after pressing the button. You many need to double the number of states after adding your message to the program. This is because you will want to press the button and only print the message once then advance to a new state where all you do is wait for the next button press.

Note, a mechanical device such as a button will need to be de-bounced either in hardware or in software. This is because the processor may see individual makes and breaks as the button is closed and opened causing it to advance to the next state in unexpected ways.

1
  • 1
    Put more simply: Make sure your code to respond to button presses "debounces" the button presses. Then add a global variable pressCount, initialized to 0. In the code that handles button presses, increment pressCount and use it to display the appropriate message. (Then you need to decide what to do on the 4th button press. Do you reset to 0 and start over? Do you ignore button presses after the 3rd?) – Duncan C May 1 '19 at 16:59

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.