Components and supplies
Arduino UNO
RGB LED Module
Jumper wires (generic)
Project description
Code
Parking/Proximity Sensor Code (No bugs)
arduino
1//Ultrasonic Parking Assistant 2//measures distance using ultrasonic sensor 3//displays approximate closeness with colours on LED module 4//Blue- out of range (3m typically) 5//Green- in range, but far away 6//Amber- closer 7//Red- very close 8#define GREENDISTANCE 200 9#define AMBERDISTANCE 60 10#define REDDISTANCE 30 11 12// Ultrasonic HC-SR04 unit interface 13//define pins here 14//if using fixed power pins, set to a negative number, and they will be ignored 15#define UVCC 10 16#define UTRIG 11 17#define UECHO 12 18#define UGND 13 19 20//define led module pins here, polarity is automatically handled by presence of LEDPLUS or LEDMINUS 21//if LED's have common negative, use LEDMINUS 22//if LED's have common positive, use LEDPLUS 23//#define LEDMINUS 3 24#define LEDPLUS 3 25#define LEDBLUE 0 26#define LEDRED 1 27#define LEDGREEN 2 28 29void setup(){ 30 usonicsetup(); 31 ledsetup(); 32} 33 34void loop(){ 35 byte r=0; //for red led 36 byte g=0; //for green led 37 byte b=0; //for blue led 38 long d; //for distance 39 d=usonic(17400)/58; //convert ping time to distance in cm 40 if(d==0){d=300;} //sometimes returns 0 when not in range 41 if(d<AMBERDISTANCE){r=1;} //turn on red LED if red or amber needs to be shown 42 if((d>=REDDISTANCE)&&(d<300)){g=1;} //turn on green LED if amber or green needs to be shown 43 if(d>=300){b=1;} //turn on blue otherwise, so you know it's working 44 ledset(r,g,b); //set the LED's 45 delay(200); //wait a bit so it won't flicker too much 46} 47 48void ledsetup(){ //set up led pins depending on whether they are common + or common -, turn all LED's off 49#ifdef LEDPLUS 50 pinMode(LEDPLUS, OUTPUT); 51 digitalWrite(LEDPLUS, HIGH); 52 pinMode(LEDRED, OUTPUT); 53 digitalWrite(LEDRED, HIGH); 54 pinMode(LEDGREEN, OUTPUT); 55 digitalWrite(LEDGREEN, HIGH); 56 pinMode(LEDBLUE, OUTPUT); 57 digitalWrite(LEDBLUE, HIGH); 58#endif 59#ifdef LEDMINUS 60 pinMode(LEDMINUS, OUTPUT); 61 digitalWrite(LEDMINUS, LOW); 62 pinMode(LEDRED, OUTPUT); 63 digitalWrite(LEDRED, LOW); 64 pinMode(LEDGREEN, OUTPUT); 65 digitalWrite(LEDGREEN, LOW); 66 pinMode(LEDBLUE, OUTPUT); 67 digitalWrite(LEDBLUE, LOW); 68#endif 69} 70 71void ledset(byte r, byte g, byte b){ 72#ifdef LEDPLUS 73 r=!r; //invert if we're using common + 74 g=!g; 75 b=!b; 76#endif 77 digitalWrite(LEDRED, r); //set outputs 78 digitalWrite(LEDGREEN, g); 79 digitalWrite(LEDBLUE, b); 80} 81 82void usonicsetup(void){ 83 if(UGND>-1){ //set up ground pin if defined 84 pinMode(UGND, OUTPUT); 85 digitalWrite(UGND, LOW); 86 } 87 if(UVCC>-1){ //set up VCC pin if defined 88 pinMode(UVCC, OUTPUT); 89 digitalWrite(UVCC, HIGH); 90 } 91 pinMode(UECHO, INPUT); //ECHO pin is input 92 pinMode(UTRIG, OUTPUT); //TRIG pin is output 93 digitalWrite(UTRIG, LOW); 94} 95 96long usonic(long utimeout){ //utimeout is maximum time to wait for return in us 97 long b; 98 if(digitalRead(UECHO)==HIGH){return 0;} //if UECHO line is still low from last result, return 0; 99 digitalWrite(UTRIG, HIGH); //send trigger pulse 100 delay(1); 101 digitalWrite(UTRIG, LOW); 102 long utimer=micros(); 103 while((digitalRead(UECHO)==LOW)&&((micros()-utimer)<1000)){} //wait for pin state to change- return starts after 460us typically 104 utimer=micros(); 105 while((digitalRead(UECHO)==HIGH)&&((micros()-utimer)<utimeout)){} //wait for pin state to change 106 b=micros()-utimer; 107 return b; 108}
Comments
Only logged in users can leave comments
icreated
0 Followers
•0 Projects
0
0
RGB Parking Sensors | Arduino Project Hub