Stop existing loop and replacing it with a press of a button.

I am very new to programming and fine electronics as well. The only experience I have with electronics beforehand is heavy machinery and that is how I got interested in the Arduino.I realized that I could learn how to control these in the way I want to if I can design my controllers rather than getting multiple different controllers and just using a few function on each making it very expensive.

So I decided to make a small test with an 4x20 LCD screen 2 buttons and the Arduino.
What I want it to preform is a simple task you will get a statement or a question in this case I just used the question “Are you bored?” “Yes/No”.

What happens now is that it flickers when turned on. When i press i button it will show me the next question in line but it will flicker between both at the same time. And what I want it to do is to go to the next question in line until an ultimate verdict is met and then it goes back to the beginning. I just can’t find the right code to use to stop the previous function aka “are you bored?” to move on the next in line depend on my choice. I bet this is real easy when you know what you do, problem is, I don’t

The code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int BUTTON1 = 9;
int BUTTON2 = 8;

void setup() {
lcd.begin (20,4);

void loop() {
lcd.clear ();
lcd.print(“Are you bored?”);

if(digitalRead(BUTTON1) == HIGH) {
lcd.clear ();
lcd.print(“Do you have any”);

if(digitalRead(BUTTON2) == HIGH) {
lcd.clear ();
lcd.print(“you are bored!”);
lcd.print(“I can tell!”);


Do you have pull-down resistors on your buttons? Normally, one would use pinMode to set the internal pull-up resistors but then the button would read LOW when pressed and HIGH when not pressed so you've got your logic backwards for that.

You have to rethink your logic here. If the button is pressed to get into the first if, and it takes just a few microseconds to execute the code in there and send the stuff to the LCD, what do you think the chances are that the user can get their finger back off the button quick enough to stop it from still being pressed a few microseconds later when it gets checked in the second if statement?

You could rewrite this to block until a button is pressed by using an empty while loop. But that stops you from adding anything else to the program. Better would be to learn about state-machines and rewrite this to be one of those. Either way, your logic needs a complete redo. You need to think about how a program runs from top to bottom and how the loop function gets called over and over again and repeats. Take advantage of that.

An important initial understanding of the Arduino style of doing things is that the loop repeats. At the end of the loop it keeps going again right at the start of the loop block. Your current example clearly has a start, middle, and end and could be written to complete properly in just one loop. Since you intend this to be a learning exersize it's better to use a method that works with the repeating loop. The three things you need are a way to remember the STATE of the program's progress, to monitor for EVENTS that must be responded to and manage the TIMING of outputs that need to happen at some time after an input.

I think the Debounce tutorial is a rather good, if trivial example of all three. You should be able to apply several of it's techniques to make your test project work as intended.

@ Delta_G
I put a 1k resistor on each button and a 10 uF capacitor on 1 and 2 on the LCD to filter out any external disturbances that might be picked up by the leads.

I did try an empty but I found out that it would just stabilize the picture meaning it just issued the first part of the loop and then stopped completely when a button was pushed. In other words it did exactly what you described it would.

I was kind of hoping there were commands that said “Wait here util further instructions” but you gave me a bone to chew. Now I have a direction to do some research on my own. Thanks!

I will check out the tutorial you linked before addressing any questions that your answer created.

How are the resistors connected to the buttons? You're not using pinMode and your using reversed logic on the button since you want pressed to be HIGH. There's one specific way to make that happen and if you don't get it right your buttons don't work.

If it were me I'd put the buttons in the normal way between pin and ground and use the normal LOW equals pressed type logic.

From the GND on the breadboard to the lane where the button is and the resistor goes from that lane into a separate one where another lead goes to the pin on the Arduino.

Can you draw a picture of that? From your description it doesn't sound like you have it right. Your desription isn't actually a complete sentence and the fact that it is missing a subject is confusing me. WHAT goes from ground to the lane where the button is?

The whole circuit would look something like this… I did not change the values in the program i used to the components I use, frankly I didn’t bother to since you already know the values. I downloaded it specifically to show you.

OK, so one side of your button is indeed grounded and you've got the pulldown. But you don't have the other side of the button hooked to anything. SO when you press the button it doesn't connect to anything and doesn't change anything.

1: Take the resistors and put them back in the drawer.
2: Hook the line going to the input pins to the other side of the button.
3: Add pinMode calls to set those pins to INPUT_PULLUP
4: Reverse the logic in your code so LOW means pressed.

This will not fix your whole issue, you've still got the logic issue. But this will at least get the buttons working.