Potentiometer Readings Off

Hello all!!

I have a new project at hand but i am running into some snags. I am trying to use a Potentiometer with a button to "iniate" the use of the Potentiometer though the snag is that once I press the button the readings from the Potentiometer are +14 when it is suppose to be 0 and -6 ish when the max reading is suppose to be 255.

It seems that some power is leaking over to the LED and im not sure entirely sure why. Here is the code below

Any ideas?

const byte LED_PIN = 9;  
const byte LED_GO = 2;
const byte BTN1 = A1;

boolean START = false;


void setup() {
 
  Serial.begin(9600);

  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_GO, OUTPUT);
  pinMode(BTN1, INPUT);
}


void loop() {

  if (digitalRead(BTN1) == HIGH)
  {
    digitalWrite(LED_GO, HIGH);
    START = true;
  }
  else {
    digitalWrite(LED_GO, LOW);
    digitalWrite(LED_PIN, LOW);
    START = false;
  }

  if (START)
  {
    // reads the input on analog pin A0 (value between 0 and 1023)
    int analogValue = analogRead(A0);

    // scales it to brightness (value between 0 and 255)
    int brightness = map(analogValue, 0, 1023, 0, 255);

    // sets the brightness LED that connects to  pin 9
    analogWrite(LED_PIN, brightness);


    Serial.print("Analog: ");
    Serial.print(analogValue);
    Serial.print(", Brightness: ");
    Serial.println(brightness);
    delay(5);
  }
}

Can you explain this please?
There is nothing to suggest negative numbers in your code.

Did you mean

int brightness = analogValue / 4;

?

Please post a schematic. Show how the button switch is connected.

So...

Button OFF means Potentiometer value = 0
Button ON means Potentiometer value = 14 (no turn of the dial, this is base value)

Then when dial is turned up to maximum (where the value is obtained from the serial monitor) 255, it reads between 248-251. Never obtaining the max value of 255

The analogValue /4 can happen or you can map it, same difference.

@groundFungus will do give me a moment. Best i can do is wire it up in Tinkercad and show you a diagram that way. Would that suffice?

Hand drawn, photographed and posted is fine. Tinkercad is too, if complete.

i see no difference in analog reading using your code modified to always print the values regardless of the button state (using MultiFunction board)

See this below. Mind you the red switch is suppose to be a push button but for representation purposes this is how its all wired. Green to Arduino, Black ground, Red power.

I know i don't have any resistors but for now this is how i have it wired up physically. Let me know

its more so a physical "spill" of power from one connection to another. The serial read is correct when I don't put in the LED_GO in the code though. Not sure why, thats my entire point to find out here to see if this specific code would replicate the same situation with someone else if they were to wire it up the same way

There must be either a pull down or pull up resistor to make the switch have a known state when it is open. Otherwise it will "float" when open and the state is indeterminate. If you wire the switch to ground, then you can enable the internal pullup resistor. The switch input will read HIGH when the switch is open (not pressed) and LOW when closed (pressed). Adjust your code accordingly.

Change this line:

 pinMode(BTN1, INPUT);

To:

 pinMode(BTN1, INPUT_PULLUP);

and your code to look for LOW when the button pressed.

Understood, though when the circuit is "open" (off and not pressed) there is no change to the LEDs or power spill. Only when the circuit is closed (button is pressed) is where there is an issue.

Unless im not understand basic electrical things correctly

You're more likely to get the top 255 value with the division.

When changing it to INPUT_PULLUP it causes the LED on the button to light up and creates another situation to fix. Not sure if im understanding what you are trying to explain correctly.

@TheMemberFormerlyKnownAsAWOL hmm, alright ill give that a go

Why don't you have any resistors? The large current draw might damage the board.

In the if structure, you check for a HIGH to see if the button pressed. If you change the switch wiring to an active low (LOW when pressed), you also need to change the if to look for a LOW when the switch pressed.

If i do that the LED are turned on from the get go and when the button is pressed they are turned off.

  if (digitalRead(BTN1) == LOW)
  {
    digitalWrite(LED_GO, HIGH);
    START = true;
  }
  if (digitalRead(BTN1) == HIGH)
  {
    digitalWrite(LED_GO, LOW);
    digitalWrite(LED_PIN, LOW);
    START = false;
  }

just an FYI for all who read this...

This code below, when removing the LED_GO from the entire code will have the correct readings throughout with the same wiring. No removed parts from the board or disconnected wires.

const byte LED_PIN = 9;  // the PWM pin the LED is attached to
// const byte LED_GO = 2;
const byte BTN1 = A1;

boolean START = false;


void setup() {
  Serial.begin(9600);
  
  pinMode(LED_PIN, OUTPUT);
  // pinMode(LED_GO, OUTPUT);
  pinMode(BTN1, INPUT);
}


void loop() {

  if (digitalRead(BTN1) == HIGH)
  {
    // digitalWrite(LED_GO, HIGH);
    START = true;
  }
  else {
    // digitalWrite(LED_GO, LOW);
    digitalWrite(LED_PIN, LOW);
    START = false;
  }

  if (START)
  {
    // reads the input on analog pin A0 (value between 0 and 1023)
    int analogValue = analogRead(A0);

    // scales it to brightness (value between 0 and 255)
    int brightness = map(analogValue, 0, 1023, 0, 255);

    // sets the brightness LED that connects to  pin 9
    analogWrite(LED_PIN, brightness);


    Serial.print("Analog: ");
    Serial.print(analogValue);
    Serial.print(", Brightness: ");
    Serial.println(brightness);
    delay(5);
  }
}

try a separate power wires to the pot

Why is the int data type when the data type byte is good enough?

Because the result of an int divided by an int is . . . TaDa! an int

all wires as separate from one another, unless im not understanding what you mean