// Define the FSR Pin and create a variable to hold the FSR reading.
int fsrPin = A0;
int fsrReading = 0;
// Define the pin for the LED associated with the FSR and the LED
// associated with the proximity sensor.
int ledPin = D0;
int greenPin = A4;
// Define a variable to store the readings from the proximity sensor.
int x;
// Define the speaker pin.
int soundPin = D1;
// Time for setup. Set the LED and speaker pins to output.
// Set the proximity sensor pin (A1) to input.
void setup()
{
pinMode(ledPin, OUTPUT);
Spark.variable("force", &fsrReading, INT);
Spark.variable("x", &x, INT);
pinMode(A1, INPUT);
pinMode(soundPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
// Time to create the loop.
void loop()
{
// Read the FSR and proximity sensors.
fsrReading = analogRead(fsrPin);
delay(100);
x = analogRead(A1);
// Next is a series of if statements to control the LEDs and speaker.
// I used if statements because I wanted all of the LEDs and
// the speaker to either be on or off.
// If someone is within range of the proximity sensor, turn off the
// vacancy light and off the speaker.
if (x>900)
{
analogWrite(greenPin, 0);
analogWrite(soundPin, 0);
}
// If no one is within range of the proximity sensor, turn on the
// vacancy light.
if (x<900)
{
analogWrite(greenPin,255);
}
// If the seat is not pressing on the FSR, and no one is within
// range of the proximity sensor, turn the speaker on.
if (fsrReading<2000 && x<900)
{
analogWrite(soundPin,100);
}
// If the seat is pressing on the FSR, turn the speaker off and
// the seat is down light on.
if (fsrReading>2000)
{
analogWrite(soundPin, 0);
analogWrite(ledPin, 255);
}
// If the seat is not pressing on the FSR, turn the seat is down
// light off.
if (fsrReading<2000)
{
analogWrite(ledPin, 0);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .