Problem mit Verarbeitung von IR-Daten

Hallo alle

Ich möchte mir ein eigenes LaserTag-Spiel bauen. Da sich meine Programmierkenntnisse in Grenzen halten, habe ich auf einen Code eines instructables.com-Projekt zurückgegriffen. Dieses Projekt wurde ursprünglich mit einem Duemillanove gebaut, doch zumal der Autor einen 328er-Chip verwendet hatte, sollte die Kompatibilität kein Problem darstellen.

Die Hardware:

  • Arduino Uno SMD Rev. 3

  • Empfänger: Platine mit einem IR-Empfänger, 38kHz Trägerfrequenz (habe verschiedene Empfänger-Frequenzen, AGC2/ AGC4 getestet), Signal an Pin12 (inklusive Pullup-Widerstand)

  • Sender: IR-Diode, angesteuert über einem NPN-Leistungstransistor von Pin2

  • Trigger-Switch: Legt Pin 3 auf GND (mit Pullup-Widerstand)

Im Serial Monitor funktioniert alles soweit. Nach dem Start kommt:

“Startup…
Oscilation time period /2: 9
Pulses: 23
Byte1: 01000001
Byte2: 00100001
Parity: 0
Ready…”

Auch die Schussabgabe wird angezeigt, doch bei einem “Treffer” (IR-Pulse) kommt immer:

“ERROR
sensor: 1… 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2”

Der relevante Code-Abschnitt, wo die Daten auf Validität geprüft werden:

// Parity Check. Was the data received a valid signal?
    check = 0;
    for(int i = 1; i <= 16; i++) {
      if(received[i] == 1){check = check + 1;}
      if(received[i] == 2){error = 1;}
    }
    // Serial.println(check);
    check = check >> 0 & B1;
    // Serial.println(check);
    if(check != received[17]){error = 1;}
    if(error == 0){Serial.println("Valid Signal");}
    else{Serial.println("ERROR");}
    if(error == 0){interpritReceived();}
    digitalWrite(hitPin,LOW);
  }

Hat jemand ne Idee, wo der Fehler liegt?

LaserTag_Ver1.ino (18.9 KB)

laurin123:
Hat jemand ne Idee, wo der Fehler liegt?

Ja, der liegt irgendwo in dem Code, den Du nicht gezeigt hast :-]

Hier noch die gesamte “receive”-Funktion (Verarbeitung des empfangenen IR-Signals):

void receiveIR() { // Void checks for an incoming signal and decodes it if it sees one.
  int error = 0;
 
  if(digitalRead(IRreceivePin) == LOW){    // If the receive pin is low a signal is being received.
    digitalWrite(hitPin,HIGH);
    if(digitalRead(IRreceive2Pin) == LOW){ // Is the incoming signal being received by the head sensors?
      received[0] = 1;
    }
    else{
      received[0] = 0;
    }
   
    while(digitalRead(IRreceivePin) == LOW){
    }
    for(int i = 1; i <= 17; i++) {                        // Repeats several times to make sure the whole signal has been received
      received[i] = pulseIn(IRreceivePin, LOW, timeOut);  // pulseIn command waits for a pulse and then records its duration in microseconds.
    }
   
    Serial.print("sensor: ");                            // Prints if it was a head shot or not.
    Serial.print(received[0]); 
    Serial.print("...");
   
    for(int i = 1; i <= 17; i++) {  // Looks at each one of the received pulses
      int receivedTemp[18];
      receivedTemp[i] = 2;
      if(received[i] > (IRpulse - 200) &&  received[i] < (IRpulse + 200)) {receivedTemp[i] = 0;}                      // Works out from the pulse length if it was a data 1 or 0 that was received writes result to receivedTemp string
      if(received[i] > (IRpulse + IRpulse - 200) &&  received[i] < (IRpulse + IRpulse + 200)) {receivedTemp[i] = 1;}  // Works out from the pulse length if it was a data 1 or 0 that was received  
      received[i] = 3;                   // Wipes raw received data
      received[i] = receivedTemp[i];     // Inputs interpreted data
     
      Serial.print(" ");
      Serial.print(received[i]);         // Print interpreted data results
    }
    Serial.println("");                  // New line to tidy up printed results
   
    // Parity Check. Was the data received a valid signal?
    check = 0;
    for(int i = 1; i <= 16; i++) {
      if(received[i] == 1){check = check + 1;}
      if(received[i] == 2){error = 1;}
    }
    // Serial.println(check);
    check = check >> 0 & B1;
    // Serial.println(check);
    if(check != received[17]){error = 1;}
    if(error == 0){Serial.println("Valid Signal");}
    else{Serial.println("ERROR");}
    if(error == 0){interpritReceived();}
    digitalWrite(hitPin,LOW);
  }
}

Und hier die “shoot”-Funktion:

void shoot() {
  if(FIRE == 1){ // Has the trigger been pressed?
    Serial.println("FIRE 1");
    sendPulse(IRtransmitPin, 4); // Transmit Header pulse, send pulse subroutine deals with the details
    delayMicroseconds(IRpulse);
 
    for(int i = 0; i < 8; i++) { // Transmit Byte1
      if(byte1[i] == 1){
        sendPulse(IRtransmitPin, 1);
        //Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }

    for(int i = 0; i < 8; i++) { // Transmit Byte2
      if(byte2[i] == 1){
        sendPulse(IRtransmitPin, 1);
       // Serial.print("1 ");
      }
      //else{Serial.print("0 ");}
      sendPulse(IRtransmitPin, 1);
      delayMicroseconds(IRpulse);
    }
    
    if(myParity == 1){ // Parity
      sendPulse(IRtransmitPin, 1);
    }
    sendPulse(IRtransmitPin, 1);
    delayMicroseconds(IRpulse);
    Serial.println("");
    Serial.println("DONE 1");
  }

Ich bin dankbar über jeden Input! Im Übrigen auch weitere instructables-User, die das selbe Problem mit dem Code haben.