Skip to content

Instantly share code, notes, and snippets.

@elktros
Created June 14, 2018 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/26c03aca43083a9d9a4dd6f73759f00d to your computer and use it in GitHub Desktop.
Save elktros/26c03aca43083a9d9a4dd6f73759f00d to your computer and use it in GitHub Desktop.
Arduino Code for Arduino Radar Project.
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distinCM;
Servo radarServo;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
radarServo.attach(11);
}
void loop()
{
for(int i=0;i<=180;i++)
{
radarServo.write(i);
delay(50);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distinCM = duration*0.034/2;
Serial.print(i);
Serial.print("*");
Serial.print(distinCM);
Serial.print("#");
}
for(int i=180;i>=0;i--)
{
radarServo.write(i);
delay(50);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distinCM = duration*0.034/2;
Serial.print(i);
Serial.print("*");
Serial.print(distinCM);
Serial.print("#");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment