#define PIN_TRIG 3
#define PIN_ECHO 2
#define RED_PIN 8
#define GREEN_PIN 9
#define BLUE_PIN 10
#define Buzzer_Pin 11
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(RED_PIN,OUTPUT);
pinMode(GREEN_PIN,OUTPUT);
pinMode(BLUE_PIN,OUTPUT);
pinMode(Buzzer_Pin,OUTPUT);
}
void setColor(int r,int g,int b)
{
analogWrite(RED_PIN,r);
analogWrite(GREEN_PIN,g);
analogWrite(BLUE_PIN,b);
delay(1000);
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
float d= duration* 0.034/2;
Serial.println("Distance in CM:"+String(d));
if(d<200)
{
setColor(255,0,0);
tone(Buzzer_Pin,50);
delay(1000);
setColor(0,0,0);
noTone(Buzzer_Pin);
}
if(d>200 && d<=300)
{
setColor(255,165,0);
}
if( d>300 && d<=400)
{
setColor(0,255,0);
}
delay(1000);
}