#include <Servo.h>
//my pins:
int echo =6;
int trig=5;
int servo=7;
int red=8;
int yellow=9;
int green=10;
///
Servo myservo;
// 
float duration,cm;


void setup() {
  Serial.begin(9600);
 myservo.attach(servo);
 pinMode(red, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(echo, INPUT);
 pinMode(trig, OUTPUT);


}



void loop() {
//ultrasoinc set up
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);


///


//distance 
duration=pulseIn(echo,HIGH);
cm=duration*(0.034/2);




//program
  if(cm<20){digitalWrite(green, HIGH);myservo.write(90);
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);
  
  }//green 90 degrees
  if(cm>=20&&cm<40){digitalWrite(yellow, HIGH);myservo.write(45);
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
  }//yellow 45 degrees
  if(cm>=41){digitalWrite(red, HIGH);myservo.write(0);
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);}//red 0 degreees
  Serial.print("distance:");
Serial.print(cm);
Serial.println(" CENTIMETERES");
delay(100);
}