Weird reading of potentiometer

Hey guys

When i try to:

 potValue = analogRead(A0);
speed = map(potValue, 0, 1023, 25, 150);

It does not map the value between 25 and 150..

But if I:

potValue = analogRead(A0);
speed = map(potValue, 15, 1010, 25, 150);

It does map the value between 25 - 150. But still some times goes from 26 - 149/151.

How can that be?? Can it be a bad potentiometer?

I did some smoothing, and that got me so far. Before that, it was unstable AF.

I hope somebody can explain it to me.

//Thomas

What are the input values and what do you get out of the function for that input values?

You've only said what the first version doesn't do. But it would be more interesting to know what it does.

Put a Serial.print in to show exactly what values you are getting into potValue.

Steve

You expect some movement in values from the pot.

The Arduino A/D is comparing the pot voltage to the supply voltage , so if you have a noisy supply , long leads, prototyping board with bad connections you can expect some changes in values .
There are also “ rounding “ errors in the ADC and the map function, ( it’s integer maths and the values you have don’t map across exactly the range in a whole number of bits, so the top and bottom values may be out ) +- 1 digit is good going.

I have attached some screendumps of the Serial monitor with the IDE next to it, so you can see what happens with which changes. And of cause the code (sorry if it bad coding, I'm totally new into this).

// Diverse fælles
unsigned long tempo;
bool vibratoState = LOW;
int valveState;
unsigned long lastDebounceTime;
unsigned long debounceDelay = 20;

// Button stuff
int buttonPin = A2;
int buttonCurrent;
int buttonPrevious = HIGH;
int valveButton = A1;

//LCD screen stuff
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Potentionmeter stuff
#include <ResponsiveAnalogRead.h>
int potPin = A0;
//int potValue = 0;
ResponsiveAnalogRead potValue(potPin, true);

// LED stuff
int ledPin = 13;
int ledState = LOW;
unsigned long ledPrevMillis = 0;
int ledBPM;

// StepperMotor Stuff



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16, 2);
  /*
    lcd.setCursor(2, 0);
    lcd.print("K.E. DYHLENS");
    lcd.setCursor(1, 1);
    lcd.print("VIBRAFONFABRIK");
    delay(4000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("MASTER  VIBRAFON");
    lcd.setCursor(2, 1);
    lcd.print("INITIALIZING");
  */
  pinMode(buttonPin, INPUT_PULLUP);    // Button setup
  pinMode(valveButton, INPUT);         // Buttons for valve degrees
  pinMode(potPin, INPUT);              // Pot setup
  pinMode(ledPin, OUTPUT);             // LED setup
  digitalWrite(ledPin, HIGH);
  /*
    delay(6000);
    lcd.clear();
    lcd.print("SPIL ORDENLIGT!");
    delay(3000);
    digitalWrite(ledPin, LOW);
    lcd.clear();
    delay(1000);
  */
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonRead = digitalRead(buttonPin);  // Reading push button
  valveState = analogRead(valveButton);

  potValue.update();            // Reading potentiometer

  tempo = map(potValue.getValue(), 13 , 1015, 25, 150); // Mapping potentiometer to go from 25 - 150 bpm/rpm
 // Serial.print("RAW potValue: ");
//  Serial.print(analogRead(potPin));
//  Serial.print("  -  Smoothed potValue: ");
//  Serial.print(potValue.getValue());
 // Serial.print("  -  Mapped potValue: ");
  Serial.println(tempo);
//  delay(100);
  lcdScreen();

  if (buttonRead != buttonPrevious) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (buttonRead != buttonCurrent) {
      buttonCurrent = buttonRead;

      if (buttonCurrent == HIGH) {
        vibratoState = !vibratoState;
      }

      if (vibratoState == HIGH) {
        vibratoState = LOW;
      }
      else {
        vibratoState = HIGH;
      }
    }
  }

  buttonPrevious = buttonRead;

  if (vibratoState == LOW) {
    vibratoOff();
  }

  else {
    vibratoOn();
  }

}


// Functions //
void vibratoOff() {
  ledBlinkOff();
}

void vibratoOn() {
  ledBlinkOn();
}

//LCD Screen
void lcdScreen() {

  lcd.setCursor(4, 0);
  if (tempo < 100) {
    lcd.print(" ");
    lcd.print(tempo);
    lcd.print(" ");
  }
  else {
    lcd.print(tempo);
    lcd.print(" ");
  }

  lcd.setCursor(8, 0);
  lcd.print("RPM");

  lcd.setCursor(0, 1);
  lcd.print("Rotors: ");
  if (valveState == 0 ) {
    lcd.print("Open    ");
  } if (valveState == 511) {
    lcd.print("50% Open");
  } if (valveState == 682) {
    lcd.print("Closed  ");
  }

}


//LED Blinker
void ledBlinkOn() {
  unsigned long ledCurrentMillis = millis();

  int ledBPM = (1000 / tempo) * 60 / 2 / 2;

  if (ledCurrentMillis - ledPrevMillis >= ledBPM) {
    ledPrevMillis += ledBPM;

    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    digitalWrite(ledPin, ledState);
  }

}

void ledBlinkOff() {
  digitalWrite(ledPin, HIGH);
}

//Motor homing/initializing


//Motor running

2 more pics..

and one more..

and the last one. A picture of my test setup.

IMG_0244.jpg|0x0

WOuld you mind posting that as text? And what values did you expect for what input value and what did you get?

Initially you claimed that a simple analogRead() followed by a map() is wrong but you never said in what way it was wrong.

And that's not what the ever-changing code in your pictures shows at all. You're using some library that I've never heard of to do the reads and depending on how that works what you're printing may not be the value that was used in the map.

Do you actually have any results from what you were originally complaining about?

Steve

@slipstick
I just asked how come a mapping didn't map potValue, until I reduced the pot's range from 0-1023 to 15-1010, because I was curious to learn if I have misunderstood the mapping function..

But at the very first beginning of my project (before any smoothening), the analogRead was ranging from 35-1018, flicking like hell, and would get even worse when I started the blink function of the led. So, because I need the blink rate, and soon the speed of a motor, to be stable and reliable, I searched a lot for a way to learn about smoothening. I found a video on YouTube (and an explanation I understood) of the library I now use to smooth the analog reading, and it helps a lot.
I just wondered why the mapping wasn't working the way I expected.

It's difficult to answer your question because although I've done many analogRead()s followed by a map() the map() has always done exactly what I expect it to.

So let's get back to basics. Do your smoothed pot results reliably show 0 at one end of travel and 1023 at the other end? If not what do you see?

With map left at 0-1023 to 25-150 please provide a range of input values and corresponding results and explain what different results you were expecting.

Steve

ResponsiveAnalogRead.h
ResponsiveAnalogRead potValue(potPin, true);

Do you need that?
Why?
Did you test this on it's own?
Does it return 0-1023?
Does it map ok?
Which Arduino are you using? (image of post#7 does not work).
Leo..

The TO still did not give proof that the map() function does not work correctly. So this all poking in the dark and solvoing a problem that does not exist.

Did the OP ever read my post and may did a bit of thinking around it ??