Calibrating the Range

While the light is now fading, chances are its response isn't quite perfect. To adjust the response, we need to add one more line to our code:

     map(value, fromLow, fromHigh, toLow, toHigh)

To calibrate our sensor, we can use the serial monitor like in CIRC-11. Open the serial monitor, then replace the fromLow value with the value display when the sensor is fully pressed.

Then, replace the fromHigh value with the unpressed value. 

Finally, fill in the range toLow = 0 and toHigh = 255

The result will look something like this:

int value = analogRead(sensePin);
map(value, 125, 854, 0, 255);
analogWrite(ledPin, value);

The RGB Strongperson Test

Step right up to test your strength against the Adafruit Metro High Striker Game! Only the strongest will succeed! Let's quickly modify the circuit with a RGB LED to indicate strength-level and modify the code to display how hard someone is pressing the force-sensitive resistor. 

Diagram: RGB + Force Resistor

The code below will need to be first run, and then modified. After running it, open the Serial Monitor and press the force sensitive resistor as hard as possible. The value printed is the maxForce that someone could press on the FSR on your circuit. Set maxForce to the value in your serial monitor:

     // set maxForce
     int maxForce = FORCEVALUE;

After setting the maxForce, try your luck! We've included a serial printout to show how hard you're depressing the FSR. 

/*
  FSR Strongperson Test for Metro (and Metro Express)
  
  Utilizes a  FSR and a RGB LED to test how strong you are.

  Created 7 July 2017
  By Brent Rubell for Adafruit Industries
  Support Open Source ~ buy Adafruit 

*/

// fsr pin
int sensePin = A2; 
// rgb led pins
int rgbLED[] = {9, 10, 11};

// common cathode rgbleds
const boolean ON = LOW;
const boolean OFF = HIGH;

//  predefined colors 
const boolean BLUE[] = {ON, OFF, OFF}; 
const boolean RED[] = {OFF, OFF, ON}; 
const boolean GREEN[] = {OFF, ON, OFF};

void setup() {
 Serial.begin(9600);
 
 // set all rgb pins as outputs  
 for(int i = 0; i<3; i++){
  pinMode(rgbLED[i], OUTPUT);
 }
}

void loop() {
 // scale voltage down to 255 from 1023
 int force = analogRead(sensePin) / 4; 
 // check maximum force by squeezing the FSR 
 Serial.println(force);
 
 // set maxForce
 int maxForce = 160;
 
 // calculate regions
 int lowForce = maxForce / 3;
 int medForce = (maxForce / 3) * 2;
 // check force regions
 if(force < lowForce) {
  Serial.println("easy");
  setColor(rgbLED, RED);
 }
 else if (force > lowForce && force < medForce) {
  Serial.println("medium");
  setColor(rgbLED, BLUE);
 }
 else {
  Serial.println("hard");
  setColor(rgbLED, GREEN);
 }
 
}

// rgb color mixing
void setColor(int* led, const boolean* color) {
  for(int i = 0; i < 3; i++){
    digitalWrite(led[i], color[i]);
  }
}

Other Applications

With sensors, the real fun comes in how you use them in neat and unexpected ways. So get thinking about how and where sensing force could enhance your life (or the life of others). 

This guide was first published on Aug 18, 2017. It was last updated on Jun 12, 2017.

This page (Make It Better) was last updated on Jun 27, 2017.

Text editor powered by tinymce.