Potentiometer returning junk?

I’m trying to make an LCD display with pages, with my intention to later build a pip-boy type device. Since I’m no good with buttons, I thought I’d map a potentiometer (0-4) in order to use it to scroll through data. Unfortunately, when I run my simulation, I’m seeing a mess of 01020310313. My potentiometer is linked (pos, data, neg), to pin 6. Here is my code, what did I do wrong?

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int potPin = 6;     // the number of the pushbutton pin
int count = 0;               // Count the button presses

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(potPin, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
   lcd.print("#");
  Serial.begin(9600);
  }

void loop() {
	int page = analogRead(6);
  	page = map(page, 0, 1023, 0, 4);
    Serial.print(page);
  
  if (page == 0){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("IT");
  }
  
  else if (page == 1){
      lcd.clear();
      lcd.setCursor(0,0);
    lcd.print("IS");
  }
  
  else if (page == 2){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("TUESDAY");
  }
  
  else if (page == 3){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("MY");
  }
  
  else if (page == 4){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("DUDES");
  }
  
  else{
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("E.R.R.O.R");}


 
}

What value pot? It should be 10K or less, maximum.

Just print out a few analogRead() values, on the serial monitor and on separate lines, to see if the pot is working as expected.

Ok, I think I figured out one major problem, I had it hooked to a normal pin and not an Analog In pin, but now it's just reading 0 even though I linked the potentiometer to 5

const int potPin = 5;     // the number of the pushbutton pin
int count = 0;               // Count the button presses

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(potPin, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
   lcd.print("#");
  Serial.begin(9600);
  }

void loop() {
    int page = analogRead(5);
   page = map(page, 0, 1023, 0, 4);
    Serial.print(page);
  
  if (page == 0){

even though I linked the potentiometer to 5

Do you mean pin 5 or pin A5 ?

I’m going to go ahead and assume I’m an idiot, since I didn’t notice that I wrote it as pin 5, not pin A5

Icyartillary: I'm going to go ahead and assume I'm an idiot, since I didn't notice that I wrote it as pin 5, not pin A5

Actually, strangely, you can write it as 5. The function will translate it to A5. But you can't connect the hardware to digital pin 5.

Actually, strangely, you can write it as 5. The function will translate it to A5.

You have that backwards. The value 5 is the correct value. The value associated with the name A5 is a digital pin number that has to be translated to an analog pin number (5).

Sorry for this, this is my first time working with analog, I usually don't have to bother with it. So..should I have it written as the ( int potPin1 = A0;) or as ( int potPin = 0; )? I have the 10k pot hooked up to analog 0, heres a screenshot from my circuits.io sim http://imgur.com/a/Nqqwg

Icyartillary: Sorry for this, this is my first time working with analog, I usually don't have to bother with it. So..should I have it written as the ( int potPin1 = A0;) or as ( int potPin = 0; )? I have the 10k pot hooked up to analog 0, heres a screenshot from my circuits.io sim http://imgur.com/a/Nqqwg

Assuming you're using an Uno, this line defined in pins_arduino.h:

static const uint8_t A0 = 14;

and this line inside the analogRead function:

if (pin >= 14) pin -= 14;

will mean that both end up exactly the same anyway.

Damnit..I see what you're saying, for some reason I'm still reading just zero..maybe I missed something in my code, or maybe its just the wrong pot

Have you tried slimming your code down to just the basics? What about the AnalogInOutSerial example?

I'm friggin about to kill myself. I didn't provide power to the potentiometer. :angry:

So..should I have it written as the ( int potPin1 = A0;) or as ( int potPin = 0; )?

It would have been better to have written it as

const byte potPin1 = A0;

That way it is clear that you are using an analogue port, or at least intend to, made the code portable between different Arduino boards, used less memory than an int and ensured that the value could not be changed by accident within the program.

Mind you, if you don't connect the pot correctly, as seems to be the case, then it doesn't matter anyway.

That way it is clear that you are using an analogue port, or at least intend to

I have to disagree with that statement. Using the name assigned to the digital pin that shares space with the analog pin does NOT make it clear that you are using the analog pin as an analog pin.

Mind you, if you don't connect the pot correctly, as seems to be the case, then it doesn't matter anyway.

No that I CAN agree with.

I have to disagree with that statement.

Why am I not surprised ?

Using the name assigned to the digital pin that shares space with the analog pin does NOT make it clear that you are using the analog pin as an analog pin.

Surely the pin has the same name, whether it is used as an analogue pin or a digital pin. The intention is, of course, open to interpretation.

I have in the past made the mistake of thinking that someone was using digital pin 0 when their code included

int wibble = analogRead(0);

I seem to remember that you quite rightly pointed out my mistake. Had they used A0 then what they were doing would have been clearer, at least to me.

PaulS: You have that backwards. The value 5 is the correct value. The value associated with the name A5 is a digital pin number that has to be translated to an analog pin number (5).

I didn't explain clearly. analogRead() will accept either an integer value starting at 0 for the lowest analog pin, or the integer value of the digital pin it is associated with. So on an Arduino, analogRead(0) and analogRead(14) give the same result.

Here is the line in the source code for analogRead() that does that:

    if (pin >= 14) pin -= 14; // allow for channel or pin numbers

Since A0 is defined as 14 in pins_arduino.h, analogRead(A0) also gives the same result.

What was probably thought of as a convenience at design time, can result in user confusion as it appears to associate analog with a digital pin.