生产实习 等待更新中 时间为整点定时发布
待同步 取消加密 > 图片还未上传至图床 > 视频嵌入还未做好 YouTube 已 OK > 目录在左侧
超声波及温度传感摇摆风扇
所用元件:
UNO R3 开发板 ×1
大面包板 ×1
舵机 ×1
舵机支架 ×1
电位器 ×1
1k 电阻 ×1
USB 数据线 ×1
小风扇 ×1
摇头风扇支架组件 ×1
ULN2003AN 驱动芯片 ×1
三色 LED ×1
鳄鱼夹 ×2
面包线 若干
**开发环境:**Arduino IDE,Mind+
预期要求:
通过超声波测距和温度传感控制舵机是否旋转,当距离小于 30cm 或温度大于 30℃ 时在设定的角度之间来回旋转;通过电位器控制电机,小风扇旋转的快慢。
大致设计思路:
具体实现源代码:
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| \#include <Servo.h> \#include <Wire.h>
void zhuan(); void buzhuan(); void chaoshengbo(); void wendu();
Servo myservo; unsigned char jiao; const int P=A0; const int U=8; int sensorValue=0; int outputValue=0;
const int TrigPin = 10; const int EchoPin = 11; unsigned long distance; int temp; int tol; int j; unsigned int loopCnt; int chr[40] = {0}; unsigned long time; \#define pin 2
void setup() { myservo.attach(9); Serial.begin(9600); pinMode(TrigPin, OUTPUT); pinMode(EchoPin, INPUT); Serial.println("Ultrasonic sensor:"); buzhuan(); chaoshengbo(); wendu(); }
void loop() { chaoshengbo(); wendu(); delay(500); if ((distance<30) || (temp>30)) { digitalWrite(4, LOW); digitalWrite(7, HIGH); zhuan(); chaoshengbo(); wendu(); delay(500); } else if((distance>30) || (temp<30)) { buzhuan(); chaoshengbo(); wendu(); delay(500); } }
void zhuan() { myservo.write(jiao); sensorValue=analogRead(P); outputValue=map(sensorValue,0,1023,0,255); analogWrite(U,outputValue); for(jiao=30;jiao<150;jiao++) { myservo.write(jiao); delay(10); } for(jiao=150;jiao>30;jiao--) { myservo.write(jiao); delay(10); } } void buzhuan() { digitalWrite(TrigPin, LOW); delayMicroseconds(2); myservo.write(jiao); digitalWrite(4, HIGH); digitalWrite(7, LOW); sensorValue=analogRead(P); outputValue=map(sensorValue,0,1023,0,255); analogWrite(U,outputValue); myservo.write(jiao); } void chaoshengbo() { digitalWrite(TrigPin, LOW); delayMicroseconds(2); digitalWrite(TrigPin, HIGH); delayMicroseconds(10); digitalWrite(TrigPin, LOW); distance = pulseIn(EchoPin, HIGH) / 58.00; Serial.print("距离:"); Serial.print(distance); Serial.print("cm"); Serial.println(); } void wendu() { bgn: delay(500);
pinMode(pin,OUTPUT); digitalWrite(pin,LOW); delay(20); digitalWrite(pin,HIGH); delayMicroseconds(40); digitalWrite(pin,LOW);
pinMode(pin,INPUT); loopCnt=10000; while(digitalRead(pin) != HIGH) { if(loopCnt-- == 0) {
Serial.println("HIGH"); goto bgn; } } loopCnt=30000; while(digitalRead(pin) != LOW) { if(loopCnt-- == 0) {
Serial.println("LOW"); goto bgn; } }
for(int i=0;i<40;i++) { while(digitalRead(pin) == LOW) {}
time = micros(); while(digitalRead(pin) == HIGH) {}
if (micros() - time >50) { chr[i]=1; }else{ chr[i]=0; } }
temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23]; tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
Serial.print("温度:"); Serial.print(temp); Serial.println("℃"); }
|
串口串口显示情况 1,此时温度为 28℃,距离为 275cm 左右,都不满足舵机旋转的条件,红色指示灯常亮。
对应的实际运行情况如下图所示:
串口串口显示情况 2,串口串口显示情况 1,此时温度为 28℃,距离为 5cm 左右,满足舵机旋转的条件,绿色指示灯常亮。
对应的实际运行情况如下图所示