Mapping analog read without success

What am I doing wrong?

int potPin = A0;
uint16_t sensorValue;
uint16_t presensorValue;
const int filter = 15;
uint16_t readinginterval = 100;
unsigned long preMillis = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(potPin, INPUT);
}
void loop()
{

  sensorValue = analogRead(A0);
if (millis() - preMillis >  readinginterval)
{
if (abs(presensorValue- sensorValue) > filter)
    {
      //update to the new value
      presensorValue = sensorValue;
      sensorValue = map(sensorValue, 0, 1023, -60, 0);
    Serial.println(sensorValue);
  }
    }

}

What do you want to happen vs what actually happens ?

I note that you never update the value of premillis

I want it not to print repeated numbers and to get a smooth reading without bouncing such as
3
3
3
4
3
4
3
5
4 etc etc

void potVol()
{

  sensorValue = analogRead(A0);
if (millis() - preMillis >  readinginterval)
{
  preMillis = millis();
if (abs(presensorValue- sensorValue) > filter)
    {
      //update to the new value
      presensorValue = sensorValue;
      sensorValue = map(sensorValue, 0, 1023, -60, 6);
      Serial.println(sensorValue);  
   
  }
    }

}

Why is sensorValue an unsigned variable when you are mapping to a negative value ?

Have you fixed the timing issue ?

True this is one mistake I have fixed:


int potPin = A0;
int sensorValue;
int presensorValue;
const int filter = 15;
uint16_t readinginterval = 100;
unsigned long preMillis = 0;


void potVol()
{

  sensorValue = analogRead(A0);
if (millis() - preMillis >  readinginterval)
{
  preMillis = millis();

if (abs(presensorValue- sensorValue) > filter)
    {
    
      sensorValue = map(sensorValue, 0, 1023, -60, 6);
      Serial.println(sensorValue);  
   
  }
    }
}

Is the output that you posted actually from the sketch that you posted ?

As @UKHeliBob says, there are a few issues, not the least the code as written is very hard to determine your intention.

Using the same variable on both side of map() is not a clear choice.
preMillis is not initiaolised or updated. in loop()

sensorValue = analogRead(A0);
  // might be easier to read as 
sensorValue = analogRead(potPin);

Keep on keeping on !

what do you mean?

preMillis is declared as zero and is never changed so millis() - preMillis will always equal millis()

I have try this line: if(presensorValue != sensorValue) but seems not to help. Still printing same value over and over


void potVol()
{

  sensorValue = analogRead(potPin);
if (millis() - preMillis >  readinginterval)
{
  preMillis = millis();

if (abs(presensorValue- sensorValue) > filter)
    {
    if(presensorValue != sensorValue)
    {
      sensorValue = map(sensorValue, 0, 1023, -60, 6);
      Serial.println(sensorValue);  
   
  }
    }
}
}

I think I finally managed:

Edit: not working the below code :frowning:

void potVol()
{

  sensorValue = analogRead(potPin);
      
if (millis() - preMillis >  readinginterval)
{
  preMillis = millis();

if (abs(presensorValue- sensorValue) > filter)
    {
      presensorValue = sensorValue;
      sensorValue = map(sensorValue, 0, 1023, -60, 6);
      Serial.println(sensorValue);  
   
  }
    }
}

Please post the full sketch as you have made other changes

As the next step you should learn that auto-fornatting by pressing ctrl-t on the keyboard is a great help.

press the CTRL-key on your keyboard holdit down and press key "t" for a short moment.

Your opening and closing curly braces are scattered around without any care.
This makes your code hard to read.
It stays unclear which line of code belongs to which block

The reason why your values are "jumping" might be in the hardware.
So please post a NON-FRITZING but handdrawn schematic how yor potentiometer is wired to IO-pin A0

For smoothing out you have to add code that creates a moving average or you can use a library that is doing this

A more intuitive way for non-blocking timing is a function like used in this code

For analysing what is going on in your code you can use serial debug-output.

Anyway you should post an example with example numbers
what you exoect for values coming from the line of code

  sensorValue = analogRead(A0);

and what do you expect as final result if everything works as you want it to
please use real numbers
DON'T use words use example numbers!


int potPin = A0;
uint16_t sensorValue;
uint16_t presensorValue;
const int filter = 15;
uint16_t readinginterval = 100;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long myReadTimer = 0;                   // Timer-variables MUST be of type unsigned long


void setup() {
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  
  pinMode(potPin, INPUT);
  myReadTimer = millis(); // initialise timer
}

void loop() {
  sensorValue = analogRead(A0);
  
  if ( TimePeriodIsOver(myReadTimer,readinginterval) ) {
    Serial.print("presensorValue - sensorValue=");
    Serial.print(presensorValue - sensorValue);
    Serial.print(" > ");
    Serial.println(filter);

    Serial.print(presensorValue);
    Serial.print("     -     ");
    Serial.println (sensorValue);

    if (abs(presensorValue - sensorValue) > filter) {
      //update to the new value
      presensorValue = sensorValue;
      sensorValue = map(sensorValue, 0, 1023, -60, 0);
      Serial.println(sensorValue);
    }
  }
}

I'm expecting it values between 0 and 1023

and what do you expect as final result if everything works as you want it to
please use real numbers

I'm expecting it to print values between -60 to 6 but without printing repeated values.
so if the value is 10 the serial should not print:
10
10
10 etc

rather only one time 10

BTW presensorValue and sensor value willl never be similar for comparison, because after the map() they’re in different ranges.

I have changed and it still printing repeated numbers:



int potPin = A0;
int sensorValue;
int presensorValue;
int mapsensorValue;

const int filter = 4;
uint16_t readinginterval = 100;
unsigned long preMillis = 0;

void potVol()
{

  sensorValue = analogRead(potPin);
      
if (millis() - preMillis >  readinginterval)
{
  preMillis = millis();

if (abs(presensorValue- sensorValue) > filter)
    {
      presensorValue = sensorValue;
      mapsensorValue = map(sensorValue, 0, 1023, -60, 6);
      Serial.println(mapsensorValue);  
   
  }
    }
}

@hk_jh

if you take the time to post a

well formatted code

created through pressing a single time Ctrl-t

I will post a a working code that does what you want.

But the sketch that you posted does not even compile

How long have you tinkered around with your code without getting the code to work?

How long did you post quick postings that did not lead to a solution?

There is advice from really experienced users you should take serious and follow the advice to speeding up finding a solution quicker as with hurrying and messing up

As well as your updated code, you might put some serial.prints in to see the values you’re working with.