אלקטרוניקה לארדואינו : שיעור 7 מד מרחק אולטראסוניק – קורס C510
מותאם לארדואינו אונו https://wokwi.com/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
// www.robotronix.co.il // רובוטרוניקס קורס C510 // אלקטרוניקה לארדואינו : שיעור 7 מד מרחק אולטראסוניק - קורס C510 #define speaker 5 #define LedRed 3 #define LedGreen 8 const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor const int echoPin = 6; // Echo Pin of Ultrasonic Sensor void setup() { // pin setting pinMode(pingPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LedRed , OUTPUT); pinMode(LedGreen, OUTPUT); digitalWrite(LedRed, HIGH); digitalWrite(LedGreen , HIGH); Serial.begin(9600); // Starting Serial Terminal delay(250); digitalWrite(LedRed, LOW); digitalWrite(LedGreen , LOW); Serial.println("starting ..."); } void loop() { long duration, inches, cm; // generate pulse __|^^^^^|_______________ digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); // read pulse duration = pulseIn(echoPin, HIGH); inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); // Serial.print(inches); // Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); if(cm > 350 ) { noTone(speaker); // Stop sound } else { if(cm>=200 && cm<=350 ) { tone(speaker, 3000 -cm*5); // 3000hz - (cm* 5 ) hz delay(50); noTone(speaker); // Stop sound } else { tone(speaker, 3000 -cm*5); // 3000hz - (cm* 5 ) hz delay(50); noTone(speaker); // Stop sound } } if(cm > 200) // 20 cm { digitalWrite(LedRed, LOW); digitalWrite(LedGreen, HIGH); } else { digitalWrite(LedRed ,HIGH); digitalWrite(LedGreen, LOW); } delay(200); } long microsecondsToInches(long microseconds) { return (microseconds / 74 / 2); } long microsecondsToCentimeters(long microseconds) { return (microseconds / 29 / 2); } |
סכמה חיבור
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
{ "version": 1, "author": "Arvind Patil", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 102.51, "left": -3.08, "attrs": {} }, { "type": "wokwi-buzzer", "id": "bz1", "top": -5.86, "left": 127.94, "attrs": { "volume": "0.1" } }, { "type": "wokwi-led", "id": "led1", "top": -3.88, "left": 241.95, "attrs": { "color": "red" } }, { "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -97.25, "left": -18.5, "attrs": { "distance": "2" } }, { "type": "wokwi-led", "id": "led3", "top": 1.63, "left": 320.18, "attrs": { "color": "green" } }, { "type": "wokwi-resistor", "id": "r2", "top": 76.09, "left": 315.07, "rotate": 90, "attrs": { "value": "470" } }, { "type": "wokwi-resistor", "id": "r1", "top": 72.26, "left": 248.62, "rotate": 90, "attrs": { "value": "470" } } ], "connections": [ [ "uno:5", "bz1:2", "green", [ "v0" ] ], [ "uno:GND.1", "bz1:1", "black", [ "v-39.07", "h77.58" ] ], [ "uno:GND.1", "led1:C", "black", [ "v-30.16", "h180.33" ] ], [ "uno:5V", "ultrasonic1:VCC", "red", [ "v-84.87", "h-39.41" ] ], [ "ultrasonic1:GND", "uno:GND.1", "black", [ "v78.78", "h-4.6", "v76.5", "h8.69" ] ], [ "uno:6", "ultrasonic1:ECHO", "green", [ "v-67.8", "h-93.26" ] ], [ "ultrasonic1:TRIG", "uno:7", "green", [ "v87.36", "h90.78" ] ], [ "uno:GND.2", "led3:C", "black", [ "v-20.18", "h174.45" ] ], [ "led1:A", "r1:1", "green", [ "v0" ] ], [ "r1:2", "uno:3", "green", [ "h0" ] ], [ "r1:1", "r1:2", "green", [ "h13.95", "v49.4" ] ], [ "led3:A", "r2:1", "green", [ "v0" ] ], [ "uno:8", "r2:2", "green", [ "v17.21", "h210.13" ] ] ] } |