Reading and Comparing Pot Value with smoothing

Hi,

I want to

  • read the existing pot value

  • smoothen it by averaging = avg_prev_val

  • processing by any defined equations to get position in mm = pos_0

  • read the new pot value (pot will be rotated manually)

  • smoothen it by averaging = avg_new_val

  • processing by any defined equations to get position in mm = posnew

  • displacement = pos_0 - posnew

  • Serial print

I tried the attached program, but not able to get the results, can you please suggest?

Displacement measurement using Pot.txt (1.77 KB)

Why did you not post it here

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int height = 9.0;
const int posref = 16.0;
const int camangle = 124;

float voltage_0;
float theta_0;
float atheta_0;
float radtheta_0;
float pos_0;

float voltage_1;
float theta_1;
float atheta_1;
float radtheta_1;
float posnew;

float displ;
float seal_gap;

int potPin = A5;

const int numReadings = 10;
int readings[numReadings];

int prev_val = 0;
int total = 0;
int avg_prev_val = 0;

int new_val = 0;
int new_total = 0;
int avg_new_val = 0;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
  {
    total = total - readings[prev_val];
    readings[prev_val] = analogRead(potPin);
    total = total + readings[prev_val];
    prev_val = prev_val + 1;
    if (prev_val >= numReadings)
    {
      prev_val = 0;
    }
    avg_prev_val = total / numReadings;
    voltage_0 = avg_prev_val * (5.0 / 1023.0);
    theta_0 = 300.0 * voltage_0 / 5.0;
    atheta_0 = theta_0 - camangle;
    radtheta_0 = atheta_0 * (22.0 / (7.0 * 180.0));
    pos_0 = height * (tan (radtheta_0));
  }
}

void loop()
{
  {
    new_total = new_total - readings[new_val];
    readings[new_val] = analogRead(potPin);
    new_total = new_total + readings[new_val];
    new_val = new_val + 1;
    if (new_val >= numReadings)
    {
      new_val = 0;
    }
    avg_new_val = new_total / numReadings;
    voltage_1 = avg_new_val * (5.0 / 1023.0);
    theta_1 = 300.0 * voltage_1 / 5.0;
    atheta_1 = theta_1 - camangle;
    radtheta_1 = atheta_1 * (22.0 / (7.0 * 180.0));
    posnew = height * (tan (radtheta_1));
  }
  displ = pos_0 - posnew;
  Serial.println(displ);
}

What problems are you having ?

Hi,
thanks for reply.

I am not able to get the initial and new pot value.

Attached is the tinkercad image.

Hi,

if you see first line on LCD screen in attached image, I am getting value 32, which should be 325.
I have used smoothing code in void setup, to set initial position of pot.

In the code that you attached to your original post you do not print anything to the LCD

Ok
That was sample code.

Can we read the pot value in setup and then smoothen it.
Then can we read new value in loop and compare with original value?

Can we read the pot value in setup and then smoothen it.
Then can we read new value in loop and compare with original value?

You can read it in setup(). What do you mean by smoothing it ?

OK, I can read it in setup.

I mean by Smoothen is, averaging of the readings.

total = total - readings[prev_val];
readings[prev_val] = analogRead(potPin);
total = total + readings[prev_val];
prev_val = prev_val + 1;
if (prev_val >= numReadings)
{
prev_val = 0;
}
avg_prev_val = total / numReadings;

when I do this, I get value one digit less.
Eg. instead of 325 I get 32

Please post the code that exhibits the behaviour you describe

Hi,
Thanks for reply.
Meanwhile I was experimenting with little coding.
Below is the latest code (I reduced it to only query)

Question is 'how to get incremental 'avg_new_val' with respect to 'avg_prev_val'
eg. avg_prev_val is 327, and I am getting avg_new_val = 184
what is to be done to get avg_new_val = 511; (327+184)

also tinker cad circuit is here

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int potPin = A5;

const int numReadings = 10;
int readings[numReadings];

int prev_val;
int total;
int avg_prev_val;

int new_val = 0;
int new_total = 0;
int avg_new_val = 0;

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();

for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;

