Back to Parent

int fsrPin=A0;
// Create a variable to hold the FSR reading
int fsrreading;
int switchPin=D0;
int ledPin0=D2;
int ledPin1=D4;

void setup(){
  //set up switch and fsr for input, led for output
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(fsrPin, INPUT);
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  //Connecting to Cloud
  Particle.variable("force", &fsrreading, INT);

}

void loop(){
  //analogRead to read force value
  fsrreading=analogRead(fsrPin);
  //digitalRead to read whether the switch is on
  int buttonState = digitalRead(switchPin);

  if(buttonState == LOW){
    //when the pressure is relatively small, let's say under 2048
    //only one led is blinking
    if(20<fsrreading<2048){
      digitalWrite(ledPin0, HIGH);
      digitalWrite(ledPin1, LOW);

    }
    //when the pressure is over 2048
    //two leds blinking together
    if(fsrreading>2048){
      digitalWrite(ledPin0, HIGH);
      digitalWrite(ledPin1, HIGH);
    }
    //no pressure(no stuff is being placed on fsr except for interference factors)
    //no led blink
    if(fsrreading<20){
      digitalWrite(ledPin0, LOW);
      digitalWrite(ledPin1, LOW);
    }
  }else{
    //the switch is off, turn off leds
    digitalWrite(ledPin0, LOW);
    digitalWrite(ledPin1, LOW);

  }


  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