Trying to create a selection program but having some issues

Hi there! So I'm new to Arduino; a college course I'm taking sprang out of the blue with this and I've never even heard of, let alone thought of ever taking on this kind of project, but I'm very eager to learn.

So I am trying to create a program in which I am able to change the text on a 16x2 display by using the joystick and button inside of it to select and change the text. For example, I want to select something, so I move right once to display "YouTube" and press in the joystick to click the button to change that text to a "tinyurl" or something along those lines.

I have been following this guide on how to do all this since I am awful and new to this whole thing, however I am getting some verify errors that I feel should not happen, mostly saying that things are not defined properly or "redefined", so I've uploaded the .ino here. What am I doing wrong, or better yet, what am I doing right so far? I'd love to see what else I could do instead of my "I know python and C# and thats what I'll attempt to do" pseudocode that barely works. Thank you for the help in advance!

Please post the code here using the code tag symbol, "</>. Most helpers don't follow tutorials, videos, guides etc.
Post the error report.

For sure! Sorry about that, here it is!

#include <LiquidCrystal.h>
#include <ezButton.h>
#define VRX_PIN  A4
#define VRY_PIN  A5
#define SW_PIN   8

ezButton button(SW_PIN);

int count = 0;
int xVal = 0;
int yVal = 0;
int bVal = 0;
int selection = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
}

void loop() 
{
  bVal = button.getState();
  xVal = analogRead(VRX_PIN);
  yVal = analogRead(VRY_PIN);
  lcd.setCursor(0, 1);
  lcd.print("QR Code Links");
  if (xVal > 0)
  {
    if (count = 0)
    {
      lcd.print("Youtube");
      selection = 1;
      count++;
    }
    if (count = 1)
    {
      lcd.print("Wikipedia")
      selection = 2;
      count++
    }
  }
}

Naturally my error report now says nothing is wrong, but I'd still love some feedback as to what I could do better.

if count ==

(== for comparison, = for assignment)

Awesome, I should've remembered that from Python. So I actually got it to work, however it does not move when I move the joystick. Here's my current running code:

#include <LiquidCrystal.h>
#include <ezButton.h>
#define VRX_PIN  A4
#define VRY_PIN  A5
#define SW_PIN   8

ezButton button(SW_PIN);

int count = 0;
int xVal = 0;
int yVal = 0;
int bVal = 0;
int selection = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
}

void loop() 
{
  bVal = button.getState();
  xVal = analogRead(VRX_PIN);
  yVal = analogRead(VRY_PIN);
  lcd.setCursor(0, 1);
  lcd.print("QR Code Links");
  if (xVal > 0)
  {
    if (count == 0)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Youtube");
      selection = 1;
      count++;
    }
    if (count == 1)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Wikipedia");
      selection = 2;
      count++;
    }
    if (count == 3)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Netflix");
      selection = 3;
      count++;
    }
    if (count == 3)
    {
      count = 0;
    }
  }
}

Do you happen to know what could be going wrong? I have the joystick's SW in digital 8, VRY in A5, and VRX in A4.

Try printing xVal and yVal to the serial monitor to see shat values you are getting.

Also... Counts starts at 0... then your code increments to 1,2 ... but you never check for 2 (typo?).

Also.. these if statements execute immediately after each other so the screen will get overwritten very quickly.

Ahh okay. I should be using "case" statements instead then? The serial monitor is just my computer correct?

A case statement or "else if" ... but even then because you are running in loop the code will execute on the next loop. Maybe a small delay before you check the joystick again.

Sounds good, thank you. I will go ahead and try that out and see what happens. How exactly would you write a case statement for this expression? An "else if" is causing the refresh loop to loop slower and now be noticeable.

Firstly, MAJOR kudos for using code tags (i.e. code formatted correctly for the forum) without prompting in your first posting. You have no idea how rare that is!
Now, about Serial Monitor. Yes, it displays output in the Serial Monitor, which you open by selecting Serial Monitor in the IDE window. When it's open, note the baud rate (in my IDE, it's at the bottom of the window.
Now, in setup() in your code, add a line, "Serial.begin(XXXX);", where XXXX is the number just noted.
From there on, whenever you want to find out what some variable's value is, or you want to flag where the code execution is, just do a Serial.println("some message"); and that will appear in the monitor. It's an incredible debugging tool. There are nuances, but that's the crux.
There are tutorials about this, search Arduino.cc.

Gotcha! Thank you for that information. I knew my C# lab reports would be useful eventually for formatting purposes! So I ended up scrapping my joystick and I am now using three buttons in place of that for input. However, when the button is pressed, the display flips through the options so fast it does not actually stay long enough to be able to cycle through the menu itself. Here is the last verified version of my code that did do that part:

#include <LiquidCrystal.h>

const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;

int count = 0;
int selection = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop() 
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  lcd.setCursor(0, 1);
  lcd.print("QR Code Links");
 if (buttonState2 == HIGH)
  {
    if (count == 0)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Youtube");
      selection = 1;
      count++;
    }
    else if (count == 1)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Wikipedia");
      selection = 2;
      count++;
    }
    else if (count == 2)
    {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Netflix");
      selection = 3;
      count++;
    }
    else if (count == 3)
    {
      count = 0;
    }
  }

When I now press Button2, it quickly flashes the first choice (Wikipedia) before immediately resetting to the first text. I attempted a switch which I will post under this paragraph but that does not seem to do anything now. Do you happen to know what could be going wrong here and how to fix that?

// This snippet is right after "lcd.print(QR Code Links)"
switch (buttonPin2)
 {
   case HIGH:
    switch (count)
    {
     case 0:
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Youtube");
      selection = 1;
      count++;
      break;
     case 1:
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Wikipedia");
      selection = 2;
      count++;
      break;
     case 2:
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Netflix");
      selection = 3;
      count++;
      break;
     case 3:
      count = 0;
      break;
   }
 }
}

If you want time to read what you printed, you might add dela(1000); after the println() statement.
Later (if your code will actually take you to netflix) you may need to remove it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.