Skip to content

Instantly share code, notes, and snippets.

@jonhiller
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonhiller/9bf2129c0f98209f1fb8 to your computer and use it in GitHub Desktop.
Save jonhiller/9bf2129c0f98209f1fb8 to your computer and use it in GitHub Desktop.
//set the faces
#define SENSOR_FACE FACE_7
#define LIGHT_FACE FACE_2
#define LIGHT2_FACE FACE_6
int lampState; //State of the lamp (on or off)
#define OFF 0 //lamp is off
#define ON 1 //lamp is on
bool WaitingToReset; //Flag to denote whether a state change can occur
#define SENSOR_THRESHOLD 30 //when the sensor value exceeds this value (out of 255) the lamp will toggle state
#define SENSOR_HYSTERESIS 10 //When the sensor drops this much below SENSOR_THRESHOLD the lamp will be eligible to change state again.
void setup()
{
//set face directions
set_face_state(SENSOR_FACE, INPUT);
set_face_state(LIGHT_FACE, OUTPUT);
set_face_state(LIGHT2_FACE, OUTPUT);
lampState = OFF;
WaitingToReset = false;
}
void loop()
{
uint8_t sensorValue = get_face_value(SENSOR_FACE);
if (WaitingToReset == false && sensorValue > SENSOR_THRESHOLD){
WaitingToReset = true;
if (lampState == OFF){
lampState = ON;
set_face_value(LIGHT_FACE, 255);
set_face_value(LIGHT2_FACE, 255);
}
else { //if lampState == ON
lampState = OFF;
set_face_value(LIGHT_FACE, 0);
set_face_value(LIGHT2_FACE, 0);
}
}
if (sensorValue < SENSOR_THRESHOLD - SENSOR_HYSTERESIS){
WaitingToReset = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment