Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define Trig_PIN 12 // Pin connect to Trig pin
- #define Echo_PIN 13 // Pin connect to Echo pin
- #define LED_PIN 11 // Pin connect to LED (Active HIGH) ** Use PIN 11, 10, 9, 6, 5 or 3 only **
- void setup() {
- pinMode(Trig_PIN, OUTPUT);
- pinMode(Echo_PIN, INPUT);
- pinMode(LED_PIN, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- // Read distance from HC-04
- digitalWrite(Trig_PIN, LOW);
- delayMicroseconds(5);
- digitalWrite(Trig_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(Trig_PIN, LOW);
- unsigned int PulseWidth = pulseIn(Echo_PIN, HIGH);
- unsigned int distance = PulseWidth * 0.0173681;
- Serial.print("Distance is ");
- Serial.print(distance);
- Serial.println(" cm.");
- // -------------------------
- int val = map(distance, 2, min(10, distance), 80, 255); // resize distance form 1 - 10 to 80 - 255
- analogWrite(LED_PIN, val); // Set brightness of LED
- delay(100);
- }
Add Comment
Please, Sign In to add comment