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.
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.
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?
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?
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.