Go Down

Topic: Problem with processing data from infrared transmission (Read 43 times) previous topic - next topic

laurin123

Hi all

I'm trying to make my one laser tag system. Since I'm not that good in programming, I took an existing code from an instructable project. This was originally designed for an arduino duemillanove, but since the author had a 328-chip on it, there shouldn't be any compatibility problems.

Hardware:

- Arduino Uno SMD Rev3

- Receiver: Board with an IR-receiver, 38kHz frequency (I tried different frequencies). Singnal goes to Pin12 (including pullup resistor)

- Transmitter: IR-diode, driven by a power NPN-transistor from Pin2

- Trigger Switch: Pushing connects Pin3 to ground (including pullup resistor)

In the Serial Monitor everything looks fine at first. After startup it shows:

"Startup...
Oscilation time period /2: 9
Pulses: 23
Byte1: 01000001
Byte2: 00100001
Parity: 0
Ready...."

Also the firing is OK in the Monitor, but when a do a "Hit" (IR-pulse to the receiver) then always this error shows up:

"ERROR
sensor: 1... 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2"

The relevant code section is the following:

Code: [Select]
// 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);
  }



Any help is very much appreciated. Not just by me since many others had the exact same problem, although the author claims the code is working!

laurin123

Sorry I didn't post all of the relevant code.

This is the "receive"-function:

Code: [Select]
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);
  }
}






and here the "shoot"-function:
Code: [Select]
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");
  }



Any help is very much appreciated!

PaulS

Quote
void receiveIR() { // Void checks for an incoming signal and decodes it if it sees one.
receiveIR() is NOT a void. It is a function.

Quote
Sorry I didn't post all of the relevant code.
If you KNOW that that is now all of the relevant code, then I am sure that you are equally capable of fixing it.

If you are, then posting here was a waste of our time.

If are, then what makes you think you are the expert in determining what is relevant?
The art of getting good answers lies in asking good questions.

Go Up