Reading force and position with an FSLP and Arduino
As discussed in previous sections, our code needs to alternate between two "modes" to read position and force. By switching pin D3 to INPUT mode, we'll effectively disconnect the reference resistor when measuring position.
The sketch below reads position and force, and displays both in a serial terminal window. No reports will be written to the terminal if no force is detected on the FSLP to avoid spurious position values while the wiper is floating.
⬇ Download LinearPotExample.zip (Arduino Project)
/**********************************************************************************************************
* Project: LinearPotExample.ino
* By: Chris Wittmier @ Sensitronics LLC
* LastRev: 07/28/2014
* Description: Basic demonstration of Force Sensing Linear Potentiometer to accompany web tutorial.
* Relative force and position readings are output to a serial terminal.
**********************************************************************************************************/
#define PIN_RIGHT 2
#define PIN_WIPER A0
#define PIN_REFERENCE 3
#define SERIAL_BAUD_RATE 115200
#define PER_CYCLE_DELAY 25
#define TOUCH_THRESH 25
void setup()
{
Serial.begin(SERIAL_BAUD_RATE);
pinMode(PIN_RIGHT, OUTPUT);
pinMode(PIN_REFERENCE, INPUT);
pinMode(PIN_WIPER, INPUT);
}
void loop()
{
/*** First read force ***/
pinMode(PIN_REFERENCE, OUTPUT);
digitalWrite(PIN_REFERENCE, LOW);
digitalWrite(PIN_RIGHT, HIGH);
delay(1);
int force_reading = analogRead(PIN_WIPER);
/*** Now read position ***/
pinMode(PIN_REFERENCE, INPUT);
digitalWrite(PIN_RIGHT, LOW);
delay(1);
int position_reading = 1023 - analogRead(PIN_WIPER);
position_reading -= 512;
/*** If readings are valid, output position and force ***/
if(force_reading >= TOUCH_THRESH)
{
Serial.print("Position: ");
printFixed(position_reading, 3, true);
Serial.print(" Force: ");
printFixed(force_reading, 3, false);
Serial.println();
}
else
{
Serial.println("Position: Force: 0");
}
delay(PER_CYCLE_DELAY);
}
void printFixed(int value, int digit_places, boolean show_sign)
{
if(value < 0)
{
Serial.print("-");
value = abs(value);
}
else if(show_sign)
{
Serial.print("+");
}
int compare = 10;
for(int i = 0; i < digit_places - 1; i ++)
{
if(value < compare) { Serial.print(" "); }
compare *= 10;
}
Serial.print(value);
}
With the code uploaded and running, we should be in business.
In the final section, we'll take a look at the results...