Back to Parent

//Eunice's Lab2: Sensors+Inputs

int buttonPin = D3;     // Pin for button
int fsrPin = A0;        // Pin for force sensor
int fsrReading = 0;     // Variable to store force sensor reading
int ledGreen = D1;      // Pin for green LED (D1 performance is better than D2)
int ledBrightness = 0;  // Variable for LED brightness

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);   // Set button pin as input with internal pull-up
  pinMode(ledGreen, OUTPUT);          // Set LED pin as output
  Particle.variable("force", &fsrReading, INT);  // Register force reading to the cloud
}

void loop() {
  int buttonState = digitalRead(buttonPin);  // Read button state

  // Read force sensor value
  fsrReading = analogRead(fsrPin);
  Serial.println(fsrReading); // Allow the Terminal on Mac to test it 

  // The sensor’s metric goes up to 2500 (0-2500) to LED brightness (0-255)
  ledBrightness = map(fsrReading, 50, 2500, 0, 255);

  // Set the LED brightness using PWM
  
  if (buttonState == HIGH) {
    // When the switch is off (or the button isn’t pressed), the LED will be turned off
  digitalWrite(ledGreen, LOW);
  } 
  else {
    // When the button is pressed, then the LED will be turned on 
   analogWrite(ledGreen, ledBrightness);
    }

  // Wait 100 milliseconds before looping
  delay(100);
}
Click to Expand

Content Rating

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

0