IoT let the dogs out
Made by Gabriela Rubio
Made by Gabriela Rubio
Automatic backyard door opening for early morning barking dogs.
Created: January 29th, 2016
I started this process by creating flow diagrams to understand what I was wanted the solution to do, considering the things the dogs are used to do every morning so no training is required for them.
After this, attached all the components in the breadboard and I added "//" type of comments in Particle Dev, and filling them out with functions and variables. This turned out to be not the best efficient process, so I got to a point that I had to delete everything and start over again. I unplugged everything from the breadboard and then I started adding and testing every component individually. I believe that doing this was a good thing for my process since I reaffirmed everything was working properly, this is why I added an LED to monitor the functions of every component, although it was not required for the final solution. As shown in the Code, dividing constants & variables, setup, loop and functions was very helpful for me to manage all the information and confirm functionality.
/* CONSTANTS & VARIABLES
------------------------------------------------------*/
//Photo resistor / photo cell input
int photoCellPin = A0;
//Store the sensor reading
int photoCellReading = 0;
//Threshold for being dark
int threshold = 2800;
int ledPin = D0;
//Pin for microphone
int micPin = A1;
//Reading noise level
int noise_level = 0;
const int sampleWindow = 50;
int servoPin = D2;
Servo myServo;
int pos=5;
int buttonPin = D3;
/* SETUP
------------------------------------------------------*/
void setup(){
//Function that will allow direct communication between usb port, analogue reading
Serial.begin(9600);
//Connect the photo cell reading to the cloud to read the value on "cloud variables"
Particle.variable("light", &photoCellReading, INT);
Particle.variable("noise", &noise_level, INT);
pinMode(ledPin, OUTPUT);
// Attaches the servo on the A7 pin to the servo object
myServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
}
/* LOOP
------------------------------------------------------*/
void loop(){
photoCellReading = analogRead(photoCellPin);
if(photoCellReading > threshold){
sampleNoise();
noise_level = sampleNoise();
Serial.println ( noise_level );
if(noise_level>950){
digitalWrite(ledPin,HIGH);
myServo.write(5);//Open door
Particle.publish("Open","Open");
}
}
digitalWrite (ledPin, LOW);
int buttonState = digitalRead(buttonPin);
if( buttonState == LOW )
{
myServo.write(90);//Close door
delay(5000); //Wait for a little bit before reading again
}
}
//Read to give threshold
//Serial.print("sensor reading is");
//Serial.println(photoCellReading);
/* FUNCTIONS
------------------------------------------------------*/
int monitorPhotoCell(){
//Reading from sensor
photoCellReading = analogRead(photoCellPin);
if(photoCellReading > threshold){
digitalWrite (ledPin, HIGH);
}
else {
digitalWrite (ledPin, LOW);}
}
int sampleNoise(){
unsigned long startMillis = millis();
int highest_sample = 0;
int lowest_sample = 1000;
while (millis()-startMillis < sampleWindow) {
int sample = analogRead(micPin);
sample = map (sample,0,4095,1000,0);
if (sample > highest_sample){
highest_sample = sample;
}
if(sample < lowest_sample){
lowest_sample = sample;
}
}
int peakToPeak = highest_sample - lowest_sample;
return peakToPeak;
}
int monitorMic(){
noise_level = sampleNoise();
Serial.println ( noise_level );
if(noise_level>800){
digitalWrite(ledPin,HIGH);
}
else{digitalWrite(ledPin, LOW);}
}
Click to Expand
1. It will detect if there is light around, since the dogs are supposed to go to the backyard only during the daytime.
2. It will detect if the dogs are barking (this part was kind of tricky - I had to play with the sensibility of the microphone so it could differentiate very loud from not very loud noises).
3. The Servo Motor acts as a door latch and if the microphone hears the barks (or very loud noises) it will rotate 90.
4. The door will be open, and the dogs will be able to push it so they can go out. A notification will be sent.
5. I included a button so the latch can be closed again.
While I was doing this project I realised that probably the microphone was not the most effective idea to detect if the dogs are ready to go out, since there might be other loud noises around. Therefore, if I was to continue to develop this solution, I would probably try this solution and also a limit switch to react to the dogs' push and see which one works best.
This course charts the emergence of the now "connected world" to explore the possibilities for future products and connected spaces.
Automatic backyard door opening for early morning barking dogs.