Last active
April 20, 2018 13:43
-
-
Save christophervigliotti/da55a0f62d0e781ed32c9f1e98aa4f3e to your computer and use it in GitHub Desktop.
An Arduino project that my son and I collaborated on.
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
/* | |
* wot | |
* water level & low temp alert | |
* | |
* status | |
* water level sensor | |
* https://www.fasttech.com/p/3809800 | |
* works | |
* active buzzer | |
* https://www.fasttech.com/p/1219302 | |
* just clicks (wired incorrectly?) | |
* temp module | |
* https://www.fasttech.com/p/1012005 | |
* not started | |
* | |
* inspired by | |
* https://www.youtube.com/watch?v=3Qvdl0q9soE (sensor values) | |
* and | |
* https://tkkrlab.nl/wiki/Arduino_KY-012_Active_buzzer_module (wiring the buzzer) | |
* and | |
* http://www.mauroalfieri.it/elettronica/funduino-water-sensor-test.html () | |
*/ | |
// temp sensor setup | |
#include <dht.h> | |
dht DHT; | |
#define DHT11_PIN 7 | |
int minimumTemperature = 50; | |
int theTemperature = 65; | |
const int analogInPin = A0; | |
const int analogOutPin = 9; | |
int sensorValue = 0; | |
int outputValue = 0; | |
int minimumSensorValue = 200; | |
int minimumOutputValue = 50; | |
int speakerPin = 8; | |
bool detectsWater = false; | |
int debugToConsole = true; | |
// this function is called once at the beginning | |
void setup() { | |
if(debugToConsole){ | |
Serial.println(""); | |
Serial.println("* * * * * setup() * * * * *"); | |
} | |
// setup speaker | |
pinMode (speakerPin, OUTPUT); | |
// setup dht | |
// n/a | |
// setup output | |
if(debugToConsole){ | |
Serial.begin(9600); | |
} | |
} | |
// this function is called over and over again | |
void loop() { | |
if(debugToConsole){ | |
Serial.println(""); | |
Serial.println("* * * * * loop() * * * * *"); | |
} | |
// get the temp | |
theTemperature = getTemperature(); | |
// check to see if water is detected | |
detectsWater = getDetectsWater(); | |
// if detects water OR the temp is less than the min temp, call the doAlert function | |
if(detectsWater || theTemperature < minimumTemperature){ | |
doAlert(); | |
// otherwise insert a brief pause | |
}else{ | |
delay(1000); | |
} | |
// go around the loop again | |
} | |
int getTemperature(){ | |
int chk = DHT.read11(DHT11_PIN); | |
int temperatureCelcius; | |
int temperatureFareinheit; | |
delay(50); | |
temperatureCelcius = DHT.temperature; | |
temperatureFareinheit = (temperatureCelcius*1.8)+32; | |
if(debugToConsole){ | |
Serial.print("getTemperature(), temperatureFareinheit = "); | |
Serial.println(temperatureFareinheit); | |
} | |
return temperatureFareinheit; | |
} | |
bool getDetectsWater(){ | |
bool waterDetected = false; | |
sensorValue = analogRead(analogInPin); | |
outputValue = map(sensorValue, 0, 1023, 0, 255); | |
analogWrite(analogOutPin, outputValue); | |
if(sensorValue > minimumSensorValue && outputValue > minimumOutputValue){ | |
waterDetected = true; | |
} | |
if(debugToConsole){ | |
/* | |
Serial.print("getDetectsWater(), sensor = "); | |
Serial.print(sensorValue); | |
Serial.print(" and output = "); | |
Serial.println(outputValue); | |
*/ | |
Serial.print("getDetectsWater(), waterDetected = "); | |
Serial.print(waterDetected); | |
} | |
return waterDetected; | |
} | |
void doAlert(){ | |
// writeAlert(); | |
// sendAlert(); | |
playAlarm(); | |
} | |
void playAlarm(){ | |
if(debugToConsole){ | |
Serial.println("playAlarm() !!!"); | |
} | |
analogWrite(speakerPin, 255); | |
delay(50); | |
analogWrite(speakerPin, 100); | |
delay(50); | |
analogWrite(speakerPin, 0); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment