Add code for Dave Astels' humidity alarm project.

This commit is contained in:
Dave Astels 2018-05-20 17:15:23 -04:00
parent fd33b3307a
commit c84bbc564a
2 changed files with 121 additions and 0 deletions

21
Humidity_Alarm/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Dave Astels
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,100 @@
// Humidity Monitor
// Copyright (C) 2017 Dave Astels
// Released until the MIT license
//
// The trinket is controlled by an external Power Timer Breakout
// (https://www.adafruit.com/product/3435)
// 1. Every however often, the timer will power up the trinket which will run
// this file.
// 2. Relative humidity is checked
// - if it's within range, the trinket tells the timer to shut it down and start
// the timing cycle
// - if it's out of range, it beeps & flashes the neopixel for 2 seconds, then
// sleeps for 10 seconds, then goes to 2
#include "Adafruit_Si7021.h"
#include <Adafruit_DotStar.h>
#include <SPI.h>
const unsigned long falling_tones[] = { 2000, 1980, 1960, 1940, 1920, 1900, 1880, 1860, 1840, 1820, 1800, 1780, 1760, 1740, 1720, 1700, 1680, 1660, 1640, 1620 };
const unsigned long rising_tones[] = { 2000, 2020, 2040, 2060, 2080, 3000, 3020, 3040, 3060, 3080, 4000, 4020, 4040, 4060, 4080, 5000, 5020, 5040, 5060, 5080 };
const int dotstar_data_pin = 7;
const int dotstar_clock_pin = 8;
const int sound_pin = 3;
const int sleep_pin = 4;
const int target_humidity = 61.0;
const unsigned long alert_interval = 20000;
const unsigned long alert_duration = 500;
const unsigned long comparison_delay = alert_interval - alert_duration;
const unsigned long number_of_freq_steps = 20;
const unsigned long alert_freq_step_time = alert_duration / number_of_freq_steps;
Adafruit_Si7021 sensor = Adafruit_Si7021();
//Adafruit_DotStar strip = Adafruit_DotStar(1, DOTSTAR_BRG);
//--------------------------------------------------------------------------------
// Sound the alert.
void chirp(boolean direction)
{
const unsigned long *freqs = direction ? rising_tones : falling_tones;
for (int i = 0; i < number_of_freq_steps; i++) {
tone(sound_pin, freqs[i]);
delay(alert_freq_step_time);
}
noTone(sound_pin);
}
//--------------------------------------------------------------------------------
// Measure relative humidity and return whether it is under/in/over range.
int check_rh()
{
float relative_humidity = sensor.readHumidity();
if (relative_humidity < (target_humidity - 5)) {
return -1;
} else if (relative_humidity > (target_humidity + 5)) {
return 1;
} else {
return 0;
}
}
//--------------------------------------------------------------------------------
// Repeatedly check the humidity and light the neopixel and sound the buzzer as
// appropriate, for as long as the reading is out of range.
// Initial comparison result is passed in.
void warn_if_out_of_range(int comparison)
{
while (comparison != 0) {
chirp(comparison > 0);
delay(comparison_delay);
comparison = check_rh();
}
}
void setup()
{
pinMode(sleep_pin, OUTPUT);
digitalWrite(sleep_pin, LOW);
// strip.setPixelColor(0, 0);
// strip.show();
sensor.begin();
warn_if_out_of_range(check_rh());
}
void loop()
{
digitalWrite(sleep_pin, HIGH);
delay(50);
digitalWrite(sleep_pin, LOW);
delay(50);
}