Back to Parent

//Define the pin that we'll place photo cell on
int photocellpin = A0;
//Defined the pin that we'll fsr on
int fsrpin = A1;
//Define the pin that red led is on
int ledredpin = D0;
//Define the pin that green led is on
int ledgreenpin = D1;
// Create a variable to store the LED brightness.
int ledredbrightness = 0;
int ledgreenbrightness = 0;

int photocellreading = 0;
// Create a variable to hold the force reading
int fsrreading = 0;


void setup()
{
  //get to know the light value around
  Spark.variable("light", &photocellreading, INT);
  //get to know the force value
  Spark.variable("force", &fsrreading, INT);

  pinMode(photocellpin, INPUT);
  pinMode(ledredpin, OUTPUT);
  pinMode(ledgreenpin, OUTPUT);
}

void loop()
{
   //read photoresistor value
   photocellreading = analogRead(photocellpin);
   //green led brightness
   ledgreenbrightness = 255 - map (photocellreading, 0, 4095, 0, 255);
   //
   analogWrite(ledgreenpin, ledgreenbrightness);

   //read fsr value
   fsrreading = analogRead(fsrpin);
   ledredbrightness = map (fsrreading, 0, 4095, 0, 255);

   analogWrite(ledredpin, ledredbrightness);



   //if force value > 1500, then red led blinks 3 times
  if(fsrreading > 1500)
   {
     for(int i=0; i < 2; i++)
     digitalWrite(ledredpin, HIGH);
     delay(500);
     digitalWrite(ledredpin, LOW);
     delay(500);
   }
  

}
Click to Expand

Content Rating

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

0