这个项目是我为我的机器人课制作的
让我们看一下代码:
LiquidCrystallcd(2,3,4,5,6,7);
初始化 LiquidCyrstal 类的实例。
int ledPin = 12;
const int PIRPin = 8;
int pirState = LOW;
int val = 0;
int photoCellPin = A0;
int photoCellReading;
int speakerPin = 10;
声明变量
void setup()
{
lcd.begin(16,2);
pinMode(ledPin, OUTPUT);
pinMode(PIRPin, INPUT);
pinMode(photoCellPin, INPUT);
pinMode(speakerPin, INPUT);
Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print("P.I.R Motion And");
lcd.setCursor(0,1);
lcd.print("light sensors");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Processing Data.");
playTone(300,300);
delay(150);
playTone(0,0);
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Waiting For");
lcd.setCursor(3,1);
lcd.print("Motion...");
}
设置传感器并在屏幕上显示一条短消息。也播放声音
void loop()
{
val = digitalRead(PIRPin);
photoCellReading = analogRead(photoCellPin);
if(val == HIGH)
{
digitalWrite(ledPin, HIGH);
if(pirState == LOW)
{
Serial.println("Motion Detected");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Motion Detected");
lcd.setCursor(0,1);
lcd.print(photoCellReading);
playTone(300,300);
delay(150);
playTone(0,0);
pirState = HIGH;
}
} else
{
digitalWrite(ledPin, LOW);
delay(300);
scrollScreenSaver();
if(pirState == HIGH)
{
Serial.println("Motion Ended");
pirState = LOW;
}
}
}
检查是否有运动,如果有则发出警报。如果没有,它会显示一条自定义消息
void playTone(long duration, int freq)
{
duration *= 1000;
int period = (1.0/freq)*100000;
long elapsed_time = 0;
while(elapsed_time < duration)
{
digitalWrite(13, HIGH);
digitalWrite(speakerPin, HIGH);
delayMicroseconds(period/2);
digitalWrite(13, LOW);
digitalWrite(speakerPin, LOW);
delayMicroseconds(period/2);
elapsed_time += (period);
}
}
播放闹钟声音的功能
void scrollScreenSaver()
{
lcd.clear();
lcd.setCursor(15,0);
lcd.print("No Motion");
lcd.setCursor(15,1);
lcd.print("Waiting");
for(int i = 0; i < 22; i++)
{
lcd.scrollDisplayLeft();
delay(150);
}
}
滚动自定义屏幕保护程序的功能
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !