Back to Parent

Code for CoffeeLove
// Setup LED and FSR pins
double fsrPin = A0;
int ledPin = D0;
int Red = D1;
int Blue = D2;
int Green = D3;
String Id1;
int currentPressure2;
String Scurrentpressure2="";
int ledState=0;
// Store the current FSR pressure reading
int currentPressure = 0;
String myID="";
// Set the threshold for the pressure sensor
int threshold = 650;
// General setup fun!
void setup() {
  // What's input and what's output
  pinMode( fsrPin , INPUT); // sets pin as input
  pinMode( ledPin , OUTPUT );
    pinMode( Red , OUTPUT );
      pinMode( Blue , OUTPUT );
        pinMode( Green , OUTPUT );

  Particle.subscribe(  "ass4/paired/pressure" , handlePressureChange );
  digitalWrite( ledPin, 0);
    digitalWrite( Red, 0);
      digitalWrite( Blue, 0);
        digitalWrite( Green, 0);
  // And so we can see what's happening...
  // Start the serial...
  Serial.begin(9600);
}
void loop()
{

double currentPressure = analogRead( fsrPin );
Serial.println(currentPressure);
digitalWrite( ledPin, HIGH);
 while ( currentPressure>threshold & currentPressure2<threshold )
 {

double currentPressure = analogRead( fsrPin );
   digitalWrite( Green, HIGH);
  delay(1000);
  digitalWrite(Green,LOW);
   ledState = 1;
}
 while ( currentPressure>threshold & currentPressure2>threshold ) {
   // otherwise
   double currentPressure = analogRead( fsrPin );
   digitalWrite( ledPin, LOW);
   digitalWrite( Blue, HIGH);
   delay(5000);
   digitalWrite( Red, HIGH);
   digitalWrite(Blue,LOW);
   delay(1000);
   digitalWrite(Red,LOW);
   digitalWrite( Green, HIGH);
   delay(1000);
   digitalWrite(Green, LOW);
   ledState=1;
 }
 if( currentPressure<threshold & currentPressure2>threshold )
 {
   // turn the LED On

   int ledValue = map( currentPressure, threshold, 4094, 0 , 255 );
   digitalWrite( Blue, ledValue);
 ledState=0;
}
 announceLed( ledState );
}
void handlePressureChange(const char *event, const char *data)
{
  // Display what we got
//  Serial.println( "received=" );
  Serial.println( data );
  Serial.println( event );
  // conver the data to a String type.
  String dataStr = data;
  String ScurrentPressure2= (dataStr.substring(dataStr.indexOf(','), dataStr.length()));

  // if there isn't data do nothing
  if (!data) return;
 // We only want to handle events
 // From our paired device
 // We're not interested in stuff that's come from
 // this device ...
 String myID = System.deviceID();
 // if the device that sent is this device... ignore. return exits this function
 if( dataStr.indexOf( myID ) > -1 ) // return;
 {// If we get to here, it's an event we care about

 int ledValue = map( currentPressure, threshold, 4094, 0 , 255 );
 digitalWrite( Blue, ledValue);
}
}
// Publish an event saying that
void announceLed( int ledState )
{

  String data = System.deviceID()+","+currentPressure;
  // and share as a public event
  if (ledState ==1){
  Particle.publish( "ass4/paired/pressure", data );}
}
Click to Expand

Content Rating

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

0