Arduino两块板用红外传输数据怎么样抗干扰?-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4509|回复: 3

[已解决] Arduino两块板用红外传输数据怎么样抗干扰?

[复制链接]
发表于 2013-12-10 11:41 | 显示全部楼层 |阅读模式
现在有两块Arduino板,想在两块板之间用红外传输数据。现在能传输到数据。但是如果旁边有红外线遥控器发送信号时就会收到受到干扰。请问怎么让才能解决。还有我想如果在两块Arduino传输数据的时候如果有物理阻碍导致无法接受信息,但是阻碍消失后数据还能继续传输。要怎么样才能实现?

谢谢大侠帮助了~~

这是我的Arduino代码。


bool transmitting_IR;
bool receiving_IR;

void turn_off_IR ()
{
TCCR2A = 0; // Disconnect PWM
TCCR2B = 0; // Stops the timer
OCR2A = 0;  // No timer top
digitalWrite(11, LOW);  // Ensure output is off
transmitting_IR = false;
}
void turn_on_IR ()
{
TCCR2A = _BV(WGM21) | _BV(COM2A0); // This mode toggles output once per timer cycle
TCCR2B = _BV(CS20);  
OCR2A = 210;  38 khz
transmitting_IR = true;
}

bool detect_IR()
{
return receiving_IR = ! digitalRead(12);
}

void setup ()
{
Serial.begin(9600);
pinMode (11, OUTPUT); // IR LED
pinMode (12, INPUT);  // IR receiver
turn_off_IR();  // My helper function for controlling the IR LED
detect_IR();    // Ensure the state is correct.
}

void loop ()
{
detect_IR();
char ch;
if(receiving_IR)
ch = readch();
ch = Serial.read();
if(ch>0)
sendch(ch);
}

unsigned long p=500;

char readch()
{
char ch = 0;
unsigned long start_time = micros();
while(micros() < start_time + p);
for(int i = 0; i < 8; i++)
{
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
int result = detect_IR();
ch = ch | (result << i);
}
Serial.print(ch);
}

void sendch(char ch)
{
turn_on_IR();
unsigned long start_time = micros();
while(micros() < start_time + 2*p);
for(int i = 0; i < 8; i++)
{
int result = (ch >> i) & 1;
if(result == 1)
{
turn_on_IR();
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
}
else
{
turn_off_IR();
unsigned long bit_time = micros();
while(micros() < bit_time + 2*p);
}
}
turn_off_IR();
unsigned long end_time = micros();
while(micros() < end_time + 2*p);
}



发表于 2013-12-10 13:57 | 显示全部楼层
本来调制到38K后就能抗一定干扰了,还有就是周围环境光不要太强....
发表于 2013-12-11 00:04 | 显示全部楼层
距离近点,避免其他光 都可以
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-4 03:41 , Processed in 0.074330 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表