Programming an interactive menu in setup()

Hello everyone!

I am having trouble with a logic problem.

Here is what I want to accomplish:

I have 1 Pushbutton and 1 Potentiometer and a LCD.

When the Arduino executes the setup() I want to display a message which asks the user to adjust the poti to the desired value and when pushing the button, this value gets stored in a variable.

So far so good...I want this to happen 5-6 Times.

Here is the procedure:

  • Arduino starts
  • Message pops up asking for value 1
  • Adjust poti to desired value
  • Push button
  • Value gets stored into variable_1
  • Message pops up asking for value 2
  • Adjust poti to desired value
  • Push button
  • new value gets stored in variable_2

and so on....

Can this be done?

So far I have come up with this...

void setup(void) {  
  .........
  .........
  .........  
  
  while(digitalRead(buttonPin) == LOW) { 
    val = analogRead(potPin);
    poti = map(val, 0, 1023, 0, 99);  
    lcd.setCursor(0, 0);
    lcd.print(poti);  
  
    if (digitalRead(buttonPin) == HIGH) {
      variable_1 = poti;
    }  
  }

Now this works for ONE variable. But it is very sloppy...How can I make it work for an unlimited amount of variables?

Thanks!

This is a perfect situation for using data arrays. Suppose you have 5 values to collect. First, I would get the code working using the Serial object, then switch to the LCD. As a starting point, try:

#define POINTS   5
int values[POINTS];


void setup()
{
    int index = 0;

    Serial.begin(9600);

    while (index < POINTS) {
       Serial.print("Enter value #");
       Serial.println(index + 1);
       if (digitalRead(buttonPin) == HIGH)) {
          values[index] = analogRead(potPin);
          values[index]= map(values[index], 0, 1023, 0, 99);  
          Serial.print(values[index++]);
       }
     }
}

I have not tested this, but something like this should work.

This is definitely a push in the right direction! Thank you!

It's not quiet perfect though. As soon as I push the button it rushes through the loop and assign the same value to all 5 indexes.

This will be the output as soon as I push the button ONCE:

Enter value #1 46 Enter value #2 46 Enter value #3 46 Enter value #4 46 Enter value #5 46

I assume I would have to check for a previous state being LOW or a falling edge? Not sure how I would go about doing this.

As soon as I push the button it rushes through the loop and assign the same value to all 5 indexes

Jack did say that it had not been tested but it looks like you have nearly got it working. As you suspect, the problem is being caused because the program currently reacts to the button BEING pressed rather than when it BECOMES pressed. Have a look at the StateChangeDetection example in the IDE.

Ok I have it working!

The result is now:

Enter value #1 99
Enter value #2 66
Enter value #3 39
Enter value #4 17
Enter value #5 0

and they all get nicely stored into the array.

Here is the final code:

while(index < POINTS) { 
    buttonState = digitalRead(buttonPin);  
    Serial.print("Enter value #");  
    Serial.println(index + 1);
    
    if (buttonState != lastButtonState) {      
      if (buttonState == 1) {        
        values[index]= map(analogRead(potPin), 0, 1023, 0, 99);  
        index++;
      }  
      lastButtonState = buttonState;      
    }    
    delay(20);
  }

Thank you all for your help!

Maybe someone can answer this here as well.

When I adjust the Poti the values from 90 down to 10, they will be displayed correctly…
When the value reaches 9 though, the digit jumps to the left making it look like 90 again.

12
11
10
90
80
70
60
50
40
30
20
10
00

Any way to correct this?

Bavilo:
Maybe someone can answer this here as well.

When I adjust the Poti the values from 90 down to 10, they will be displayed correctly…
When the value reaches 9 though, the digit jumps to the left making it look like 90 again.

Any way to correct this?

  val = analogRead(potPin);
  poti = map(val, 0, 1023, 0, 99);  
  lcd.setCursor(0, 0);
  if (poti<10) lcd.print(" ");  
  lcd.print(poti);

Ugh! So simple! Thanks a lot buddy :)