Skip to content

Instantly share code, notes, and snippets.

@garethfoote
Created November 12, 2015 18:45
Show Gist options
  • Select an option

  • Save garethfoote/f138c694f01a4241c0ae to your computer and use it in GitHub Desktop.

Select an option

Save garethfoote/f138c694f01a4241c0ae to your computer and use it in GitHub Desktop.
/*
* Fade - Using a pressure sensor to fade an LED on.
* @author Gareth Foote & Berit Greinke
* 12th November 2015
*/
int sensorPin = 1; // The number of the input pin for the pressure sensor
int ledPin = 0; // The number of the output pin for the LED
void setup() {
// Tell the Gemma that the LED pin is an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the value of the pressure sensor (variable resistor).
// analogRead() returns a range of values.
// The value increases the more pressure is
// applied to our sensor.
int sensorReading = analogRead(sensorPin);
// The range of values we get from our sensorReading
// will vary depending on the resistance of our pressure
// sensor. We estimate that the range is approximately
// between 0 and 50 depending on the pressure sensor.
// The LED brightness is controlled by passing a value
// between 0 and 255. We therefore need to convert/map our
// sensorReading to this range.
int brightness = map(sensorReading, 0, 50, 0, 255);
brightness = min(brightness, 255);
// We then tell the ledPin to turn on at this brightness.
analogWrite(ledPin, brightness);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment