How Do I Set Up Multiple Loops in Arduino?

Hello,

I’m trying to create a syllogism tester, using an LCD, a potentiometer, and a button. This requires multiple loops that check for the potentiometer position, then check if the button has been pressed. If the buttons has been pressed, it moves onto the next loop that does the same checks, but if it hasn’t been pressed, it loops back around to continue checking the potentiometer’s position.

One bit of code I’ve written allows for me to twist the potentiometer and switch between “ALL,” “NO,” or “SOME” being displayed on the LCD. However, that code does nothing when the button is pressed.

Another bit of code I wrote displays nothing on the LCD, no matter what position the pot is in, but will display “TEST PASSED” when I press the button.

To be clear from the beginning: I believe that, more than anything, this is an issue of figuring out how looping subroutines are made in Arduino’s code.

Here is the code which will display text based on the pot’s position, but will not move out of that code when a button is pressed:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins 

const int buttonPin = 10;
const int potPin = A0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

void setup() {
pinMode(buttonPin, INPUT);
pinMode(potPin, INPUT);
lcd.noAutoscroll();
int buttonState = 0;
}
void loop() {
int  buttonState = digitalRead(buttonPin);
for(; buttonState == LOW;)
{
int sensorValue = analogRead(A0); 
if (sensorValue < 330) 
{  
lcd.print("ALL"); 
} 
if ((sensorValue > 330) && (sensorValue < 660)) 
{  
lcd.print("SOME"); 
} 
if (sensorValue > 660)
{  
lcd.print("NO"); 
}
}
lcd.clear(); 
lcd.print("TEST PASSED");

Here is some code that displays nothing until I press the button, at which point it shows “TEST PASSED.”:

#include <LiquidCrystal.h>

int buttonPin = 10;
int potPin = A0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

void setup() {
pinMode(buttonPin, OUTPUT);
pinMode(potPin, INPUT);
lcd.noAutoscroll();
}
void loop() {
for(;digitalRead(buttonPin) == LOW;);
{
int sensorValue = analogRead(A0); 
if (sensorValue < 330) 
{  
lcd.setCursor(0,0);
lcd.print("ALL "); 
} 
if ((sensorValue > 330) && (sensorValue < 660)) 
{  
lcd.setCursor(0,0);
lcd.print("SOME"); 
} 
if (sensorValue > 660)
{  
lcd.setCursor(0,0);
lcd.print("NO  "); 
}
}
lcd.clear(); 
lcd.print("TEST PASSED");
}

I'm pretty sure I'm not using the FOR statement correctly, but I do not know what commands to use to make it display text based on the pot position, check for button press, repeat until the button is pressed.
 for(;digitalRead(buttonPin) == LOW;);

How does the seemingly extra ';' in there work?

It compiles, but I'm sure it isn't formatted correctly. I likely need a while statement instead, though that doesn't change the issues that I've outlined in regards to getting it to display text depending on the pot position, until the button is pressed and then moving onto another loop that does the same.

I'll put some code together with a while statement instead, and post it here, but it won't make happen what I need to make happen.

No, you are not using for correctly, you’re using it like a while.

for (expression on start;test;expression every loop) {

}
A typical use of for:

for (i=0;i<8;i++) {
doSomething(i); //calls doSomething(0), doSomething(1), and so on until doSomething(7);
}

While is:
while(test) {
//keep doing this as long as test is true.
}

A typical use might be (though often this is the wrong approach in terms of reading inputs - but right now just highlighting the types of loop )

while (digitalRead(2)==1) {
doSomething(); //repeatedly call doSomething() until digitalRead(2) returns something other than 1.
}

What you’re doing with the for is giving it an empty expression to evaluate at the start of the loop and on each iteration, so it just checks the test - doing the same thing as a while (and likely compiling to the same machine instructions), but more confusing to read.

I have no idea, frankly, what you’re trying to do. Have you drawn out a flow-chart of the behavior you want? That I think would be a valuable thing for you to do.

Don't use blocking loops, (re)design your program to use (or be) a state machine.

DrAzzy, thanks for the reply.

Here is a flow chart of what I'm trying to get it to do: http://i.imgur.com/ra5J6kU.png

Variables are also going to be assigned when the pot is in a particular position but the first step is to simply get it to display correctly, meaning it's reading the pot, displaying text based on position, and then exiting that loop when a button is pressed.