|
|
目标是按下遥控器,发送一个a或者A,然后另一块的lcd有显示,但目前是发送一次a后,再发A没反应,必须通过关闭电脑上的串口监视器窗口才行,也就是发一次a,lcd有显示,关了IDE的串口监视器,在打开,发送A,另一块板子的lcd有显示,想发送a必须关了串口监视器,求大神解释一下,是tx,rx就这样,还是代码没处理好,清空缓存啥的,求解释
************从机代码*****************************************************
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
////Technical support:[email protected]
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
Adafruit_GFX_Button buttons[15];
char ss;
void setup()
{
Serial.begin(9600);
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
}
void loop()
{
if (Serial.available()>0){
ss=Serial.read();
if(ss=='a')
{
tft.fillScreen(BLACK);
tft.setCursor(40, 40);
tft.setTextColor(RED); tft.setTextSize(2);
tft.println("Danger!!!");
ss=0;
delay(3000);
Serial.print('b'); //收到'a'或者'A'后向主机发一个'b',
} //目的是让主机可以循环读取下一个数
else if(ss=='A')
{
tft.fillScreen(BLACK);
tft.setCursor(40, 40);
tft.setTextColor(RED); tft.setTextSize(2);
tft.println("Safe!!!");
ss=0;
delay(3000);
Serial.print('b');//收到'a'或者'A'后向主机发一个'b',
//目的是让主机可以循环读取下一个数
}
}
}
*********************主机代码****************
#include <IRremote.h>
#include <IRremoteInt.h>
const int irReceiverPin = 3;
IRrecv irrecv(irReceiverPin);
decode_results results;
char ss;
int i=1;
void setup()
{
Serial.begin(9600); //设置串口速度
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results)){
if(results.value==275196119){
if(i>0){ //启动时的开关信号,这样可以避免主程序陷入死循环
Serial.print('a'); //先向从机发一个‘a’,
i=0;
}
ss=Serial.read(); //再读串口,接受从机的字符‘b’
if(ss=='b'){
Serial.print("ok1");
ss=0;
i=1;//让i重新赋值
}
irrecv.resume();
delay(500);
}
else if(results.value==275212439){
if(i>0){ //启动时的开关信号,这样可以避免主程序陷入死循环
Serial.print('A'); //先向从机发一个‘A’,
i=0;
}
ss=Serial.read(); //再读串口,接受从机的字符‘b’
if(ss=='b'){
Serial.print("ok2");
ss=0;
i=1;//让i重新赋值
}
irrecv.resume();
delay(500);
}
}
} |
|