total = total - readings[prev_val];
readings[prev_val] = analogRead(potPin);
total = total + readings[prev_val];
prev_val = prev_val + 1;
if (prev_val >= numReadings)
{
prev_val;
}
avg_prev_val = total / numReadings;
}

}

void loop()
{
{
new_total = new_total - readings[new_val];
readings[new_val] = analogRead(potPin);
new_total = new_total + readings[new_val];
new_val = new_val + 1;
if (new_val >= numReadings)
{
new_val = 0;
}
avg_new_val = new_total / numReadings;
}

lcd.setCursor(0, 0);
lcd.print(avg_prev_val);

lcd.setCursor(0, 1);
lcd.print(avg_new_val);

}

What's all that stuff in setup, under "lcd.clear();"?

Please remember to use code tags when posting code.

under lcd.clear, it is code for averaging the input values from potentiometer.

eg.
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;

total = total - readings[prev_val];
readings[prev_val] = analogRead(potPin);
total = total + readings[prev_val];
prev_val = prev_val + 1;
if (prev_val >= numReadings)
{
prev_val;
}
avg_prev_val = total / numReadings;
}

for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;

"readings has already been zeroed.

Why are you calculating an average in setup and loop?

Please remember to use code tags when posting code

I am calculating average in setup, because I want initial reading of potentiometer.
But while getting initial reading, value from potentiometer is unstable.
Hence using averaging method.

Dear All,

thank for reading and writing reply.
I guess I got results by following code.

  • actually I am trying to use small rotary potentiometer for fine linear measurements due to compact packaging space
  • pot input signal is received and stabilised in setup using averaging method
  • operational input signal is considered in loop and stabilised by averaging method
  • result is printed to lcd screen
  • pls message if any suggestions
    thank you

below is the tinkercad link if any suggestions

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int height = 9.0;
const int posref = 16.0;
const int camangle = 124.363;

float voltage_0;
float theta_0;
float atheta_0;
float radtheta_0;
float pos_0;

float voltage_1;
float theta_1;
float atheta_1;
float radtheta_1;
float posnew;

float displ;
float seal_gap;

int potPin = A5;

const int numReadings = 10;
int readings[numReadings];

int prev_val;
int total;
int avg_prev_val;

int new_val;
int new_total;
int avg_new_val;
int val_diff;

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();

for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;

total = total - readings[prev_val];
readings[prev_val] = analogRead(potPin);
total = total + readings[prev_val];
prev_val = prev_val + 1;
if (prev_val >= numReadings)
{
prev_val;
}
avg_prev_val = total / numReadings;
voltage_0 = avg_prev_val * (5.0 / 1023.0);
theta_0 = 300.0 * voltage_0 / 5.0;
atheta_0 = camangle - theta_0;
radtheta_0 = atheta_0 * (22.0 /(7.0 * 180.0));
pos_0 = height * (tan (radtheta_0));
}
}

void loop()
{
{
new_total = new_total - readings[new_val];
readings[new_val] = analogRead(potPin);
new_total = new_total + readings[new_val];
new_val = new_val + 1;
if (new_val >= numReadings)
{
new_val = 0;
}
avg_new_val = new_total / numReadings;
val_diff = avg_prev_val + avg_new_val;
voltage_1 = val_diff * (5.0 / 1023.0);
theta_1 = 300.0 * voltage_1 / 5.0;
atheta_1 = theta_1 - camangle;
radtheta_1 = atheta_1 * (22.0 /(7.0 * 180.0));
posnew = height * (tan (radtheta_1));
displ = pos_0 - ( - posnew);
seal_gap = posref - displ;
}

lcd.setCursor(0, 0);
lcd.print(pos_0);
lcd.setCursor(6, 0);
lcd.print(posnew);

lcd.setCursor(0, 1);
lcd.print(displ);
lcd.setCursor(6, 1);
lcd.print(seal_gap);

Serial.println("Displacement");
Serial.println(displ);
Serial.println("Sealing Gap");
Serial.println(seal_gap);
delay (1);

}

val_diff = avg_prev_val + avg_new_val; Variable name suggests difference, but operator choice suggests otherwise.

Please remember to use code tags when posting code