Last active
August 29, 2015 14:21
-
-
Save jonhiller/9bf2129c0f98209f1fb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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