Get 2 separate measurements from 1 displacement sensor?

I am doing a project where i want 2 different measurments from 1 displcements sensor. I was thinking to create a new variable and just remove from the old measurment to get back to 0 but that did not work because the sensor and the Arduino ADC are ratiometric. Now I am not sure how I can achieve that, or if it is possible the way the code is written.

int potPen = A2;  //Assigning potPen to A2

float Displacement;
float newDis;

void setup() {
  pinMode(potPen, INPUT);  //Declare potPen an input
  Serial.begin(9600);
}

void loop() {
Displacement =map(analogRead(potPen),0, 1024, 0, 355);
Serial.println(Displacement);
newDis = Displacement - Displacement; 
delay(500);
newDis =map(analogRead(potPen),0, 1024, 0, 355);
Serial.println(newDis);
}

I'm not sure what it is you're asking, but I am sure that the map function returns "long", an integer datatype.

newDis = Displacement - Displacement;

How is this ever going to result in a value other than zero

If you want to measure the displacement at 2 different times then do as you suggest. Measure the current displacement and save it in a variable. Let's call it dispacement1. Later, when you measure the displacement again put the result in a different variable. Let's call that displacement2. Now you can determine the difference by subtracting one variable from the other

You can do this more neatly by putting the process in a loop

Okey thank you! Will give it a try. Is it possible to do it in 1 go because the displacement sensor is attached to an actuator. is it possible for example it start measure and after 2 seconds the new measurment starts.

Yes

take a measurement and assign it to a variable
wait 2 seconds using delay() if you don't mind the code being blocked, or millis() timing to not block the code
take a second reading

This code kinda works but still not correct, i do get 2 different values from the displacement. But I want the displacement2 to start from 0 and continue higher.

currentDisplacement = displacement2 - displacement1
Is to get the value to 0 and continue but now from 0 but it calculate it over and over since it is in a loop. How can I change to make it continue from 0? I m thinking to maybe make a if loop somehow?

int potPen = A2;  //Assigning potPen to A2

float Displacement1;
float Displacement2;
float X;
float currentDisplacement;


void setup() {
  pinMode(potPen, INPUT);  //Declare potPen an input
  Serial.begin(9600);
}

void loop() {
  Displacement1 =map(analogRead(potPen),0, 1024, 0, 355);
  delay(2000);
  Displacement2 =map(analogRead(potPen),0, 1024, 0, 355);
  currentDisplacement = Displacement2 - Displacement1;
  Serial.print(" Displacement1   ");
  Serial.println(Displacement1);
  Serial.print("   ");
  Serial.print("Displacement2:   ");
  Serial.println(Displacement2);
  Serial.print("");
  Serial.print("The number is ");
  Serial.println(X);
}

You don't need to set anything back to zero

Try this

int potPen = A2;  //Assigning potPen to A2

int currentReading = 0;
int previousReading = 0;
int currentDisplacement;

void setup()
{
  Serial.begin(115200);
  pinMode(potPen, INPUT);  //Declare potPen an input
}

void loop()
{
  currentReading = map(analogRead(potPen), 0, 1024, 0, 355);
  currentDisplacement = currentReading - previousReading;
  Serial.print(" Displacement   ");
  Serial.println(currentDisplacement);
  previousReading = currentReading;
  delay(2000);
}

Can you show a list of the Displacement1 and Displacement2 values you actually got and show what values you wanted to get instead of what you actually got?

This is a list of the value I get but for example "The number I want" the first one I want that to start at 0 and go up.

image

void loop() {

  Displacement1 =map(analogRead(potPen),0, 1024, 0, 355);
  delay(2000);
  Displacement2 =map(analogRead(potPen),0, 1024, 0, 355);

  if (Displacement1 <= Displacement2) {
    currentDisplacement = Displacement2 - Displacement1;

    Serial.print(" Displacement1   ");
    Serial.println(Displacement1);
    Serial.print("   ");
    Serial.print("Displacement2:   ");
    Serial.println(Displacement2);
    Serial.print("");
    Serial.print("The number is ");
    lastDisplacement = lastDisplacement + currentDisplacement;
    Serial.println(currentDisplacement);
    
    Serial.print("This is the one I want ");
    Serial.println(lastDisplacement);  
  }
  else {
    Serial.println("Retracting");
    Serial.print("");
    lastDisplacement = 0;
  }
}

It looks like you are trying to measure two different positions of a displacement sensor and are having trouble resetting the measurement to zero. One way to reset the measurement is to use a reference position as the zero point, and then measure the displacement relative to that position.

For example, you could add a button that the user can press to set the current position as the reference position. Then, when the button is pressed, you can store the current displacement value as the reference position and use that value as the zero point for subsequent measurements.

Here's an example of how you could modify your code to do this:

int potPen = A2;  //Assign potPen to A2
int button = 2;  //Assign a button to digital pin 2

float Displacement;
float newDis;
float referencePosition = 0;  //Initialize reference position to 0

void setup() {
  pinMode(potPen, INPUT);  //Declare potPen as an input
  pinMode(button, INPUT);  //Declare button as an input
  Serial.begin(9600);
}

void loop() {
  //Read the button state
  int buttonState = digitalRead(button);

  //If the button is pressed, set the current displacement as the reference position
  if (buttonState == HIGH) {
    referencePosition = Displacement;
  }

  //Read the displacement sensor value and map it to the range 0-355
  Displacement = map(analogRead(potPen), 0, 1024, 0, 355);

  //Subtract the reference position from the displacement to get the relative displacement
  newDis = Displacement - referencePosition;

  //Print the relative displacement
  Serial.println(newDis);
  delay(500);
}

Note that the displacement sensor and the Arduino ADC are ratiometric, which means that the output of the sensor is proportional to the supply voltage. This means that if the supply voltage changes, the output of the sensor will also change, even if the physical displacement of the sensor has not changed.

To minimize the impact of supply voltage changes on the measurement, you can try to use a regulated power supply for the Arduino and the sensor, or you can use a voltage divider to reduce the effect of supply voltage changes on the sensor output.

Okey. This is one idea that could work but I am going to connect a force sensor later and with this idea I got idea to connect them together. Would it be possible instead of the button to use the force sensor for example: if force applyed bigger then X

I will have to figure out the code for the forcesensor to arudino. But to get the displacement this can work?

if (forceReading >=10)  {
    referencePosition = Displacement;
}

Yes, it is possible to use the force sensor to reset the reference position instead of using a button. You can use an if statement to check the value of the force sensor reading and reset the reference position if the reading is above a certain threshold.

if (forceReading >= 10)
{
referencePosition = displacement;
}

This code will set the reference position to the current displacement value if the force sensor reading is greater than or equal to 10.

Keep in mind that you will need to make sure that you have properly set up and configured the force sensor to work with your Arduino board, and that you have written the code to read the sensor's values and store them in the "forceReading" variable. You may also need to adjust the threshold value (10 in this example) to a value that is appropriate for your particular application.

Additionally, you may want to consider using a low-pass filter to smooth out the force sensor readings, as they may be subject to noise and fluctuations. This can help to improve the accuracy and reliability of the measurement.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.