Arduino project combat - based on Arduino [smart trash can] design

   Arduino project actual combat   

    —Based on Arduino [smart trash can] design

     The first time I came into contact with Arduino was when I was a freshman. It has been five years since now. At that time, a simple "electronic organ" project made me scratch my head. I didn't have the ability to "program for Baidu" at all. It took several days for the team members to remind the Arduino and the buzzer of a simple version of "Little Star". The joy in my heart at that time and the sand sculpture appearance of pretending to be an X in front of my roommate seem to be still yesterday.

    Because Arduino is easy to get started, and it allows beginners to quickly experience the fun of MCU development, so I use a small column to share some small projects I have done in school before. This project is the primary version of the smart trash can, and 4G will be used later Intermediate version of remote control such as communication module or Wifi module (Esp8266/Esp32). It is absolutely possible to complete a good course design or even a graduation project.

    The first meeting on the Arduino journey, thanks for criticism and correction, let's make progress together!

1. Hardware preparation

  • Arduino UNO
  • HC-SR04 ultrasonic module
  • SG90 steering gear
  • Several DuPont lines
  • power supply

2. Wiring method

 

 

 

3. Code analysis

#include <Servo.h>   //servo库
Servo servo;    //创建舵机对象来控制舵机 
int trigPin = 5;    
int echoPin = 6;   
int servoPin = 7;
int led= 10;
long duration, dist, average;   
long aver[3];   //array for average


void setup() {   
  //初始化串口通信以及连接SR04的引脚    
    Serial.begin(9600);
    
    servo.attach(servoPin);//把连接在引脚7上的舵机赋予舵机对其控制
      
    pinMode(trigPin, OUTPUT);   //要检测引脚上输入的脉冲宽度,需要先设置为输入状态  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(500)
    servo.detach(); 
} 

void measure() {  
    digitalWrite(10,HIGH);
    
    digitalWrite(trigPin, HIGH);  //产生一个10us的高脉冲去触发TrigPin
    delayMicroseconds(2);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);  
    dist = (duration/2) / 10;    //obtain distance 检测脉冲宽度并测算出距离    
}
void loop() { 
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
    aver[i]=dist;            
    delay(10);              //delay between measurements
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    

    if ( dist<50 ) {
    //Change distance as per your need
    servo.attach(servoPin);
    delay(1);
    servo.write(0);  
    delay(3000);       
    servo.write(150);    
    delay(1000);
    servo.detach();      
}
    Serial.print(dist);
}

4. Effect demonstration:

Guess you like

Origin blog.csdn.net/qq_41899773/article/details/107058679