Back to Parent

int speakerPin = D5; // Piezo is connected to pin D5

// Defining a pin that we'll place the FSR and tilt sensor on
int fsrPin = A0;
int tiltPin = D3;

// Creating a variable to hold the FSR and Tilt sensor reading
int fsrReading = 0;
int tiltstate = 0;

// Defining a pin we'll place LEDS on- for indicting status of both FSR and Tilt Sensor
int ledFsr = D0;
int ledTilt = D2;

// Creating a variable to store the LED brightness.
int ledBrightness = 0;

bool tilt = false;
bool force = false;

void setup()

  // Set up the LEDs for output
  pinMode(ledFsr, OUTPUT);
  pinMode(ledTilt, OUTPUT);

  pinMode(tiltPin, INPUT);
  pinMode(speakerPin, OUTPUT);

  // Create a cloud variable of type integer
  // called 'force' mapped to FSR
  Spark.variable("force", &fsrReading, INT);

void loop() {

  // Use analogRead to read the FSR reading
  // This gives us a value from 0 to 4095
  fsrReading = analogRead(fsrPin);

  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  ledBrightness = map(fsrReading, 0, 4095, 0, 255);

  // fade the LED to the desired brightness
  analogWrite(ledFsr, ledBrightness);

  // wait 1/10th of a second and then loop

  tiltstate = digitalRead(tiltPin);

  // check if the tilt Sensor is active
  // if it is, the state is HIGH:
  if (tiltstate == HIGH){
    digitalWrite(ledTilt, HIGH);

    //if both tilt sensor and FSR are active then start the buzzer
    if(fsrReading > 150)
      digitalWrite(speakerPin, HIGH);
      digitalWrite(speakerPin, LOW);
  else {
    // turn LED off:
    digitalWrite(ledTilt, LOW);

Click to Expand

Content Rating

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