烟雾报警器之小星星情趣版

伺服电机,步进电机,烟雾传感器,摇杆,光电门,RGB,红外传感器,超声波......这阵子把这么多传感器使用方法给学会了但是看到他们却没有什么好的想法,无聊之际便想给家里火灾的时候设计一个报警器,为了更有情调,我找来了《小星星》《天空之城》...等曲子 并用童稚的《小星星》作为背景音乐,让你即使在慌乱的火灾现场也能静下心来。

功能介绍:当有烟雾达到一定范围时,蜂鸣器用我们熟悉的小星星报警。

电子元件:烟雾传感器,蜂鸣器,打火机加蚊香(当然你也可以对着烟雾传感器哈气)

这个是专门为它拍摄的视频:

以下是arduino代码:

int speakerPin = 21;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
 
void playTone(int tone, int duration) {
  for (long i = 0; i< duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
 
void playNote(char note, int duration) {
  char names[] = {
  'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = {
  1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 
  // play the tone corresponding to the note name
  for (int i = 0; i< 8; i++) {
    if (names[i] == note) {
    	playTone(tones[i], duration);
    }
  }
}
 
void setup()
{
  pinMode(4,OUTPUT);
  pinMode(speakerPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop()
{
  int val;
  val=analogRead(4);
  Serial.println(val);
  delay(10);
  if (val>150){
  while(1){
    for (int i = 0; i < length; i++) {
      if (notes[i] == ' ') {
      	delay(beats[i] * tempo); // rest
  }
  else {
  	playNote(notes[i], beats[i] * tempo);
}
 
  // pause between notes
  delay(tempo / 2);
  		}
  	}
  }
 
}
打赏

烟雾报警器之小星星情趣版” 中有 1 条评论

发表评论