import time
import utime
from machine import Pin
time.sleep(0.1) # Wait for USB to become ready
int IRreceivePin = 12; #// The pin that incoming IR signals are read from
int IRreceive2Pin = 11; #// Allows for checking external sensors are attached as well as distinguishing between sensor locations (eg spotting head shots)
#//Incoming signal Details
int received[18]; #// Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
print("Hello, Pi Pico tag!")
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico W!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(1); // this speeds up the simulation
}
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);
}
}