Code issues

When I am programming, my code is glitching to where i have a variable ans and each time the right button is pressed, it adds one to ans, then i used a bunch of if else statements to make it so if ans = 1 display this on the LCD if else ans = 2 display this on the LCD but it only goes to ans = 1 then I press it again and nothing happens. Please Help

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that’s not included with the Arduino IDE please always post a link(using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int ans;
ans = 0;

void setup() {
 lcd.begin(16, 2);
 lcd.setCursor(0,0);
 lcd.print("Virtual Drill");
 lcd.setCursor(1,6);
 lcd.print("Book");
 
 
}

void loop() {
 int x;  
 x = analogRead (0);
 if (x < 60) {
  //right button
  ans++;
   if (ans = 1) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt1 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
     return;
   }
   else if (ans = 2) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt2 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 3) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt3 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 4) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt4 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 5) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt5 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 6) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt6 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 7) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt7 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 8) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt8 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 9) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt9 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 10) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt10 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 11) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt11 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 12) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt12 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 13) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt13 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 14) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt14 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 15) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt15 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
   else if (ans = 16) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt16 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }
}
}
if (ans = 1)

Oops. See #3 here.

You forgot to do the Auto Format.

So I changed it to this,now when I click the right button, it goes straight to the ans = 4 display

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int ans = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Virtual Drill");
  lcd.setCursor(1,6);
  lcd.print("Book");
  
  
}

void loop() {
  int x;  
  x = analogRead (0);
  if (x < 60) {
   //right button
   ans++;
    if (ans == 1) {
      lcd.setCursor(0,0);
      lcd.print("S1 Crt1 Move 16");
      lcd.setCursor(0,1);
      lcd.print("8 BHD Fh 3 in 30");
    }
    else if (ans == 2) {
      lcd.setCursor(0,0);
      lcd.print("S1 Crt2 Move 16");
      lcd.setCursor(0,1);
      lcd.print("8 BHD Fh 3 in 30");
    }
    else if (ans == 3) {
      lcd.setCursor(0,0);
      lcd.print("S1 Crt3 Move 16");
      lcd.setCursor(0,1);
      lcd.print("8 BHD Fh 3 in 30");
    }
    else if (ans == 4) {
      lcd.setCursor(0,0);
      lcd.print("S1 Crt4 Move 16");
      lcd.setCursor(0,1);
      lcd.print("8 BHD Fh 3 in 30");
    }
}
}
else if (ans = 11) {
     lcd.setCursor(0,0);
     lcd.print("S1 Crt11 Move 16");
     lcd.setCursor(0,1);
     lcd.print("8 BHD Fh 3 in 30");
   }

If you’d learn to write a function this could all be done in about 5 or 6 lines of code instead of being repeated umpteen million times. Would be a lot less typing and way easier to maintain.

void doThisThang(int aNum){
   lcd.setCursor(0,0);
   lcd.print("S1 Crt");
   lcd.print(aNum);
   lcd.print(" Move 16");
   lcd.setCursor(0,1);
   lcd.print("8 BHD Fh 3 in 30");
}

Now your loop becomes:

void loop() {
 int x;  
 x = analogRead (0);
 if (x < 60) {
  //right button
  ans++;
  doThisThang(ans);
  }
}

Don’t that look a lot simpler?

5dannyboy:
So I changed it to this,now when I click the right button, it goes straight to the ans = 4 display

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int ans = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print(“Virtual Drill”);
  lcd.setCursor(1,6);
  lcd.print(“Book”);
 
 
}

void loop() {
  int x; 
  x = analogRead (0);
  if (x < 60) {
  //right button
  ans++;
    if (ans == 1) {
      lcd.setCursor(0,0);
      lcd.print(“S1 Crt1 Move 16”);
      lcd.setCursor(0,1);
      lcd.print(“8 BHD Fh 3 in 30”);
    }
    else if (ans == 2) {
      lcd.setCursor(0,0);
      lcd.print(“S1 Crt2 Move 16”);
      lcd.setCursor(0,1);
      lcd.print(“8 BHD Fh 3 in 30”);
    }
    else if (ans == 3) {
      lcd.setCursor(0,0);
      lcd.print(“S1 Crt3 Move 16”);
      lcd.setCursor(0,1);
      lcd.print(“8 BHD Fh 3 in 30”);
    }
    else if (ans == 4) {
      lcd.setCursor(0,0);
      lcd.print(“S1 Crt4 Move 16”);
      lcd.setCursor(0,1);
      lcd.print(“8 BHD Fh 3 in 30”);
    }
}
}

Yeah, that loop is going to run a few thousand times a second. So when you press that button, it is going to be read a few thousand times a second and it’s going to increase ans a few thousand times a second and which number you end up landing on when you finally get your finger off the button is a bit of a crap-shoot. Have a look at the “State Change” example. It works with a digitalRead setup for the button, but the same concept can be applied here to allow you to react only once per press of the button.

Yes that's simpler, except im using the 8 BHD FH 3 in 30 as a place holder, it is going to be different on every ans = number

5dannyboy: Yes that's simpler, except im using the 8 BHD FH 3 in 30 as a place holder, it is going to be different on every ans = number

OK, create an array of the possible answers and use ans as an index to it.

const char* answers[] = {"ANS = 1", "Now 2", "Third one", "Big Four"};

void doThisThang(int aNum){
   lcd.setCursor(0,0);
   lcd.print(answers[aNum]);
   // etc.
}

Or if they only differ in some small way, store the differences in an array and print them in pieces. Anything is easier than typing out the same code every time.

Honestly I just copied and pasted the original then am going to edit it to what I want it to say, because I am going to have 88 pages with a different set of word on each bottom and top part so I just figured this would work, thanks for all your help though!

5dannyboy: 88 pages with a different set of word on each bottom and top part

Then you probably also need to learn about PROGMEM. That a lot of strings to have hanging around in SRAM.