Printing a single line of Text from a FSR threshold

I'm doing a project where I need Arduino to send a single line of text to the monitor (eventually to Isadora) when the reading from an FSR goes above a certain threshold, and not print again until the FSR reads over the threshold again.

Example: Someone steps on the FSR, so Arduino prints the text. The person continues standing on the FSR, so arduino does not send another. The person leaves, changing the reading from the FSR, but it's under the threshold, so arduino doesn't print the text. Another person steps on the FSR so Arduino prints the text.

So far this is what I have (code for 4 analog FSR sensors):

void setup(void) {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

int Sens0 = analogRead(A0);
int Sens1 = analogRead(A1);
int Sens2 = analogRead(A2);
int Sens3 = analogRead(A3);
//read Analog inputs for pressure plates and assign them variables

int threshold = 300; //Threshold for how much pressure is applied

int pressure0 = 0;  //False
int pressure1 = 0;  //False
int pressure2 = 0;  //False
int pressure3 = 0;  //False
//Changing variables to trigger the "Shuffle" order

int Sensor0State = 0;  //Current state of Sensor 0
int lastSensor0State = 0; //Last Recorded state of sensor 0

int Sensor1State = 0;  //Current state of Sensor 1
int lastSensor1State = 0; //Last Recorded state of sensor 1

int Sensor2State = 0;  //Current state of Sensor 2
int lastSensor2State = 0; //Last Recorded state of sensor 2

int Sensor3State = 0;  //Current state of Sensor 3
int lastSensor3State = 0; //Last Recorded state of sensor 3
//Defining current and previous state variables

void loop(void) {

//////////////////////////////////////////////////////////////////////////////////////
//SENSOR 0 PRESSURE PRINT CODE
if (Sens0 > threshold){
  pressure0 = 1;
  if (Sens0 < threshold) {
    pressure0 = 0;
  }
}
//Set pressure0 variable to "true" if the sensor is reading higher than the threshold

Sensor0State = pressure0; //Set Sensor state to the current state of the pressure0 variable

if (Sensor0State != lastSensor0State) {
  //Check the sensor state against the last previous state
  if (Sensor0State == 1){
    // Check if the Sensor is reading more than 300
    Serial.println(" SHUFFLE ");
    // Print the Shuffle Order to Isadora
    } 
  } 
  delay(50);
  // Set a delay to avoid tripping over itself

lastSensor0State = Sensor0State;
// Save current state to previous state

//////////////////////////////////////////////////////////////////////////////////////
// SENSOR 1 PRESSURE PRINT CODE

if (Sens1 > threshold){
  pressure1 = 1;
  if (Sens1 < threshold) {
    pressure1 = 0;
  }
}
//Set pressure1 variable to "true" if the sensor is reading higher than the threshold

Sensor1State = pressure1; //Set Sensor state to the current state of the pressure1 variable

if (Sensor1State != lastSensor1State) {
  //Check the sensor state against the last previous state
  if (Sensor1State == 1){
    // Check if the Sensor is reading more than 300
    Serial.println(" SHUFFLE ");
    // Print the Shuffle Order to Isadora
    } 
  } 
  delay(50);
  // Set a delay to avoid tripping over itself

lastSensor1State = Sensor1State;
// Save current state to previous state

//////////////////////////////////////////////////////////////////////////////////////
// SENSOR 2 PRESSURE PRINT CODE

if (Sens2 > threshold){
  pressure2 = 1;
  if (Sens2 < threshold) {
    pressure2 = 0;
  }
}
//Set pressure2 variable to "true" if the sensor is reading higher than the threshold

Sensor2State = pressure2; //Set Sensor state to the current state of the pressure2 variable

if (Sensor2State != lastSensor2State) {
  //Check the sensor state against the last previous state
  if (Sensor2State == 1){
    // Check if the Sensor is reading more than 300
    Serial.println(" SHUFFLE ");
    // Print the Shuffle Order to Isadora
    } 
  } 
  delay(50);
  // Set a delay to avoid tripping over itself

lastSensor2State = Sensor2State;
// Save current state to previous state

//////////////////////////////////////////////////////////////////////////////////////
// SENSOR 3 PRESSURE PRINT CODE

if (Sens3 > threshold){
  pressure3 = 1;
  if (Sens3 < threshold) {
    pressure3 = 0;
  }
}
//Set pressure3 variable to "true" if the sensor is reading higher than the threshold

Sensor3State = pressure3; //Set Sensor state to the current state of the pressure3 variable

if (Sensor3State != lastSensor3State) {
  //Check the sensor state against the last previous state
  if (Sensor3State == 1){
    // Check if the Sensor is reading more than 300
    Serial.println(" SHUFFLE ");
    // Print the Shuffle Order to Isadora
    } 
  } 
  delay(50);
  // Set a delay to avoid tripping over itself

lastSensor3State = Sensor3State;
// Save current state to previous state

}

/////////////////////

This all follows a logic, but it's not printing to the monitor. Am I missing something?

Incorrect.

Where in the loop() does your code read the Analog pins?

There the analog sensors are read one time, at the variables creation.

if this code

Sens0 = analogRead(A0);
Sens1 = analogRead(A1);
Sens2 = analogRead(A2);
Sens3 = analogRead(A3);

was in the loop() then each pass of the loop() the sensors value would be updated.

That worked for the most part, but now I'm running into the issue where the text will print once, but won't print after the first round. THey'll all get through a single loop, but they won't print again.

Any suggestions? I didn't even think about the variables not being in the loop. I was thinking since the readings would be changing, they'd continuously update. Thanks for the tip!

Edit: I got it working. I needed to amend my if/else statement. Rather than 2 "if" statements, it worked when I changed it to resetting the pressure variable with an else clause.

It may make your life easier by creating an array[ ] of sensors.

The code will be a lot shorter, then you ca rashly see where to set a persistent flag when the value goes high, then print the notification.
When the value drops, clear the flag, then it’s ready to look for highs again.

I'm still pretty new at all of this, so maybe for next time I'll learn how to use arrays. I wanted the freedom to have each of them operate independently for now, just in case I want them to do different things later down the line.

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