Paso 1: Programas de Arduino
Heden Benodigd:
weerstanden 7
2 montó 5mm LED
oranje 2 5mm LED
groene 2 5mm LED
1 Sensor de distancia ultrasónico
1 módulo de alarma de zumbador activo
Getoond de Bouw het systeem zoals op bovenstaande foto.
De código:
#define trigPin 7 #define echoPin 6 #define led 13 #define led2 12 #define led3 11 #define led4 10 #define led5 9 #define led6 8 #define buzzer 3 int buzzerPin = 3; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); pinMode(led6, OUTPUT); pinMode(buzzerPin, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; digitalWrite(buzzerPin ,HIGH); if (distance <= 35) { digitalWrite(led, HIGH); digitalWrite(buzzerPin ,HIGH); } else { digitalWrite(led,LOW); } if (distance < 25) { digitalWrite(led2, HIGH); digitalWrite(buzzerPin ,HIGH); } else { digitalWrite(led2, LOW); } if (distance < 20) { digitalWrite(led3, HIGH); digitalWrite(buzzerPin ,HIGH); } else { digitalWrite(led3, LOW); } if (distance < 15) { digitalWrite(led4, HIGH); digitalWrite(buzzerPin ,HIGH); } else { digitalWrite(led4,LOW); } if (distance < 30) { digitalWrite(led5, HIGH); digitalWrite(buzzerPin ,HIGH); } else { digitalWrite(led5,LOW); } if (distance < 5) { digitalWrite(led6, HIGH); digitalWrite(buzzerPin ,LOW); } else { digitalWrite(led6,LOW); digitalWrite(buzzerPin ,HIGH); } if (distance > 3 || distance <= 0){ Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); } void beep(unsigned char delayms) { //creating function analogWrite(buzzerPin, 20); //Setting pin to high delay(delayms); //Delaying analogWrite(buzzerPin ,0); //Setting pin to LOW delay(delayms); //Delaying } <br>