-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforceSensorAruduinoCode.txt
More file actions
89 lines (71 loc) · 3.37 KB
/
forceSensorAruduinoCode.txt
File metadata and controls
89 lines (71 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Team Tech Force and Strain Sensor
CIRCUIT SETUP INSTRUCTIONS:
Connect one end of FSR to power, the other end to Analog 0.
Then connect one end of a 10K ohm resistor from Analog 0 to ground.
Connect one end of the strain to power, the other end to Analog 1.
Then connect one end of a 470 ohm resistor from Analog 1 to ground.
For more information on force sensor part see https://learn.adafruit.com/force-sensitive-resistor-fsr/using-an-fsr
www.ladyada.net/learn/sensors/fsr.html */
int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrReading; // the analog reading from the FSR resistor divider
int fsrVoltage; // the analog reading converted to voltage
unsigned long fsrResistance; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance;
long fsrForce; // Finally, the resistance converted to force (NEWTONS)
int timeIncrement = 1000; //in ms (delay between loops)
int strainPin = 1; //analog pin (a1) that strain sensor is connected to
double strain = -99; //no units, just relative magnitudes
int timestep = 0; // start at 0 seconds
long lastForce = -99; //initiate prev force reading recorded
double lastStrain = -99; //initiate prev strain reading recorded
//Hyperparameters
double F_THRESHOLD = 0; //The min change in force (N) reading for us to record that data point
double S_THRESHOLD = 0; //The min change in strain reading for us to record that data point
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
/*SET UP COL NAMES*/
Serial.println("'Time', 'Force', 'Strain'");
}
void loop(void) {
/*GET PRESSURE READING (force in newtons)*/
fsrReading = analogRead(fsrPin);
/*CONVERTING FSR_READING INTO FORCE IN NEWTONS */
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
if (fsrVoltage == 0) { //prevent divide by 0 error
//No pressure
fsrForce = 0;
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V so FSR = ((Vcc - V) * R) / V
fsrResistance = 5000 - fsrVoltage; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance *= 10000; // 10K resistor
fsrResistance /= fsrVoltage;
fsrConductance = 1000000; // we measure in micromhos so
fsrConductance /= fsrResistance;
// Use the two FSR guide graphs to approximate the force (N)
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
}
}
/*GET STRAIN READING*/
strain = analogRead(strainPin);
/*ONLY OUTPUT TO SERIAL MONITOR AND KEEP DATA IF CHANGE IS GREATER THAN THRES
or if it's the first reading*/
double deltaForce = abs(fsrForce - lastForce);
double deltaStrain = abs(strain - lastStrain);
if ( timestep == 0 || deltaStrain >= S_THRESHOLD || deltaForce >= F_THRESHOLD){
//time, fsrForce, strain \n
Serial.print(timestep);
Serial.print(",");
Serial.print(fsrForce); //if fsrForce always = 0, use fsrReading (not in Newtons anymore)
Serial.print(",");
Serial.println(strain);
lastForce = fsrForce; //change these to fsrReading if changed in line 3 above
lastStrain = strain;
}
delay(timeIncrement); //sample by seconds (1 sec = 1000 ms)
timestep = timestep + 1;
}