使用方法:
1. 首先输入遥控品牌名称
2. 输入每个按键需要记录的条数
3. 每个按键录制完成之后,提示按c/C继续,其它键退出。
代码
String brandName=""; byte maxRecordsPerButton=0; boolean stop=false; int currentRecord =0; byte getNumber() { char * value; String s; s=getChars(); value = &s[0]; return atoi(value); } String getChars(void) { char rcv=' '; String str; while (rcv!='\n') { if (rcv!=' ') str += rcv; if (Serial.available()>0) rcv=Serial.read(); else rcv=' '; } return str; } void setup() { Serial.begin(9600); Serial.println("Please connect your datapin of IR Receiver to D2 for UNO, or D19 for MEGA1280"); Serial.println(" "); Serial.print("Please enter the brand name of your remote:"); brandName=getChars(); Serial.println(brandName); Serial.println(""); Serial.println("Please enter how many records "); Serial.print("would be recorded per button:"); maxRecordsPerButton = getNumber(); Serial.println(maxRecordsPerButton,DEC); Serial.println(" Ready to Record ... "); currentRecord=0; } void loop() { if (!stop) { while (currentRecord<maxRecordsPerButton) { if (currentRecord==0) { String buttonName=""; Serial.println(" "); Serial.print("Please enter button name:"); buttonName= getChars(); Serial.println(buttonName); } ///// Serial.println(" "); Serial.println("Press the button, please..."); listenIR(); printpulses(); Serial.println(" "); ///// currentRecord ++; if (currentRecord==maxRecordsPerButton) { Serial.println(" "); Serial.println(" "); Serial.println("Press c or C to continue, others to exit..."); char c; c=getChars()[0]; switch(c) { case 'c': case 'C': currentRecord=0; break; default: //stop stop=true; } } } } } void listenIR() { listenForIR(); }
采集部分程序是基于ladyada的代码
#define IRpin_PIN PIND #define IRpin 2// the maximum pulse we'll listen for - 65 milliseconds is a long time #define MAXPULSE 65000// what our timing resolution should be, larger is better// as its more 'precise' - but too large and you wont get// accurate timing #define RESOLUTION 20 // What percent we will allow in variation to match the same code #define FUZZINESS 20// we will store up to 100 pulse pairs (this is -a lot-) uint16_t pulses[100][2]; // pair is high and low pulse uint8_t currentpulse = 0; // index for pulses we're storing int listenForIR(void) { currentpulse = 0; while (1) { uint16_t highpulse, lowpulse; // temporary storage timing highpulse = lowpulse = 0; // start out with no pulse length // while (digitalRead(IRpin)) { // this is too slow! while (IRpin_PIN & (1 << IRpin)) { // pin is still HIGH // count off another few microseconds highpulse++; delayMicroseconds(RESOLUTION); // If the pulse is too long, we 'timed out' - either nothing // was received or the code is finished, so print what // we've grabbed so far, and then reset if ((highpulse >= MAXPULSE) && (currentpulse != 0)) { return currentpulse; } } // we didn't time out so lets stash the reading pulses[currentpulse][0] = highpulse; // same as above while (! (IRpin_PIN & _BV(IRpin))) { // pin is still LOW lowpulse++; delayMicroseconds(RESOLUTION); if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) { return currentpulse; } } pulses[currentpulse][1] = lowpulse; // we read one high-low pulse successfully, continue! currentpulse++; } } void printpulses(void) { Serial.println("\n\r\n\rReceived: \n\rOFF \tON"); for (uint8_t i = 0; i < currentpulse; i++) { Serial.print(pulses[i][0] * RESOLUTION, DEC); Serial.print(" usec, "); Serial.print(pulses[i][1] * RESOLUTION, DEC); Serial.println(" usec"); } // print it in a 'array' format Serial.println("int IRsignal[] = {"); Serial.println("// ON, OFF (in 10's of microseconds)"); for (uint8_t i = 0; i < currentpulse-1; i++) { Serial.print("\t"); // tab Serial.print(pulses[i][1] * RESOLUTION / 10, DEC); Serial.print(", "); Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC); Serial.println(","); } Serial.print("\t"); // tab Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC); Serial.print(", 0};"); }输入的结果如下
Please connect your datapin of IR Receiver to D2 for UNO, or D19 for MEGA1280
Please enter the brand name of your remote:SONY
Please enter how many records
would be recorded per button:3
Ready to Record ...
Please enter button name:PowerON
Press the button, please...
Received:
OFF ON
26388 usec, 4560 usec
4300 usec, 640 usec
1520 usec, 660 usec
1520 usec, 640 usec
1520 usec, 640 usec
380 usec, 660 usec
400 usec, 680 usec
380 usec, 660 usec
400 usec, 640 usec
420 usec, 640 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
400 usec, 640 usec
400 usec, 660 usec
400 usec, 660 usec
400 usec, 660 usec
400 usec, 640 usec
420 usec, 640 usec
1560 usec, 620 usec
400 usec, 640 usec
420 usec, 640 usec
420 usec, 640 usec
400 usec, 660 usec
400 usec, 660 usec
400 usec, 660 usec
1540 usec, 620 usec
400 usec, 660 usec
1540 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
45740 usec, 4560 usec
4280 usec, 640 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
400 usec, 660 usec
400 usec, 660 usec
400 usec, 640 usec
420 usec, 640 usec
400 usec, 680 usec
1520 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
400 usec, 660 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
420 usec, 640 usec
440 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
1540 usec, 640 usec
420 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
int IRsignal[] = {
// ON, OFF (in 10's of microseconds)
456, 430,
64, 152,
66, 152,
64, 152,
64, 38,
66, 40,
68, 38,
66, 40,
64, 42,
64, 156,
62, 154,
62, 154,
64, 40,
64, 40,
66, 40,
66, 40,
66, 40,
64, 42,
64, 156,
62, 40,
64, 42,
64, 42,
64, 40,
66, 40,
66, 40,
66, 154,
62, 40,
66, 154,
62, 154,
62, 156,
62, 154,
62, 156,
62, 154,
62, 4574,
456, 428,
64, 154,
62, 154,
64, 154,
62, 40,
66, 40,
66, 40,
64, 42,
64, 40,
68, 152,
62, 154,
64, 154,
62, 40,
66, 42,
64, 42,
62, 44,
62, 44,
62, 44,
62, 154,
62, 42,
64, 44,
62, 42,
64, 42,
64, 42,
62, 44,
62, 154,
64, 42,
62, 154,
64, 154,
62, 154,
62, 156,
62, 154,
62, 154,
64, 0};
Press the button, please...
Received:
OFF ON
45080 usec, 4560 usec
4260 usec, 660 usec
1540 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
400 usec, 660 usec
420 usec, 640 usec
400 usec, 640 usec
440 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
400 usec, 660 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
400 usec, 660 usec
440 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 620 usec
1540 usec, 640 usec
420 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
1540 usec, 620 usec
45760 usec, 4540 usec
4280 usec, 640 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 620 usec
420 usec, 640 usec
440 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
400 usec, 640 usec
440 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
420 usec, 640 usec
1540 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
420 usec, 640 usec
1540 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
int IRsignal[] = {
// ON, OFF (in 10's of microseconds)
456, 426,
66, 154,
62, 154,
62, 156,
62, 40,
66, 42,
64, 40,
64, 44,
62, 44,
62, 154,
62, 154,
64, 154,
62, 40,
66, 42,
64, 42,
62, 44,
62, 44,
62, 44,
62, 154,
62, 40,
66, 44,
62, 42,
64, 42,
64, 42,
64, 42,
62, 154,
64, 42,
62, 156,
62, 154,
62, 154,
64, 154,
62, 154,
62, 154,
62, 4576,
454, 428,
64, 156,
62, 154,
62, 154,
62, 42,
64, 44,
62, 42,
64, 42,
64, 42,
64, 154,
62, 154,
62, 154,
64, 40,
64, 44,
62, 44,
62, 44,
62, 44,
62, 42,
64, 154,
62, 42,
64, 42,
64, 42,
62, 44,
62, 44,
62, 42,
64, 154,
62, 44,
62, 154,
62, 156,
62, 154,
62, 154,
64, 154,
62, 154,
62, 0};
Press the button, please...
Received:
OFF ON
40692 usec, 4560 usec
4280 usec, 640 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 640 usec
380 usec, 660 usec
400 usec, 660 usec
420 usec, 640 usec
400 usec, 640 usec
440 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
400 usec, 660 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
420 usec, 640 usec
440 usec, 620 usec
440 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 620 usec
1540 usec, 640 usec
400 usec, 660 usec
1540 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
45740 usec, 4560 usec
4260 usec, 660 usec
1520 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
420 usec, 640 usec
440 usec, 620 usec
420 usec, 640 usec
420 usec, 640 usec
420 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
400 usec, 660 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
420 usec, 640 usec
1540 usec, 620 usec
400 usec, 660 usec
420 usec, 640 usec
420 usec, 620 usec
440 usec, 620 usec
440 usec, 620 usec
420 usec, 640 usec
1540 usec, 620 usec
440 usec, 620 usec
1540 usec, 620 usec
1560 usec, 620 usec
1540 usec, 620 usec
1540 usec, 640 usec
1540 usec, 620 usec
1540 usec, 620 usec
int IRsignal[] = {
// ON, OFF (in 10's of microseconds)
456, 428,
64, 154,
62, 154,
64, 154,
64, 38,
66, 40,
66, 42,
64, 40,
64, 44,
62, 154,
62, 156,
62, 154,
62, 40,
66, 42,
64, 42,
64, 42,
64, 42,
62, 44,
62, 154,
62, 42,
64, 44,
62, 44,
62, 42,
64, 42,
64, 42,
62, 154,
64, 40,
66, 154,
62, 154,
62, 154,
64, 154,
62, 154,
62, 154,
64, 4574,
456, 426,
66, 152,
64, 154,
62, 154,
62, 42,
64, 44,
62, 42,
64, 42,
64, 42,
64, 154,
62, 154,
62, 154,
64, 40,
66, 42,
64, 42,
62, 44,
62, 44,
62, 42,
64, 154,
62, 40,
66, 42,
64, 42,
62, 44,
62, 44,
62, 42,
64, 154,
62, 44,
62, 154,
62, 156,
62, 154,
62, 154,
64, 154,
62, 154,
62, 0};
Press c or C to continue, others to exit...
从键盘读取字符串的代码可能存在缺陷,输入空格时,显示的时候将被忽略。
回复删除