I'm trying to build some little thing like laser War (Milestag,maybe some guys from western know).
It use IR to simulate fire , you hit someone by IR instead of color ball or bullet.
I make a simple IR sender and receiver program based on IRLib2.
The hardware based on milestag’s design:
The MCU will be replaced with Nano, use the pin9 as PWM out.
I just use 1 pin to send out, not 2 like the original design( is it the reason leading to failure?).
The receiver did receive the signal send by sender,but received info is not same with sent(even think of tolerance).
Package looks like this:
headmark,space,mark,space,mark,space...
I have printed out the received signal,it is not right:
- Total counts is not right, less than sent, and there is no regular pattern;
- The time interval is not right. E.g. Headmark should be 2400us,but received is far away from that, actually most of the interval is not right.
The source segments:
Send :
void IRsender::send(uint16_t data) {
//try{
digitalWrite(13,HIGH); //debug
mark(headMark);
space(headSpace);
for (uint8_t i = 0; i <numBits; i++) {
if (data & TOPBIT) {
mark(markOne); space(spaceBoth);
}
else {
mark(markZero); space(spaceBoth);
}
data <<= 1;
}
digitalWrite(13,LOW); //debug
};
#define IR_SEND_MARK_TIME(time) IRLibDelayUSecs(time)
void IRLibDelayUSecs(uint16_t time) {
if(time){if(time>16000) {delayMicroseconds(time % 1000); delay(time/1000); } else delayMicroseconds(time);};
}
void IRsender::mark(uint16_t time) {
IR_SEND_PWM_START;
IR_SEND_MARK_TIME(time);
}
void IRsender::space(uint16_t time) {
IR_SEND_PWM_STOP;
delayMicroseconds(time);
}
Receive:
void IRrecv_Handler(){
uint32_t volatile curUs = micros();
uint32_t deltaUs = curUs - recvGlobal.preUs;
value[idx] = deltaUs;
recvGlobal.preUs = curUs;
if(++idx>Max) NotifyDecode();
}
attachInterrupt(intrNum, IRrecv_Handler, CHANGE);