/* Utilisation du capteur Ultrason HC-SR04 */

// définition des broches utilisées
int LedRed = 9;
int LedGreen = 10;
int LedBlue = 11;
int trig = 8;
int echo = 7;
long lecture_echo;
long cm;

void setup()
{
  pinMode(LedRed, OUTPUT);
  pinMode(LedGreen, OUTPUT);
  pinMode(LedBlue, OUTPUT);
  pinMode(trig, OUTPUT);
  digitalWrite(trig, LOW);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  lecture_echo = pulseIn(echo, HIGH);
  cm = lecture_echo / 58;
  Serial.print("Distance : ");
  Serial.print(cm);
  Serial.println(" cm.");
  if(cm > 50){
          analogWrite(LedRed,0);
          analogWrite(LedGreen, 255);
          analogWrite(LedBlue, 0);
          //digitalWrite(LedGreen, HIGH);
          //digitalWrite(LedBlue, LOW);
          //digitalWrite(LedRed, LOW);
     }
 
     else if(cm >= 20 && cm <= 50){
          analogWrite(LedRed, 255);
          analogWrite(LedGreen, 128);
          analogWrite(LedBlue, 0);
          //digitalWrite(LedBlue, HIGH);
          //digitalWrite(LedRed, LOW);
     }
 
     else {
          analogWrite(LedRed, 255);
          analogWrite(LedGreen, 0);
          analogWrite(LedBlue, 0);          
          //digitalWrite(LedGreen, LOW);
          //digitalWrite(LedBlue, LOW);
          //digitalWrite(LedRed, HIGH);
     }
  delay(100);
}
