First, prepare materials.
- arduion nano SCM 15RMB
- An ultrasonic sensor (HC-HR04) 5RMB
- 9G servo (triplet) 10RMB
- Electric iron (40-60W) 10RMB
- Dupont line (male to male, female-female, male to female) 10RMB
- MINI USB 3RMB
- Hot melt glue gun 20RMB
- Arduion ide official website to download free
- A trash can
-
Production start:
- The microcontroller 5V VCC connected ultrasonic sensor, TRIG even 07, ECHO even 06, GND GND even
- N cascaded servos 5V, negative cascade GND, the signal line 011 is connected. I do not know what to Baidu
-
1 #include <Servo.h> 2 3 #define Trig 7 4 #define Echo 6 5 6 Servo servo1; 7 8 int Duration; 9 float Distance; 10 int Distance1; 11 12 void setup() { 13 Serial.begin(9600); 14 servo1.attach(11); 15 pinMode(Trig, OUTPUT); 16 pinMode(Echo, INPUT); 17 } 18 19 void loop() { 20 Dis(); 21 while (Distance < 10) { 22 Dis(); 23 servo1.write(90); 24 delay(2500); 25 } 26 servo1.write(0); 27 delay(100); 28 } 29 30 void Dis() { 31 digitalWrite(Trig, LOW); 32 delayMicroseconds(1); 33 digitalWrite(Trig, HIGH); 34 delayMicroseconds(11); 35 digitalWrite(Trig, LOW); 36 Duration = pulseIn(Echo, HIGH, 10000); 37 if (Duration > 0) { 38 Distance = Duration / 2; 39 Distance = Distance * 340 / 10000; // ultrasonic speed is 340m/s = 34000cm/s = 0.034cm/us 40 Distance1 = Distance * 10; 41 } else { 42 Distance = 100; 43 } 44 }
12.
Last modified trash