Arduino单片机一块~(型号随意)
面包板(当然只是测试,之后你可以自制电路板~)
电线
超声波测距模块
led(rgb led 4脚)
9v电池
超声波模块插入面包板~为了感知距离,我们将使用超声波测距仪。感知距离,测距仪迅速发出超声波。每波会向外,如果路径中有一个物体,它会发生碰撞和反弹的测距仪。它需要的波反弹回来决定了该对象的距离。只能感知对象从2英寸到10英尺。所有我们需要做的是发送测距仪从Arduino快速5V脉冲和使用pulsein()函数来检查了波反弹的时间。
取出你的蜂鸣器,插入面包板~当然我这个比较特殊,比较响亮,常见的那种是黑黑的,小小的,不过都一样~
RGB led 市面上有两种,一种是自动切换颜色,变色,我之前做荧光棒用的~当然这次就不一样了,是四个角的,如图,学习一下他的结构~RGB LED是发光二极管,输出一系列的颜色组合(用红、绿、蓝)。他们有两种配置:共阳极,共阴极。看到这一步第二图片.共阳极:常见的铅,最长的铅,是积极的(阳极意味着积极的铅)。此引线应连接到一个正电压。为了使LED绿色,你将一个0V(又名GND)的绿色铅。同样适用于其他颜色。在这三个引脚上的电压的不同组合,可以创建一个颜色范围。共阴极:常见的线(引脚),最长的线(引脚),是负(阴极意味着负极)。这种类型是比较直观的,就像一个普通的LED。长引线连接到GND和其他三个线索可以连接到一个正电压(取决于你想要什么颜色的)。请记住,LED可能需要一个串联电阻,以限制通过该设备的电流。这是最简单的把电阻串联在一起的普通导线(所以你只需要一个,而不是把一个电阻串联在红色的线,绿色的线,和蓝色的线)
我用的arduino板,比较小,大家自己去找找,我就是懒,忘了~反正一样用~
现在让我们把把所有的东西连在一起!我们有一个电力系统,距离传感器,报警系统,和微控制器。使用以下各部分的连接到Arduino开发板:动力系统:由于这是一个低功率,移动的物体,我们可以用一个9V电源Arduino然后将下调电力的其他组件。电池+ ---> Arduino VIN电池-> Arduino GND距离传感器:测距仪GND ---> Arduino GND测距仪5V ---> Arduino的5V测距信号--->类比引脚5警报系统:我们的系统来提醒用户由两装置:压电蜂鸣器和LED。这触发了一个人的听觉和视觉感官。Piezo:Piezo Buzzer Black线——> Arduino GND压电蜂鸣器红色线---> Arduino数字引脚13RGB LEDRGB LED长---> ---> Arduino 330欧姆的电阻接地RGB LED红色导线-> Arduino数字引脚12RGB LED绿色导线-> Arduino数字引脚10RGB LED蓝色铅---> Arduino数字引脚11具体看图也行
该编程啦~#define NOTE_B0 31 //NOTE_B0 = 31 Hzint jb_m[] = { //this pattern of notes is a snippet from james bond /*NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4, NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,*/ NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4, NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4, NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4, /*NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4, NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5, NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6,NO_SOUND*/ }; int jb_n[] = { //the corresponding note durations /*8,16,16,8,4,8,8,8, 8,16,16,8,4,8,8,8,*/ 8,16,16,8,4,8,8,8, 8,16,16,8,4,8,8,8, 8,2,8,8,1, /*4,8,4,8,8, 8,8,4,8,4,8, 4,8,4,8,3*/ };bool change=false; //variable to check if something entered the red zone double duration; //duration is used to determine the distance sensed from the rangefinder int distance; //the distance found from the rangefinder //the corresponding pins for the RBG LED int red = 12; int blue = 11; int green = 10; int buzzer = 13; //the corresponding pin for the buzzervoid setup() { Serial.begin(9600); //enable the serial monitor at a 9600 baud rate //make the LED and buzzer an output from the Arduino pinMode(red,OUTPUT); pinMode(blue,OUTPUT); pinMode(green,OUTPUT); pinMode(buzzer,OUTPUT); }void loop() { getDistance(); //find the distance of any object (if there even is an object) delay(10); //wait for the computer to catch up alert(); //let the user know what distance we found }void getDistance(){ pinMode(A5, OUTPUT); //set the rangefinder pin to be an output in preparation to send a pulse analogWrite(A5, 0); //start with no output, start with a clean slate delayMicroseconds(2); //let the computer catch up analogWrite(A5, 256); //send out the pulse delayMicroseconds(5); //wait 5 ms before turning off the pulse analogWrite(A5, 0); //turn off the pulse pinMode(A5, INPUT); //set rangefinder pin back to input to be ready to catch the bounced sound wave duration = pulseIn(A5, HIGH); //pulseIn() function reads the time it took for wave to bounce back distance = duration/72/2; //converts the found time to distance (using physics and math) delay(10); //this slows down the rate at which we take distance measurements //Serial.println(distance); //debugging }void alert(){ //turn off all the LEDs digitalWrite(red,LOW); digitalWrite(green,LOW); digitalWrite(blue,LOW); if(distance > 120){ //if object is far away, LED set to green and allow change to be true digitalWrite(green,HIGH); } if(distance < 115 && distance > 85){ //if the object not too far or too close, digitalWrite(blue,HIGH); //make LED blue and allow change to be true change = true; } if(distance < 80){ //if the object gets close, make the LED red digitalWrite(red,HIGH); if(change) music(jb_m,jb_n,1500,sizeof(jb_n)/2); //if the object was not previously in the red zone change = false; //disable change //then play the music } }void music(int melody[],int noteDuration[],int pace,int length){ for (int Note = 0; Note < length; Note++){ //loop through the length of the music) int duration = pace/noteDuration[Note]; //take into account the pace tone(buzzer, melody[Note],duration); //play the note using the tone() function delay(duration*1.2); //let the note run for the corresponding duration } }
最后我还自己画了个pcb图~估计可以找人定制~
喜欢的同志点个赞
不懂得可以留言