极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11093|回复: 0

声控彩灯

[复制链接]
发表于 2014-4-14 12:42:53 | 显示全部楼层 |阅读模式
本帖最后由 yyy_zc 于 2014-4-14 12:52 编辑

影视作品中土豪们拍拍手,就有人弹奏曲子。我们能做到什么呢,女神驾到时,拍一下亮粉灯,拍两下亮蓝灯哈哈试试下面的小程序吧。

硬件:1.MIC口 1个ARDUINO实验板,其他的灯自己加。
软件主要识别你拍了几下。


如果你拍拍拍拍拍是什么效果?自己实验一下吧

/*
测试声音响应情况。拟实现口哨控制
*/


int micpin = 2;                   // 麦克风侦测管脚,目前侦测数字信号
unsigned long time,th,tl;
const unsigned long PT=2500;     //波峰的最大间隔时间,判断一次响应
int count;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(micpin, INPUT);
  Serial.println("=Sound control version 03-1===");
  ledtest();
  count = 0;
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  unsigned long duration,dr1,dr2;

  //获得一次计数

  time = micros();        //重新计时
  duration = pulseIn(micpin,LOW,1000000);//1秒钟超时
  if (duration > 0)
  {   
    //--------越长估计波峰越高-------
    dr1=duration;
    while(1)
    {
      duration = pulseIn(micpin,LOW,PT);//2500micsecond超时

      if (duration > dr1) dr1=duration;      //新的峰值 else 继续听 直到
      if (duration==0) break; //无后续信号接收到
    }
    //---------------------------------
    th = micros()-time;
    tl = dr1;
    Serial.print("1,"); //前面间断的时间
    Serial.println(th);//包含期间的几个duration
    Serial.print("0,"); //收到声音的声音时长
    Serial.println(tl);

    if (count == 0) //第一个信号 可能等了很久,故th不好引用
      count++;     //一个信号如何结束?
    else            //判断后续信号是否有效
    {
      if ((th>300000)&(th<800000)) //与前一信号间隔为0.3~0.8秒间有效
        count++;
    }
  }
  else        //超过1秒无信号收到
  {
    if (count > 0)     //前面已收到过信息
    {     
      Serial.print("============================");
      Serial.print("I have got singer for times:");
      Serial.println(count);
      ledshow(count);
      count = 0;
    }
  }
}
void ledtest()
{
  pinMode(7, OUTPUT);  
  pinMode(8, OUTPUT);  
  pinMode(9, OUTPUT);
  digitalWrite(7, HIGH);
  delay(300);
  digitalWrite(8, HIGH);
  delay(300);   
  digitalWrite(9, HIGH);
  delay(300);
  digitalWrite(7, LOW);
  delay(300);
  digitalWrite(8, LOW);
  delay(300);   
  digitalWrite(9, LOW);
  delay(300);   
}
void ledshow(int c)
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  if (c == 1) digitalWrite(7, HIGH);
  if(c ==2 )
  {
    digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  }
  if(c>2)
  {
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
  }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 01:00 , Processed in 0.053528 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表