Potentiometer mapping problem

I wanna scroll menu on 16*2 lcd with a potentiometer. I have created ‘if statements’ with defining limits for each menu option like “if(val<200 && val>0)” but as it check value each time after a loop and the looping is so fast that the menu option on lcd gets blur because of updation of text every fraction of second. So i have mapped the standard values (0-1023) to 1-5 and on printing it on serial monitor, it works perfectly. But when implying these value on if statements, it malfunctions i.e. Prints 1 2 3 4 5 1 2 3…

Please help me out of this.

Please help me out of this.

Without seeing your code? Not likely.

Please read the “how to use the forum” stickies. They will tell you how to format and post code. Without seeing the code is is difficult to trouble shoot. My best suggestion without seeing the code is to look in the reference on the if, else if, else construct and/or the switch case construct.

http://www.arduino.cc/en/Reference/Else
http://www.arduino.cc/en/Reference/SwitchCase

And that's why pots are a poor choice for menu navigation. Pots are great for things with continuous ranges, like how bright to make a light. But for things with discreet steps like a menu a rotary encoder is much more suitable.

I am really sorry for giving such incomplete information. But with inserting delay of 1 second and mapping fixes the problem. Thanks a lot for you guys, but can you please just help me in one more problem in this project.

Now the menu options can be scrolled perfectly, and now I wanna do is, if we want to open any specific menu option just keep the potentiometer unchanged and if potentiometer value doesn’t changes for five seconds, we get into that specific menu option. But I don’t know how to do this.

here is the code:-

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potval=0;
int val=0;

void setup() 
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();                        
  lcd.setCursor(0, 0);                
  lcd.print("Rotate The Knob");
  lcd.setCursor(0, 1);                
  lcd.print("For Menu Options");
  delay(2500);
 }
 
void loop() 
{
 potval = analogRead(A0);
 potval=map(potval,0,1023,1,6);
 if(val!=potval)
 {
 if(potval==1)                              
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);                
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("Option 1");
  
 }
 if(potval==2)                                 
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);               
  lcd.print("Option 2");
  
 }
 if(potval==3)
 {
  lcd.clear();                        
  lcd.setCursor(0, 0);              
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);              
  lcd.print("Option 3");
 }
 if(potval==4)
 {
  lcd.clear();                     
  lcd.setCursor(0, 0);            
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("Option 4");
  
 }
 if(potval==5)
 {
  lcd.clear();                      
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);             
  lcd.print("Option 5");
  
}
if(potval==6)                              
 {
  lcd.clear();                       
 }
val=potval;
delay(1000);
}
}

But I don't know how to do this.

How would YOU do it? This is your opportunity to "play Arduino".

When you can describe how YOU would do it, writing the code will be trivial.

The millis() function might prove useful (as used in the blink without delay example).

:sweat_smile: thanks Paul I have successfully made the program that I want with use of millis. But as I observed, push button selection method is much good then 5 second delay method. I had made a code for that but I have a problem in that too :slightly_frowning_face: . Believe me I tried a lot so that program work but nothing happened. Actually the menu doesn’t open with a button push.

here is the code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potval=0;
int val=0;
int button=7;
int state=LOW;
void setup() 
{
  pinMode(button,INPUT);
  lcd.begin(16, 2);
  lcd.clear();                        
  lcd.setCursor(0, 0);                
  lcd.print("Rotate The Knob");
  lcd.setCursor(0, 1);                
  lcd.print("For Menu Options");
  delay(2500);
 }
 
void loop() 
{
 potval = analogRead(A0);
 potval=map(potval,0,1023,1,6);
 if(val!=potval)
 {
 if(potval==1)                              
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);                
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("option 1");
  state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.print("option 1 opens");
  }
 }
 if(potval==2)                                 
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);               
  lcd.print("Option 2");
  state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.print("option 2 opens");
  }
 }
 if(potval==3)
 {
  lcd.clear();                        
  lcd.setCursor(0, 0);              
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);              
  lcd.print("Option 3");
  state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.print("Option 3 opens");
  }
 }
 if(potval==4)
 {
  lcd.clear();                     
  lcd.setCursor(0, 0);            
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("Option 4");
  state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.print("Option 4 opens");
  }
  
 }
 if(potval==5)
 {
  lcd.clear();                      
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);             
  lcd.print("Option 5");
  state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.print("Option 5 opens");
  }
  
}
if(potval==6)                              
 {
  lcd.clear();                       
 }
val=potval;
delay(1000);
}
}

Actually the menu doesn't open with a button push.

You would have to be holding the switch for up to a full second. Are you doing that?

How IS the switch wired?

After pressing it for one second delay too it doesn’t work. Although when I have tried to put this button state change code outside the “if(val!=potval)” i.e. just after void loop starting, it works.

i.e.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potval=0;
int val=0;
int button=7;
int state=LOW;
void setup() 
{
  pinMode(button,INPUT);
  lcd.begin(16, 2);
  lcd.clear();                        
  lcd.setCursor(0, 0);                
  lcd.print("Rotate The Knob");
  lcd.setCursor(0, 1);                
  lcd.print("For Menu Options");
  delay(2500);
 }
 
void loop() 
{
state=digitalRead(button);
  if(state==HIGH)
  {
    lcd.clear();
    lcd.print("Button pushed");
    delay(2000);
  } 
 potval = analogRead(A0);
 potval=map(potval,0,1023,1,6);
 if(val!=potval)
 {
 if(potval==1)                              
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);                
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("option 1");
   }
 if(potval==2)                                 
 {
  lcd.clear();                       
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);               
  lcd.print("Option 2");
   }
 if(potval==3)
 {
  lcd.clear();                        
  lcd.setCursor(0, 0);              
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);              
  lcd.print("Option 3");
   }
 if(potval==4)
 {
  lcd.clear();                     
  lcd.setCursor(0, 0);            
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);                
  lcd.print("Option 4");
    
 }
 if(potval==5)
 {
  lcd.clear();                      
  lcd.setCursor(0, 0);               
  lcd.print("Menu:-");
  lcd.setCursor(0, 1);             
  lcd.print("Option 5");
   
}
if(potval==6)                              
 {
  lcd.clear();                       
 }
val=potval;
delay(1000);
}
}

I have attached a picture for push button.

2.png

As i said if i put the button state change code in the very starting of void loop, the button works. I had modified

If(state==HIGH) to If(state==HIGH && potval==1) due to which the system worked as i want.

But why this all doesn't work inside If(val!=potval) statement?

Rishabh8721:
But why this all doesn’t work inside If(val!=potval) statement?

Because you didn’t turn the pot at EXACTLY the instant that you pressed the button. So the if for the button never got called because at the time the button got pressed val and potval were the same.

So is I am doing this right? I.e. If(state==HIGH && potval==1) outside if(val!=potval).

Rishabh8721:
So is I am doing this right? I.e. If(state==HIGH && potval==1) outside if(val!=potval).

Yes. Think about it. What are the chances that the button changes state at the same exact instant that the pot changes state? Do you still want to be able to react to the button when the pot HASN'T just changed state? If you want to be able to react to the button after the pot has sat still for a second, then it doesn't make any sense to only check the button when the pot is changing. Don't just take our word for it. Think about it. Does it make sense?