The number 14 is printing to the Serial Monitor

This question comes from Jim Kenyon.  It’s based on…

  • Arduino Course for Absolute Beginners, 2nd Edition,
    • Control Structures
      • Using the While Loop
        • Challenge #5 – Add a potentiometer to the circuit. Write a program that turns off an LED while the input from the potentiometer is below a certain threshold.

The Question:

…when I write the program similar to how you have it:

const byte potPin = A0;
const byte led1 = 9;

void setup() {
  Serial.begin(9600);
  pinMode(potPin, INPUT);
  pinMode(led1, OUTPUT);
}

void loop() {
  while (analogRead(potPin) < 500) {
    digitalWrite(led1, HIGH);
    Serial.println(potPin);
    digitalWrite(led1, LOW);
  }
}

Everything works fine and the LED goes out around 500, but when I open the Serial Monitor the ONLY number that I get is “14”.

No matter what I turn the pot to, it reads “14”.

Your Challenge:

Take a look at this code and see if you can spot why the #14 is printing to the serial monitor window.

Hint:

Pay close attention to what is being printed by the Serial.println() function.

The Solution:

Here is what I came up with…

[fvplayer src=”https://vimeo.com/334659555″ transcript=”auto” splash=”https://i.vimeocdn.com/video/781014940_1280x720.jpg?r=pad” caption=”Jim Kenyon – num 14″]

Your Thoughts:

What do you think?  Ever done this yourself? (I know I have!)

Let us know in the comments below!

5 Comments

  1. Kris Falkowski on November 5, 2019 at 10:16 pm

    Hi, I was trying to use “const byte” type in VS2017 and it doesn’t recognize the type “byte”. Do you know, by any chance, why? Thanks.
    P.S. If are unable to answer because of the volume of this type of questions, I understand.

    • Michael Cheich on November 5, 2019 at 10:37 pm

      Hmmm! Great question. I have not used the Arduino IDE for Visual Studio before…but you’d think it should be recognizing byte as a type.

      By chance, are you using it in a header file?

      I found this post in the Arduino Forum:
      https://forum.arduino.cc/index.php?topic=46782.0

      The gentleman suggests adding #include “WProgram.h” to the top of the file.

      Hope this helps some Kris!

  2. Richard Sandford on January 30, 2023 at 4:03 pm

    I have issues with the “while” statement with an analog variable. My sketch executes and serial mon functions until the condition is met and the led comes on. Then after the led comes on the program locks and led wont go off. I thought the sketch kept re-looping until the voltage is below the threshold and then it might fall through to the next statement? What did I do wrong.
    Here is my sketch.

    const byte switchPin = 5;
    const byte ledPin = 6;
    const byte sensorPin = A0;

    void setup() {
    pinMode(A0, INPUT);
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
    }

    void loop() {
    int volt1 = analogRead(sensorPin);
    byte voltlevel = map (volt1, 0, 1023, 0, 4);
    Serial.print(“Voltlevel =”);
    Serial.println(voltlevel);
    Serial.print(“Voltl =”);
    Serial.println(volt1);
    volt1 = digitalRead(A0);
    while (voltlevel > 3) {
    digitalWrite(ledPin, HIGH);
    }
    {
    digitalWrite(ledPin, LOW);
    }
    delay(500);

    }

    • Michael Cheich on January 30, 2023 at 6:35 pm

      Great question Richard!

      OK, once your while loop condition is met – the program will stay there because you do no re-sample the reading inside the while loop – that is you use the original mapped voltlevel reading.

  3. Robert Patton on October 24, 2024 at 4:22 am

    the sketch reads
    Serial.println(potPin);
    this prints the value of the variable potPin
    set at 14
    the A0 pin.
    with const byte sensorPin = A0;

    the value of the pot can be read with an analogeRead() function

    the line could read Serial.println(digitalRead(A0));
    that would print the value of the pot.

Leave a Comment