Hi,
I currently have a code that records the resistance of an FSR and converts it into force readings in N but I wanted to add code that acts as trigger so I can choose when the experiment starts and to make it more user interface friendly. Does anyone know how to do that thanks?
int fsrPin = 2; // the FSR and 10K pulldown are connected to a2
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
unsigned long fsrConductance;
long fsrForce; // Finally, the resistance converted to force
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
startMillis = millis(); //initial start time
}
void loop(void) {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage == 0) {
Serial.println("No pressure");
} 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;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000; // we measure in micromhos
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
// Use the two FSR guide graphs to approximate the force
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
startMillis = currentMillis;
}
}
Serial.println("-----------------------");
delay(1000);
}
yeah so i wanted the experiment to start only when pressure is detected and only record when pressure is applied - so not just random results - so it makes it more user friendly
while not the most elegant approach, you have a condition -- 'No pressure' -- depending on fsrVoltage. why not make that a state variable and in that condition check for a minimum pressure that triggers subsequent processing by changing the state to do the else.
presumably the state can be set back to Monitor once that processing is complete or perhaps when the pressure drops below some other threshold
it's just a variable, "monitor". initially set to true or 1.
then set to false or 0 when the trigger occurs causing the else condition to be performed
not exactly sure what you're trying to do here.
this may work for you because startMillis isn't being updated and once currentMillis exceeds its value by period the condition is always true. i see startMillis is update under the else case and then only under the if fsrConductance else case.
startMillis would normally always be reset to currentMillis when the time expires.
and i see little need to do anything else if the time hasn't expired. so it make make more sense to return from loop if the time hasn't expired -- < period
are you sure that you want to have just the first line below this if-condition to be executed conditional?
void loop(void) {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
I guess you forgot to write a open curly bracket and a closing one like that
void loop(void) {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) { //test whether the period has elapsed
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
}
if (fsrVoltage == 0) {
Serial.println("No pressure");
}
what exactly shall act as the trigger?
you have to provide a precise description of the trigger
best regards Stefan