Ultrasonic sensor Code
- Code
int trigpin = 7;
int echopin = 6;
int buzz = 13;
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;
int duration = 0;
int distance = 0;
void setup()
{
for (int pin0 = 13;pin0<=8;pin0++)
{
pinMode(pin0,OUTPUT);
}
pinMode(trigpin ,OUTPUT);
pinMode(echopin ,INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin,HIGH);
distance = duration/58.2;
Serial.println(distance);
if(distance <= 30)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
if (distance <= 42)
{
digitalWrite(12,HIGH);
Serial.println("led1 is on.");
}
else
{
digitalWrite(12,LOW);
}
if(distance <= 80)
{
digitalWrite(11,HIGH);
}
else
{
digitalWrite(11,LOW);
}
if(distance <= 100)
{
digitalWrite(10,HIGH);
}
else
{
digitalWrite(10,LOW);
}
if(distance <= 110)
{
digitalWrite(9,HIGH);
}
else
{
digitalWrite(9,LOW);
}
if(distance <= 120)
{
digitalWrite(8,HIGH);
}
else
{
digitalWrite(8,LOW);
}
}
Comments
Post a Comment