Devices & Components
Arduino Uno Rev3
Female to Female Wires
Paper Clips
330 ohm resistors
4xAA Battery Holder
Sparkfun Mini Speaker
Velcro with Adhesive Back
Ultrasonic Distance Sensor - HC-SR04
Mini Power Switch - SPDT
Solderable Bread board
LED - RGB Diffused Common Cathode
Hot Glue
Solid Core Copper Wire with PVC Coating
Silicone Weather Strip
Sparkfun Micro Servo
Hardware & Tools
3D Printer (generic)
Soldering Kit (Optional)
Wire Stripper
Pliers
Hot glue gun (generic)
Wrench
Software & Tools
Tinkercad
Arduino IDE
Project description
Code
Code
cpp
1#include <Servo.h> 2 3Servo myservo; 4 5const int trigPin = 11; //connects to the trigger pin on the distance sensor 6const int echoPin = 12; //connects to the echo pin on the distance sensor 7 8const int redPin = 3; //pin to control the red LED inside the RGB LED 9const int greenPin = 5; //pin to control the green LED inside the RGB LED 10const int bluePin = 6; //pin to control the blue LED inside the RGB LED 11 12const int buzzerPin = 8; //pin that will drive the buzzer 13 14float distance = 0; //stores the distance measured by the distance sensor 15 16const int switchPin = 7; // switch to turn sensor on and off 17 18 19void setup() { 20 pinMode(switchPin, INPUT_PULLUP); 21 Serial.begin (9600); //set up a serial connection with the computer 22 23 pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity 24 pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor 25 26 //set the RGB LED pins to output 27 pinMode(redPin, OUTPUT); 28 pinMode(greenPin, OUTPUT); 29 pinMode(bluePin, OUTPUT); 30 pinMode(buzzerPin, OUTPUT); //set the buzzer pin to output 31 32 myservo.attach(9); 33 34} 35 36void loop() { 37 38 if (digitalRead(7) == LOW) { 39 myservo.write(0); 40 delay(400); 41 distance = getDistance(); //variable to store the distance measured by the sensor 42 43 Serial.print(distance); //print the distance that was measured 44 Serial.println(" in"); //print units after the distance 45 46 if (distance <= 20) { //if the object is close 47 48 //make the RGB LED red 49 analogWrite(redPin, 255); 50 analogWrite(greenPin, 0); 51 analogWrite(bluePin, 0); 52 53 tone(buzzerPin, 272); //buzz the buzzer pin 54 delay(100); //wait 100 milliseconds 55 56 noTone(buzzerPin); //turn the buzzer off 57 delay(100); //wait 100 milliseconds 58 59 } else if (20 < distance && distance < 30) { //if the object is a medium distance 60 61 //make the RGB LED yellow 62 analogWrite(redPin, 255); 63 analogWrite(greenPin, 50); 64 analogWrite(bluePin, 0); 65 66 } else if (30 < distance && distance < 40) { //if the object is far away 67 68 //make the RGB LED green 69 analogWrite(redPin, 9); 70 analogWrite(greenPin, 255); 71 analogWrite(bluePin, 0); 72 } else { 73 //make the RGB LED blue 74 analogWrite(redPin, 0); 75 analogWrite(greenPin, 0); 76 analogWrite(bluePin, 255); 77 } 78 79 delay(50); //delay 50ms between each reading 80 } else { 81 myservo.write(128); 82 analogWrite(redPin, 0); 83 analogWrite(greenPin, 0); 84 analogWrite(bluePin, 0); 85 } 86} 87 88//------------------FUNCTIONS------------------------------- 89 90//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR 91float getDistance() 92{ 93 float echoTime; //variable to store the time it takes for a ping to bounce off an object 94 float calculatedDistance; //variable to store the distance calculated from the echo time 95 96 //send out an ultrasonic pulse that's 10ms long 97 digitalWrite(trigPin, HIGH); 98 delayMicroseconds(10); 99 digitalWrite(trigPin, LOW); 100 101 echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the 102 //pulse to bounce back to the sensor 103 104 calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) 105 106 return calculatedDistance; //send back the distance that was calculated 107}
Downloadable files
3D-Print Main Components
Car Sensor Main Components.stl
3D-Print Hinge
file.None
Comments
Only logged in users can leave comments