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);
}