Commit 2a82b226 authored by charlotte.kerr's avatar charlotte.kerr

Motion Detection System - Source Code

parent 45843211
#include <SPI.h>
#include <ThingSpeak.h>
#include <WiFiNINA.h>
int sensor = 2; // Establishes the PIR motion sensor to use Pin 2
int led = 13; // Establishes the LED light to use Pin 13
int motion = 0; // Variable to be used as a status code: 0 indicates no motion, 1 indicates motion
const char ssid[] = "********"; // WiFi set-up variables (change if relocating)
const char password[] = "********";
const unsigned long channel_id = 2010206; // Establishes the correct credentials for the ThingSpeak Channel
const char write_api_key[] = "VE3UTMCYSQAIY2AQ";
WiFiClient client;
void setup(){
pinMode(led, OUTPUT); // Sets up the testing LED as an output device
pinMode(sensor, INPUT); // Sets up the PIR sensor as an input device
Serial.begin(9600); // Configures the baud rate
WiFi.begin(ssid, password); // Establishes the Arduino's connection to the WiFi network.
Serial.println("Connected to WiFi network.");
Serial.println();
ThingSpeak.begin(client);
Serial.println("Sensor initialising. You have 30 seconds to leave the area without triggering the alert. Once triggered, the sensor will wait 1 minute to re-test for motion.");
delay(30000); // Waits thirty seconds to allow a user to leave the room they need monitored and not affect the sensor
Serial.println("Sensor ready and watching."); // Assuming the user has left the area, the sensor can now wait for motion.
}
void loop(){
if(digitalRead(sensor)) { // If the PIR sensor detects a motion (voltage is sent high when motion is detected, creating the signal)
motion = 1; // Changes the variable to reflect that there is now motion detected (value of 1)
Serial.print("Status ");
Serial.print(motion);
Serial.println(": Motion detected. Uploading motion data to ThingSpeak."); // Indicates the detection of a motion to the serial monitor
digitalWrite(led, HIGH); // Turns the LED on (testing purposes)
ThingSpeak.setField(1, String(motion));
ThingSpeak.writeFields(channel_id, write_api_key); // Writes the motion status to ThingSpeak
delay(60000); // Sets a delay of 1 minute before the next motion can be identified
}
else{ // If no motion is detected
motion = 0; // Returns the value to its default state so that it can be re-triggered
Serial.print("Status ");
Serial.print(motion);
Serial.println(": No motion"); // Indicates no detection of motion to the serial monitor
digitalWrite(led, LOW); // The LED is turned off
ThingSpeak.setField(1, String(motion));
ThingSpeak.writeFields(channel_id, write_api_key); // Uploads the 0 motion status to ThingSpeak to return it to its default state
delay(5000); // Sets a delay of 5 seconds before the next motion can be identified
}
//motion = 0; // Returns the value to its default state so that it can be re-triggered
//ThingSpeak.setField(1, String(motion));
//ThingSpeak.writeFields(channel_id, write_api_key); // Uploads the 0 motion status to ThingSpeak to return it to its default state
//delay(3000); // Sets a delay before the sensor can be re-triggered
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment