今天练习的程序是实现红外遥控器
的控制,首先,通过太极创客
的视频来看看最终的实现结果:
【太极创客】零基础入门学用Arduino 第三部分 智能应用篇 合辑
本次程序例程来自太极创客官网,此平台的Arduino教程深入浅出,对于想学习Arduino的同学,首推太极创客团队。(๑•̀ㅂ•́)و✧
以下便是今天练习的造轮子程序:
#include <IRremote.h>
#include <EEPROM.h>
#define codeTypeEAddr 0
#define codeLenEAddr 1
#define toggleEAddr 2
#define codeValueEAddr 3
#define RECV_PIN 11
#define BUTTON_PIN 12
#define STATUS_PIN LED_BUILTIN
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
int codeType;
unsigned long codeValue;
unsigned int rawCodes[RAWBUF];
int codeLen;
int toggle;
int lastButtonState;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(BUTTON_PIN,INPUT_PULLUP);
pinMode(STATUS_PIN,OUTPUT);
delay(10);
loadEepromValues();
}
coid loop(){
int buttonState=!digitalRead(BUTTPN_PIN);
if(lastButtonState==HIGH&&buttonState==LOW){
Serial.println("Reseased");
irrecv.enableIRIn():
}
if(buttonState){
Serial.println("Pressed, sending");
digitalWrite(STATUS_PIN,HIGH);
sendCode(lastButtonState==buttonState);
digitalWrite(STATUS_PIN,LOW);
delay(50);
}else if(irrecv.decode(&result)){
degitalWrite(STATUS_PIN,HIGH);
storeCode(&result);
irrecv.resume();
digitalWrite(STATUS_PIN,LOW);
}
lastButtonState=buttonState;
}
void storeCode(decode_result *resukts){
codeType=results->decode_type;
int count=result->rawlen;
if(codeType==UNKNOWN){
Serial.println("Receive unknown code, saving as raw");
codeLen=result->rawlen-1;
for(int i=1;i<=codeLen;i++){
if(i%2){
rawCodes[i-1]=results->rawbuf[i]*UNECPERTICK-MARK_EXCESS;
Serial.print(" m");
}else{
rawCodes[i-1]=result->rawbuf[i]*USECPERTICK+MARK_EXCESS;
Serial.println(" s");
}
Serial.print(rawCodes[i-1],DEC);
}
Serial.println("");
}else{
if(codeType==NEC){
Serial.print("Received NEC: ");
if(result->value==REPEAT){
Serial.println("repeat; ingnoring.");
return;
}
}else if(codeType==SONY){
Serial.println("Received SONY: ");
}else if(codeType==PANASONIC){
Serial.println("Received PANASONIC: ");
}else if(codeType==JVC){
Serial.println("Received JVC: ");
}else if(codeType==RC5){
Serial.println("Received RC5: ");
}else if(codeType==RC6){
Serial.println("Received RC6: ");
}else{
Serial.print("Unexpected codeType ");
Serial.println(codeType, DEC);
Serial.println("");
}
Serail.println(result->value,HEX);
codeValue=result->value;
codeLen=result->bits;
}
writeEepromVal();
}
void sendCode(int repeat){
if(codeType==NEC){
if(repeat){
irsend.sendNEC(REPEAT,codeLen);
Serial.println("Sent NEC repeat");
}else{
irsend.sendNEC(codeValuecodeLen);
Serial.print("Sent NEC");
Serial.println(codeValue,HEX);
}
}else if(codeType==SONY){
irsend.sendSony(codeValue,codeLen);
Serial.print("Sent Sony");
Serial.println(codeValue,HEX);
}else if(codeType==PANASONIC){
irsend.sendPanasonic(codeValue,codeLen);
Serial.print("Sent Panasonic");
Serial.println(codeValue,HEX);
}else if(codeType==JVC){
irsend.sendJVC(codeValue,codeLen);
Serial.print("Sent JVC");
Serial.println(codeValue,HEX);
}else if(codeType==RC5||codeType==RC6){
if(!repeat){
toggle=1-toggle;
}
codeValue=codeValue&~(1<<(codeLen-1));
codeValue=codeValue|(toggle<<(codeLen-1));
if(codeType==RC5){
Serial.print("Sent RC5")'
Serial.println(codeValue,HEX);
irsend.sendRC5(codeValue,codeLen);
}else{
irsend.sendRC6(codeValue,codeLen);
Serial.print("Senr RC6")
Serial.println(codeValue,HEX);
}
}else if(codeType==UNKNOWN){
irsend.sendRaw(rawCodes,codeLen,38);
Serial.println("send raw");
}
}
void loadEepromValues(){
codeType=EEPROM.read(codeTypeEAddr);
delay(10);
codeLen=EEPROM.read(codeLenEAddr);
delay(10);
toggle=EEPROM.read(toggleEAddr);
delay(10);
codeType=EEPROM.read(codeTypeEAddr);
delay(10); //
EEPROM.get(codeValueEAddr,codeValue);
}
void writeEepromValue(){
EPPROM.write(codeTypeEAddr, codeType);
delay(10);
EEPROM.write(codeLenEaddr,codeLen);
delay(10);
EEPROM.write(toggleledEAddr,toggleEAddr);
delay(10);
EEPROM.put(codeValueEAddr,codeValue);
delay(10);
}
心得:
此例程使用了Arduino第三方库,且视频中也没有详细的代码解释,所以在造轮子的过程中总是磕磕绊绊,这就需要不断地查阅第三方库所附带的源码
以及各种文档与手册
;
这次造轮子其实是在看完视频一周之后才开始着手,当遇到磕绊而去查看过去的笔记的时候,真后悔当初没有把笔记录详细。记笔记的时候总是有这样的傲慢:现在懂了=永远懂了。这次造轮子的手足无措就是对自己的警示。记笔记一定要详细,这样就会不会“笔记用时方恨少
”!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)