Skip to content

Instantly share code, notes, and snippets.

@booyaa
Created April 26, 2011 10:53
Show Gist options
  • Save booyaa/942109 to your computer and use it in GitHub Desktop.
Save booyaa/942109 to your computer and use it in GitHub Desktop.
Bathroom light Arduino sketch
// button to activate light
/*
* Purpose: Push button activates an LED that slow fades on and after a fixed paused, fades off.
* Change log:
* 20080202: initial release, calling this the first prototype.
* Code taken from:
* Button by DojoDave <http://www.0j0.org> - http://www.arduino.cc/en/Tutorial/Button
* Fading LED // by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
*/
// Parameters
#define STEPPER 50 // how slow to fade on and off
// #define PAUSE 300000 // five minutes
#define PAUSE 500 // how long to leave the light on
#define DEBUG 1 // see serial messages
// Vars
int inputPin = 2; // choose the input pin (for a pushbutton)
int switchVal = 0; // variable for reading the pin status
int switchOn = 0; // switch is set to off
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
void setup() {
pinMode(inputPin, INPUT); // declare pushbutton as input
if ( DEBUG )
{
Serial.begin(9600);
}
}
void loop(){
switchVal = digitalRead(inputPin); // read input value
if (switchVal == LOW ) { // check if the input is LOW (pushed in)
switchOn = 1;
if (DEBUG)
{
Serial.print("Turning on switch for ");
Serial.print(PAUSE);
Serial.println(" milliseconds.");
}
lightOn();
delay(PAUSE);
if (DEBUG)
{
Serial.println("Turning off switch");
}
switchOn = 0;
lightOff();
}
}
// supporting functions
void lightOn()
{
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(STEPPER); // waits for STEPPER milli seconds to see the dimming effect
}
}
void lightOff()
{
for(value = 255; value >=0; value-=1) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(STEPPER);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment