Back to Parent

Coding for Flex Sensor
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11;      // connect Red LED to pin 11 (PWM pin)
int fsrReading;      // the analog reading from the FSR resistor divider
int LEDbrightness;
int i=0;
int calories=0;

void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  pinMode(LEDpin, OUTPUT);
  Particle.variable("fsr", fsrReading);
  Particle.variable("reps", i);
  Particle.variable("cal", calories);
}

void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  //Serial.print("Analog reading, Reps = ");
  //Serial.println(fsrReading);
  //Serial.print(i);
  Serial.print(calories);

  if ( fsrReading > 1000)
  { i=i+1;
    calories= calories+2;
  delay(300);
  }

  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  // LED gets brighter the harder you press
  analogWrite(LEDpin, LEDbrightness);

  delay(100);

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0