Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int trigpin=A1, echopin = A2,ledpin =A3;
- float duration,distance;
- void setup(){
- pinMode(trigpin,OUTPUT);
- pinMode(echopin,INPUT);
- pinMode(ledpin,OUTPUT);
- Serial.begin(9600);
- }
- void loop(){
- digitalWrite(trigpin,LOW);
- delay(2);
- digitalWrite(trigpin,HIGH);
- delayMicroseconds(10);
- digitalWrite(trigpin,LOW);
- duration = pulseIn(echopin,HIGH);
- distance =( duration * 0.034 * 0.5);
- if(distance<=100){
- digitalWrite(ledpin,HIGH);
- }else{
- digitalWrite(ledpin,LOW);
- }
- Serial.print("The distance is: ");
- Serial.print(distance);
- Serial.print("cm\n");
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment