Commit 4304e423 authored by Shrishailya Agashe's avatar Shrishailya Agashe
Browse files

Upload New File

parent 744ff6f8
/*
* This code takes care of the Ultrasonic and PIR motion sensor.
* It calculates the distance and also checks status of the PIR motion sensor.
* It sends a JSON string to the Serial Port
* Here, it is assumed that trigPin = 9, echoPin = 10.
* The PIR input is taken through pin 8
* Change the pins accordingly
* - Shrishailya Agashe
*/
#include <Arduino_JSON.h>
//Function that calculates the distance form Ultra Sonic Sensor in cm.
long getUlrasonicDistance(int trigPin, int echoPin){
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = duration /29 /2;
return cm;
}
//Function that returns the status of PIR Sensor.
int getPIRStatus(int PIRPin){
pinMode(PIRPin, INPUT);
if(digitalRead(PIRPin) == 0){
return 0;
}
else{
return 1;
}
}
void setup() {
//Begin Serial Communication
Serial.begin(115200);
}
void loop() {
//Create object to store items like a python dictionary
JSONVar myObject;
//Set the values as needed.
myObject["US"] = getUlrasonicDistance(9,10);
myObject["PIR"] = getPIRStatus(8);
//Convert the JSON object to a JSON string
String jsonString = JSON.stringify(myObject);
//Send the string through Serial port.
Serial.println(jsonString);
//100ms delay
delay(1000);
}
Supports Markdown
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