Back to Parent

int forcePin = A0; //map the FSR sensor to pin A0
int fanPin = D0; //map the fan to pin D0
int thresholdValue = 2000; //trigger the fan when detected pressure is larger than this number
int forceRead; //the detected pressure
String state; //define when the fan should be on and should be off
int condition = 0

void setup() {
  Serial.begin(9600);
  pinMode(forcePin, INPUT);
  pinMode(fanPin, OUTPUT);
  Particle.variable ("track",forceRead);
  Particle.subscribe("K-Aurapress-detected", myHandler);
}
//subscribe to the other device while the coupled should subscribe to "Y-Aurapress-detected"

void loop(){
  forceRead = analogRead(forcePin);
  if(forceRead > thresholdValue and condition == 0){
    state = "yes";
    Particle.publish("Y-Aurapress-detected", state);
    condition == 1;
    delay(600);
//publish "Y" event when the FSR senses pressure to trigger the fan
  }else if(forceRead <= thresholdValue and condition == 1){
    state = "no";
    condition == 0;
    Particle.publish("Y-Aurapress-detected", state);
    delay(600);
//publish "Y" event again when the FSR cannot sense any pressure to stop the fan
  }
}

void myHandler(const char *event, const char *data){
  String has = String(data);
  if (has == "yes"){
    spinthefan();
  }else if (has == "no"){
    stopthefan();
  }
}
void spinthefan(){
  digitalWrite(fanPin,HIGH);
}
void stopthefan(){
  digitalWrite(fanPin,LOW);
}
Click to Expand

Content Rating

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

0