How to Make Smart Trash with arduion

First, prepare materials.

  1. arduion nano SCM 15RMB
  2. An ultrasonic sensor (HC-HR04) 5RMB
  3. 9G servo (triplet) 10RMB
  4. Electric iron (40-60W) 10RMB
  5. Dupont line (male to male, female-female, male to female) 10RMB
  6. MINI USB   3RMB
  7. Hot melt glue gun 20RMB
  8. Arduion ide official website to download free
  9. A trash can
  10. Production start:

    1. The microcontroller 5V VCC connected ultrasonic sensor, TRIG even 07, ECHO even 06, GND GND even
    2. N cascaded servos 5V, negative cascade GND, the signal line 011 is connected. I do not know what to Baidu
    Open arduion ide enter the following code
  11.  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

Guess you like

Origin www.cnblogs.com/SkystarX/p/12286013.html