Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
AgriTech
Mobile Autonomous Cart with guided Vision for Agriculture
Commits
4304e423
Commit
4304e423
authored
Jul 17, 2021
by
Shrishailya Agashe
Browse files
Upload New File
parent
744ff6f8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Arduino - Python Communication/sensorJSON.ino
0 → 100644
View file @
4304e423
/*
* 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
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment