Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
LED (generic)
Project description
Code
stable_ultrasonic_blink.ino
arduino
A code to sense rate of change of altitude of aircraft above ground in form of blinking of LED
1int triggerpin=10; 2int echopin=11; 3long duration; 4long distance; 5long x; 6long x1=45; 7int z; 8void setup() { 9 // put your setup code here, to run once: 10pinMode(triggerpin,OUTPUT); 11pinMode(echopin,INPUT); 12pinMode(9,OUTPUT); 13Serial.begin(9600); 14} 15 16void loop() { 17 // put your main code here, to run repeatedly: 18digitalWrite(triggerpin,LOW); 19delayMicroseconds(2); 20digitalWrite(triggerpin,HIGH); 21delayMicroseconds(10); 22digitalWrite(triggerpin,HIGH); 23duration=pulseIn(echopin,HIGH); 24x=(duration/2)/29.1; 25Serial.print("Distance :"); 26Serial.println(x); 27z=(x1-x)/2; 28x1=x; 29Serial.print("\ z :"); 30Serial.println(z); 31 32if(z<-10) 33{ 34analogWrite(9,25); 35delay(1000); 36analogWrite(9,LOW); 37delay(1000); 38} 39else if(z>6) 40{ 41analogWrite(9,25); 42delay(10); 43analogWrite(9,LOW); 44delay(10); 45} 46else 47{ 48 z=map(z,-10,6,1000,5); 49analogWrite(9,25); 50delay(z); 51analogWrite(9,LOW); 52delay(z); 53} 54} 55
stable_ultrasonic_blink.ino
arduino
A code to sense rate of change of altitude of aircraft above ground in form of blinking of LED
1int triggerpin=10; 2int echopin=11; 3long duration; 4long distance; 5long x; 6long x1=45; 7int z; 8void setup() { 9 // put your setup code here, to run once: 10pinMode(triggerpin,OUTPUT); 11pinMode(echopin,INPUT); 12pinMode(9,OUTPUT); 13Serial.begin(9600); 14} 15 16void loop() { 17 // put your main code here, to run repeatedly: 18digitalWrite(triggerpin,LOW); 19delayMicroseconds(2); 20digitalWrite(triggerpin,HIGH); 21delayMicroseconds(10); 22digitalWrite(triggerpin,HIGH); 23duration=pulseIn(echopin,HIGH); 24x=(duration/2)/29.1; 25Serial.print("Distance :"); 26Serial.println(x); 27z=(x1-x)/2; 28x1=x; 29Serial.print("\ z :"); 30Serial.println(z); 31 32if(z<-10) 33{ 34analogWrite(9,25); 35delay(1000); 36analogWrite(9,LOW); 37delay(1000); 38} 39else if(z>6) 40{ 41analogWrite(9,25); 42delay(10); 43analogWrite(9,LOW); 44delay(10); 45} 46else 47{ 48 z=map(z,-10,6,1000,5); 49analogWrite(9,25); 50delay(z); 51analogWrite(9,LOW); 52delay(z); 53} 54} 55
stable_ultrasonic_brightness
arduino
To measure the height and change brightness of LED accordingly.
1int triggerpin=10; 2int echopin=11; 3long duration; 4long distance; 5long x; 6void setup() { 7 // put your setup code here, to run once: 8pinMode(triggerpin,OUTPUT); 9pinMode(echopin,INPUT); 10pinMode(9,OUTPUT); 11Serial.begin(9600); 12} 13 14void loop() { 15 // put your main code here, to run repeatedly: 16digitalWrite(triggerpin,LOW); 17delayMicroseconds(2); 18digitalWrite(triggerpin,HIGH); 19delayMicroseconds(10); 20digitalWrite(triggerpin,HIGH); 21duration=pulseIn(echopin,HIGH); 22x=(duration/2)/29.1; //1/29.1=0.034 ~ speed of sound converted into centimeters per microsecond 23Serial.print("Distance :"); 24Serial.print(x); 25x=map(x,0,100,255,0); //mapping the intensity as per distance 26Serial.print("\ "); 27Serial.print(x); 28Serial.println(); 29if(x<255 && x>10) //code stability condition 30{ 31 analogWrite(9,x); 32 } 33 else if (x<10) 34 { 35 analogWrite(9,0); 36 } 37 else 38 { 39 analogWrite(9,0); 40 } 41} 42
Downloadable files
circuitDiagram
The fritzing drawing of circuit diagram
circuitDiagram
circuitDiagram
The fritzing drawing of circuit diagram
circuitDiagram
Comments
Only logged in users can leave comments