Undefined
Home
Project
Team
Blog
Blog 1
Home
Project
Final Project
Arduino
电路图
简介
利用超声波传感器感应手机,检测到手机放入时LED灯珠闪烁(提示灯),放入手机的时间每达到25分钟,亮起一个白色LED灯珠(水滴灯),同时开启水泵浇水。5个水滴灯珠都依次点亮后,5个白色水滴将会变成1个蓝色水滴。
所需硬件
Arduino开发板
X 1
连接线
X n
面包板
X 1
水泵
X 1
水管
X 1
超声波传感器
X 1
LED灯珠
X 6
继电器
X 1
具体代码
int trigPin = 11; //Trig int echoPin = 12; //Echo int pumpPin = 8; int led1Pin = 7; int led2Pin = 6; int led3Pin = 5; int led4Pin = 4; int led5Pin = 3; int led6Pin = 9; //int led6Pin = 2; long duration, cm, inches; int phone = 0; int time; void setup() { digitalWrite(led6Pin,HIGH); digitalWrite(led5Pin,HIGH); digitalWrite(led4Pin,HIGH); digitalWrite(led3Pin,HIGH); digitalWrite(led2Pin,HIGH); digitalWrite(led1Pin,HIGH); digitalWrite(pumpPin,HIGH); //Serial Port begin // digitalWrite(led1Pin,255); Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(pumpPin, OUTPUT); pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(led4Pin, OUTPUT); pinMode(led5Pin, OUTPUT); pinMode(led6Pin, OUTPUT); time = 0; } void loop() { // digitalWrite(led6Pin,LOW); digitalWrite(pumpPin,HIGH); // delay(20000); // digitalWrite(pumpPin,LOW); // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. duration = pulseIn(echoPin, HIGH); // convert the time into a distance cm = (duration/2) / 29.1; inches = (duration/2) / 74; Serial.print("duration: "); Serial.println(duration); //detect the phone if(duration <= 135){ phone = 1; // Serial.println(phone); time++; }else{ phone = 0; // Serial.println(phone); time = 0; } Serial.println(time); int interval = 25; for(int i = 1; i < 7; i++){ if(time == i*interval){ //light ith led digitalWrite(pumpPin,LOW); delay(800); digitalWrite(pumpPin,HIGH); if(i == 1){ // digitalWrite(led1Pin,HIGH); digitalWrite(led5Pin,LOW); }else if(i == 2){ digitalWrite(led4Pin,LOW); }else if(i == 3){ digitalWrite(led3Pin,LOW); }else if(i == 4){ digitalWrite(led2Pin,LOW); }else if(i == 5){ digitalWrite(led1Pin,LOW); }else if(i == 6){ digitalWrite(led6Pin,LOW); digitalWrite(led1Pin,HIGH); digitalWrite(led2Pin,HIGH); digitalWrite(led3Pin,HIGH); digitalWrite(led4Pin,HIGH); digitalWrite(led5Pin,HIGH); } } if(time == 0){ digitalWrite(led6Pin,HIGH); digitalWrite(led5Pin,HIGH); digitalWrite(led4Pin,HIGH); digitalWrite(led3Pin,HIGH); digitalWrite(led2Pin,HIGH); digitalWrite(led1Pin,HIGH); } } delay(60*1000L); }
装配