Last active
December 28, 2019 23:17
-
-
Save rocammo/3c589be76c4fec0943a2497deecf94d5 to your computer and use it in GitHub Desktop.
monitors the actual occupancy of a parking lot
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
const int trigpin = 13; // select the pin for the 'trig' ultrasonic sensor | |
const int echopin = 12; // select the pin for the 'echo' ultrasonic sensor | |
const int redpin = 11; // select the pin for the red LED | |
const int greenpin = 10; // select the pin for the green LED | |
const int bluepin = 9; // select the pin for the blue LED | |
void setup () { | |
// set ultrasonic sensor pin modes | |
pinMode(trigpin, OUTPUT); | |
pinMode(echopin, INPUT); | |
// set LED pin modes | |
pinMode (redpin, OUTPUT); | |
pinMode (bluepin, OUTPUT); | |
pinMode (greenpin, OUTPUT); | |
// open serial port, set data rate to 9600 bps | |
Serial.begin (9600); | |
} | |
void loop () { | |
detectParkingFree(); | |
} | |
void rgb(int r, int g, int b) { | |
analogWrite(redpin, r); | |
analogWrite(greenpin, g); | |
analogWrite(bluepin, b); | |
} | |
float getDistance() { | |
static const float v = 331.5 + 0.6 * 20; // m/s | |
// send sound pulse | |
digitalWrite(trigpin, LOW); | |
delayMicroseconds(3); | |
digitalWrite(trigpin, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(trigpin, LOW); | |
// listen for echo | |
float pulse = pulseIn(echopin, HIGH); // microseconds | |
float t = pulse / 1000.0 / 1000.0 / 2; // seconds | |
float d = t * v; // m | |
return d * 100; // cm | |
} | |
void detectParkingFree() { | |
// get distance to the vehicle below | |
float distance = getDistance(); | |
// set color depending on the distance | |
if (distance <= 10) { | |
rgb(255, 0, 0); // red | |
} else if (distance > 10 && distance <= 35) { | |
rgb(255, 170, 0); // yellow | |
} else { | |
rgb(0, 255, 0); // green | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment