Introduction
Simulate the car park assist system. Detect an item, blink the LEDs and make a buzzer tone. Use the LCD to print the distance from the incoming object.
What you will need - Hardware
We will use:
Connections
LEDs and Buzzer
Ultrasonic Sensor HC-SR04
Nokia 5110 LCD (Blue PCB)
|
|
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/* * === Ardumotive Shield for Arduino UNO Board === * Arduino Tutorial: Fade LEDs * Dev Michalis Vasilakis/Date 24/3/2018/http://ardumotive.com */ #define buzzer 13 #define pinLED1 9 #define pinLED2 10 #define pinLED3 11 #define trig A3 #define echo A2 #include "Ultrasonic.h" Ultrasonic ultrasonic(trig,echo); #include <Adafruit_GFX.h> #include <Adafruit_PCD8544.h> Adafruit_PCD8544 display = Adafruit_PCD8544(3,4,5,6,7); int distance; void setup() { //Serial Port init at 9600 Serial.begin (9600); display.begin(); display.setContrast(50); display.clearDisplay(); // clears the screen and buffer pinMode(buzzer, OUTPUT); pinMode(pinLED1, OUTPUT); pinMode(pinLED2, OUTPUT); pinMode(pinLED3, OUTPUT); } void loop() { distance = ultrasonic.Ranging(CM); display.setTextSize(1); display.println("Distance:"); display.println(""); display.setTextSize(3); display.println(distance); display.display(); if (distance >= 20 && distance <=30){ tone(buzzer, 5000, 100); digitalWrite(pinLED1, HIGH); } else if (distance >=10 && distance <20){ tone(buzzer, 5000, 200); digitalWrite(pinLED1, HIGH); digitalWrite(pinLED2, HIGH); } else if (distance <10){ tone(buzzer, 5000, 500); digitalWrite(pinLED1, HIGH); digitalWrite(pinLED2, HIGH); digitalWrite(pinLED3, HIGH); } delay(500); digitalWrite(pinLED1, LOW); digitalWrite(pinLED2, LOW); digitalWrite(pinLED3, LOW); noTone(buzzer); display.clearDisplay(); } |
Download the code from here and open it with Arduino IDE. Inside you will also find the library files.
|
|