Differences between analogRead value on Tinker and LCD/Serial values [SOLVED]

Hi everyone,
I’m playing with my Photon, a RTC module and a LCD QC1602A (v.2.0).
I used to put the Liquidcrystal library to display the time and it works great.
I tried, successfully, to send text on the lcd but I’m now stuck in a bad story with a potentiometer…
So, I’d like to show on the LCD display the value (analogRead) of an analog pin (let’s assume A5).
I try with a potentiometer but the values go from 2045 to 4095 and then from 1400 to some-what-else.
I put away the potentiometer, loaded the Tinker sketch - together with my simple sketch for the LCD - and I tried to control the value displayed on LCD by the app on my phone.
The result is that the maximum value is (I assume correctly…) 4095 but the minimum is floating between 1340 and 0340… instead of 0.
What the problem?
Thanks in advance.

Here below, the simple code.

#include "application.h"
#include "LiquidCrystal/LiquidCrystal.h" // the lcd library


//Tinker
int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);
//Tinker

int val = 0; // the value I'd like to show

LiquidCrystal lcd(D7,D6, D5, D4, D3, D2); // the pins for LCD


void setup()
{
  lcd.begin(16, 2);
  lcd.print("tinker"); // just to see the refresh...   
    
//Tinker    
  Spark.function("digitalread", tinkerDigitalRead);
  Spark.function("digitalwrite", tinkerDigitalWrite);
  Spark.function("analogread", tinkerAnalogRead);
  Spark.function("analogwrite", tinkerAnalogWrite);
//Tinker 
}

void loop()
{
  lcd.setCursor(0, 1);
  val = analogRead(A5);    // read the value from the sensor
  lcd.print(val);
  delay(200);   
}

//Tinker
int tinkerDigitalRead(String pin) {
  int pinNumber = pin.charAt(1) - '0';
  if (pinNumber< 0 || pinNumber >7) return -1;
  if(pin.startsWith("D")) {
    pinMode(pinNumber, INPUT_PULLDOWN);
    return digitalRead(pinNumber);}
  else if (pin.startsWith("A")){
    pinMode(pinNumber+10, INPUT_PULLDOWN);
    return digitalRead(pinNumber+10);}
  return -2;
  }

int tinkerDigitalWrite(String command){
  bool value = 0;
  int pinNumber = command.charAt(1) - '0';
  if (pinNumber< 0 || pinNumber >7) return -1;
  if(command.substring(3,7) == "HIGH") value = 1;
  else if(command.substring(3,6) == "LOW") value = 0;
  else return -2;
  if(command.startsWith("D")){
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, value);
    return 1;}
  else if(command.startsWith("A")){
    pinMode(pinNumber+10, OUTPUT);
    digitalWrite(pinNumber+10, value);
    return 1;}
  else return -3;
  }

int tinkerAnalogRead(String pin){
  int pinNumber = pin.charAt(1) - '0';
  if (pinNumber< 0 || pinNumber >7) return -1;
  if(pin.startsWith("D")){
    pinMode(pinNumber, INPUT);
    return analogRead(pinNumber);}
  else if (pin.startsWith("A")){
    pinMode(pinNumber+10, INPUT);
    return analogRead(pinNumber+10);}
  return -2;
  }

int tinkerAnalogWrite(String command){
  int pinNumber = command.charAt(1) - '0';
  if (pinNumber< 0 || pinNumber >7) return -1;
  String value = command.substring(3);
  if(command.startsWith("D")){
    pinMode(pinNumber, OUTPUT);
    analogWrite(pinNumber, value.toInt());
    return 1;}
  else if(command.startsWith("A")){
    pinMode(pinNumber+10, OUTPUT);
    analogWrite(pinNumber+10, value.toInt());
    return 1;}
  else return -2;
  }
//Tinker

Ok, it was a lcd digit issue: I need to put a “0” when the displayed value is less than the maximum digit.
ex.

  lcd.setCursor(0, 1);
  val = analogRead(A5); 
  lcd.print(val);
  delay(200);

This way doesn’t work. I need to add something like this

  lcd.setCursor(0, 1);
  val = analogRead(A5);
  if(val<100) {lcd.print(0);}
  lcd.print(val);
  delay(200);

In this way the value is displayed correctly in the LCD.

1 Like