LED Nightlight

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Description

Use the FSR to behave as a button that when pressed, turns on the LED. pressed again dims to medium. pressed again dims to low. and pressed a final time dims to off. further presses cycles through this process.

Components Used

  • Light Emitting Diode (LED)
  • FSR

Arduino Code

/*
* FSR acts as a "button" to dim an LED similar to a dimming bedside night lamp. First turns on, then dims to medium,
* then dims to low, then turns off, and repeats every time button is pressed long enough
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
* adapted by Kurt Sookwongse
*/
int fsrPin = 2; // select the input pin for the fsr
int ledPin = 9; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int currentbrightness = 0; //variable to store current value being sent to ledPin
int newbrightness = 0;// variable to store new value to write to ledPin

int count = 0; // variable to count continuous cycles FSR has been pressed
int off = 0; //various possible brightness levels for LED (0-255)
int low = 20;
int medium = 100;
int full = 255;

void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(fsrPin); // read the value from the sensor, between 0 - 1024
Serial.println(val);

if(val < 10) {count=0;}
else {count += 1;} //update how long pressure sensor has been pressed continuously

if (count == 100){ //cycle to next brightness level if button has been pressed long enough
if(currentbrightness == off) newbrightness = full;
if(currentbrightness == full) newbrightness = medium;
if(currentbrightness == medium) newbrightness = low;
if(currentbrightness == low) newbrightness = off;
analogWrite(ledPin, newbrightness); // analogWrite can be between 0-255, write newbrightness
currentbrightness=newbrightness; // update currentbrightness
}
}