5. sensor icm PWM led

/*

HC-SR04 afstand sensor]

VCC:  Arduino 5V GND naar arduino GND

"Echo" naar Arduino pin no 13 en "Trig" naar Arduino pin no 12

info: http://goo.gl/kJ8Gl

*/

#define trigPin 12
#define echoPin 13
int ledPin = 9; // LED verbinden met digitale pin 9

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <50)
{
leds();
}

else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

void leds() {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 10) {
analogWrite(ledPin, fadeValue);
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 25) {
analogWrite(ledPin, fadeValue);
delay(30);
}
}