Pilot S107G w Arduino / Remote control S107G in Arduino
30 Grudzień 2011 Arduino
Jak za pomocą pilota od helikoptera, model S107G sterować poprzez podczerwień moduł Arduino
Pilot od S107G

Wyjście z portu

Odbiornik podczerwieni TSOP

Schemat podłączeniowy
Kod programu
[ csharp ] S107G.pdeZaznacz cały
/* Raw IR decoder sketch! This sketch/program uses the Arduno and a PNA4602 to decode IR received. This can be used to make a IR receiver (by looking for a particular code) or transmitter (by pulsing an IR LED at ~38KHz for the durations detected Code is public domain, check out www.ladyada.net and adafruit.com for more tutorials! */ // We need to use the 'raw' pin reading methods // because timing is very important here and the digitalRead() // procedure is slower! //uint8_t IRpin = 2; // Digital pin #2 is the same as Pin D2 see // http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping #define IRpin_PIN PIND #define IRpin 2 // the maximum pulse we'll listen for - 65 milliseconds is a long time #define MAXPULSE 200 // what our timing resolution should be, larger is better // as its more 'precise' - but too large and you wont get // accurate timing #define RESOLUTION 10 // 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 void setup(void) { Serial.begin(9600); Serial.println("Ready to decode IR!"); } void loop(void) { 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)) { remoteControl(); // printpulses(); currentpulse=0; return; } } // 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)) { currentpulse=0; return; } } pulses[currentpulse][1] = lowpulse; // we read one high-low pulse successfully, continue! currentpulse++; } void remoteControl(void) { int heightH = 0; int leftRight = 0; int forwardBack = 0; unsigned long aaa=0; unsigned int bits[currentpulse]; for (uint8_t i = 0; i < currentpulse; i++) { /*250 - 370 230 - 350 590 - 320 640 - 330 640 - 370 210 – 370 …. 1403F7A45 So, the header is a 2000 ms on, 2000ms off, the 0 a 300/300 pair and the 1 a 300/600 pair pulse.*/ if (pulses[i][0] < 1000){ if ((pulses[i][0] - pulses[i][1])>200){ bits[currentpulse-i] = 0; }else{ bits[currentpulse-i] = 1; } } } int ik=8; for(int i = 16 ; i > 8 ; i--) { ik--; if (bits[i]==1){ heightH = heightH + pow(2,ik); } // Serial.print(bits[i]); } ik=8; for(int i = 32 ; i > 24 ; i--) { ik--; if (bits[i]==1){ leftRight = leftRight + pow(2,ik); } // Serial.print(bits[i]); } ik=8; for(int i = 24 ; i > 16 ; i--) { ik--; if (bits[i]==1){ forwardBack = forwardBack + pow(2,ik); } //Serial.print(bits[i]); } Serial.print("height: "); Serial.print((int)heightH); Serial.print(" left/right: "); Serial.print((int)leftRight); Serial.print(" forw/back: "); Serial.print((int)forwardBack); Serial.println(); } void printpulses(void) { String asd = ""; unsigned long aaa=0; for (uint8_t i = 0; i < currentpulse; i++) { /*250 - 370 230 - 350 590 - 320 640 - 330 640 - 370 210 – 370 …. 1403F7A45 So, the header is a 2000 ms on, 2000ms off, the 0 a 300/300 pair and the 1 a 300/600 pair pulse.*/ if (pulses[i][0] < 1000){ if ((pulses[i][0] - pulses[i][1])>400){ asd = asd + 0; }else{ asd = asd + 1; } Serial.print(pulses[i][0] * RESOLUTION, DEC); Serial.print(" usec, "); Serial.print(pulses[i][1] * RESOLUTION, DEC); Serial.println(" usec"); //asd = asd + "," + pulses[i][1]; } } Serial.println(asd); }
Copyright © TutorialSource 2012

