最近测通了很多传感器,记录个整理的简单案例代码:
/* *遥控程序 */ #include <IRremote.h> // IRremote库声明 int RECV_PIN = 3; //定义红外接收器的引脚为11 int buzzer = 7; //设置控制蜂鸣器的数字IO脚 IRrecv irrecv(RECV_PIN); decode_results results; long control[7][3] = {//遥控器矫正数字 {16580863, 16613503, 16597183}, {16589023, 16621663, 16605343}, {16584943, 16617583, 16601263}, {16593103, 16625743, 16609423}, {16582903, 16615543, 16599223}, {16591063, 16623703, 16607383}, {16586983, 16619623, 16603303} }; int LED = 13;//13号灯 int LED6 = 6;//6号灯 void openLight() { digitalWrite(LED, HIGH); digitalWrite(LED6, HIGH); delay(500); digitalWrite(LED, LOW); digitalWrite(LED6, LOW); } void openDuang(int count,int fq) { //digitalWrite(buzzer,HIGH);//发声音 for (int i = 0;i < count; i++) //输出一个频率的声音 { digitalWrite(buzzer, HIGH); //发声音 delay(fq);//延时1ms digitalWrite(buzzer, LOW); //不发声音 delay(fq);//延时ms } } void openLightLong() { digitalWrite(LED, HIGH); digitalWrite(LED6, HIGH); } void setup() { Serial.begin(9600); irrecv.enableIRIn(); // 启动接收器 pinMode(LED, OUTPUT); pinMode(LED6, OUTPUT); pinMode(buzzer, OUTPUT); //设置数字IO脚模式,OUTPUT为输出 } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX);//以16进制换行输出接收代码 if (results.value == 4294967295) { //long click openLightLong(); openDuang(1000,1); } else { if (results.value == control[0][0]) { openLight(); openDuang(80,1); } else if (results.value == control[0][1]) { openLight(); } else if (results.value == control[0][2]) { openLight(); } else if (results.value == control[1][0]) { openLight(); } else if (results.value == control[1][1]) { openLight(); } else if (results.value == control[1][2]) { openLight(); } else if (results.value == control[2][0]) { openLight(); } else if (results.value == control[2][1]) { openLight(); } else if (results.value == control[2][2]) { openLight(); } else if (results.value == control[3][0]) { openLight(); openDuang(80,0);//0 } else if (results.value == control[3][1]) { openLight(); } else if (results.value == control[3][2]) { openLight(); } else if (results.value == control[4][0]) { openLight(); openDuang(80,1); } else if (results.value == control[4][1]) { openLight(); openDuang(80,2); } else if (results.value == control[4][2]) { openLight(); openDuang(80,3); } else if (results.value == control[5][0]) { openLight(); openDuang(80,4); } else if (results.value == control[5][1]) { openLight(); openDuang(80,5); } else if (results.value == control[5][2]) { openLight(); openDuang(80,6); } else if (results.value == control[6][0]) { openLight(); openDuang(80,7); } else if (results.value == control[6][1]) { openLight(); openDuang(80,8); } else if (results.value == control[6][2]) { openLight(); openDuang(80,9); } } irrecv.resume(); // 接收下一个值 } delay(100); }
效果图:
蜂鸣器接线:
1 = GND
2 = S I/O 1-13