int green_pin = D0; // RED pin of the LED to PWM pin **A4**
int red_pin = D1; // GREEN pin of the LED to PWM pin **D0**
int hall_effect_pin = D3; // Hall Effect Sensor Pin
int hall_effect_read = 0;
int door_status = 0; // Used to indicate door status
int prev_door = 0; // as above
int door_was_opened = 0; // singular int used to vary light
void setup()
{
// Set up our pins for output
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(hall_effect_pin, INPUT);
digitalWrite(red_pin, LOW);
digitalWrite(green_pin, LOW);
Serial.begin(9600);
Particle.variable("DoorOpened", hall_effect_read); // Used for IFTTT
}
void loop() {
hall_effect_read = digitalRead(hall_effect_pin);
// If the Hall Effect sensor detects the absence of a magnet, Photon sends a
// publication to the website
if (hall_effect_read == HIGH) {
door_status = 1;
door_was_opened = 1;
if (door_status == 1 && prev_door == 0) { // Door just opened
Particle.publish("DoorOpened", "HIGH");
}
}
// Reset door status to 0
if (hall_effect_read == LOW) {
door_status = 0;
}
if (hall_effect_read == LOW && door_was_opened == 0) {
digitalWrite(green_pin, HIGH);
digitalWrite(red_pin, LOW);
}
else {
digitalWrite(red_pin, HIGH);
digitalWrite(green_pin, LOW);
}
prev_door = door_status;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .