Back to Parent

int ledPin = D0;
int warmPin = D1;
int fsrPin = A0;
int fsrReading = 0;
int pressureValue = 0;
int reading = 0;
int press = 0;
int brightness = 0;
int speed = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(warmPin, OUTPUT);
  pinMode(fsrPin, INPUT);

  Spark.variable(  "pressure" , &fsrReading , INT );  // From all devices!

  Spark.subscribe(  "db2015/hugged" , bearGlow );  // From all devices!
  //Spark.subscribe(  "db2015/servo-changed" , handleServoChange , MY_DEVICES);  // From your devices
  //Spark.subscribe(  "db2015/servo-changed" , handleServoChange , "53ff71066667574831282367");  // From a specific device


}

void loop()
{
  // read the value
  reading = analogRead(fsrPin);
  
  if( reading != fsrReading ){
    publishBearChange();
  }
  
  fsrReading = reading; 
  
  if( pressureValue > 0 ){
  
     brightness=map(pressureValue, 0, 4095, 0, 255);
     speed=map(pressureValue, 0, 4095, 3000,100);
  
      analogWrite(warmPin, brightness);
      analogWrite(ledPin, 255);
      delay(speed);
      analogWrite(ledPin, 0);
    delay(speed);
  }else{
    delay( 1000 );
  }
}

void bearGlow (const char *event, const char *data)
{
  // if there isn't data do nothing
  if (!data) return;
  
  if( strcmp( data, "STOP" ) == true )
  {
    pressureValue = -1;  
    return;
  }    
    
    
  press = atoi( data );
  
  if (!press) return;
  
  pressureValue = press;
  
  /*
  
  int brightness=map(press, 0, 4095, 0, 255);
  int speed=map(press, 0, 4095, 2000,500);
  
  analogWrite(warmPin, brightness);
  for( int i = 0 ; i < 5; i++ ){
  analogWrite(ledPin, 255);
  delay(speed);
  analogWrite(ledPin, 0);
  delay(speed);
  }
  */
}


// Publish an event saying that the
// Doorbell has been pushed
void publishBearChange()
{
  String data = "" ;
  data = data + fsrReading;
  if (fsrReading > 0)
    Spark.publish( "db2015/hugged", "" + data );
  else 
    Spark.publish( "db2015/hugged", "STOP" );
  
}
Click to Expand

Content Rating

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

0