大家好,我是小编。
记上一节内容点击打开链接
今天我们讲传感器。
今天讲光感和超声波。
先发一张图:
光感有三个接口,分别是:
UCC、out、GND。
超声波有四个接口,分别是:
UCC、T、E、GND。
下面是原理。
超声波是一头发出超声波,声波碰到物体后反弹,另一端接收,从而判断距离。
光感是检测光值,判断的最小0最大255.
附上程序:
避障小车
#include <Servo.h> int pinLF=14; int pinLB=15; int pinRF=16; int pinRB=17; int PWMEN1=3; int PWMEN2=5; void setup() { Serial.begin(9600); pinMode(pinLB,OUTPUT); pinMode(pinLF,OUTPUT); pinMode(pinRB,OUTPUT); pinMode(pinRF,OUTPUT); pinMode(PWMEN1,OUTPUT); pinMode(PWMEN2,OUTPUT); } void advance(int a) { digitalWrite(pinRF,HIGH); digitalWrite(pinRB,LOW); analogWrite(pinRF,150); analogWrite(pinRB, 0); digitalWrite(pinLF,HIGH); digitalWrite(pinLB,LOW); analogWrite(pinLF,150); analogWrite(pinLB,0); delay(a * 100); } void right(int b) { digitalWrite(pinRF,LOW); digitalWrite(pinRB,LOW); analogWrite(pinRF,0); analogWrite(pinRB, 0); digitalWrite(pinLF,HIGH); digitalWrite(pinLB,LOW); analogWrite(pinLF,150); analogWrite(pinLB,0); delay(b * 100); } void left(int c) { digitalWrite(pinRF,HIGH); digitalWrite(pinRB,LOW); analogWrite(pinRF,150); analogWrite(pinRB, 0); digitalWrite(pinLF,LOW); digitalWrite(pinLB,LOW); analogWrite(pinLF,0); analogWrite(pinLB,0); delay(c * 100); } void turnR(int d) { digitalWrite(pinRF,LOW); digitalWrite(pinRB,HIGH); analogWrite(pinRF,0); analogWrite(pinRB, 150); digitalWrite(pinLF,HIGH); digitalWrite(pinLB,LOW); analogWrite(pinLF,100); analogWrite(pinLB,0); delay(d * 100); } void turnL(int e) { digitalWrite(pinRF,HIGH); digitalWrite(pinRB,LOW); analogWrite(pinRF,150); analogWrite(pinRB, 0); digitalWrite(pinLF,LOW); digitalWrite(pinLB,HIGH); analogWrite(pinLF,0); analogWrite(pinLB,150); delay(e * 100); } void stopp(int f) { digitalWrite(pinRB,LOW); digitalWrite(pinRF,LOW); digitalWrite(pinLB,LOW); digitalWrite(pinLF,LOW); delay(f * 100); } void back(int g) { digitalWrite(pinRF,LOW); digitalWrite(pinRB,HIGH); analogWrite(pinRF,0); analogWrite(pinRB, 150); digitalWrite(pinLF,LOW); digitalWrite(pinLB,HIGH); analogWrite(pinLF,0); analogWrite(pinLB,150); delay(g * 100); } void loop() { advance(1); delay(1000); back(1); delay(1000); turnL(1); delay(1000); turnR(1); delay(1000); }
循迹小车
#include <Servo.h> int pinLF=14; int pinLB=15; int pinRF=16; int pinRB=17; int PWM1=3; int PWM2=5; void setup() { Serial.begin(9600); pinMode(pinLB,OUTPUT); pinMode(pinLF,OUTPUT); pinMode(pinRB,OUTPUT); pinMode(pinRF,OUTPUT); } void advance(int a) { digitalWrite(pinRF,HIGH); digitalWrite(pinRB,LOW); analogWrite(pinRF,130); analogWrite(pinRB, 0); digitalWrite(pinLF,HIGH); digitalWrite(pinLB,LOW); analogWrite(pinLF,200); analogWrite(pinLB,0); delay(a * 100); } void loop() { advance(1); }
谢谢大家
原文地址:http://blog.csdn.net/g1342522389/article/details/46480433