我有一个连接到Arduino的LCD,它显示图像,如果触摸了图像,则显示在序列上。我想使用Xbee将串行写入的数据发送到PC。我已经配置了Xbee模块,它们工作正常。
我面临的问题是PC上的数据没有自动发送。相反,如果我在串口中写东西,那么它就会在PC上显示。使用的代码是
// UTFT_SdRaw_800x480_Demo
// Copyright (C)2015 Graham Lawrence (GHLawrence2000). All Rights reserved.
// web: https://github.com/ghlawrence2000/UTFT_SdRaw
//
// This program is a demo of how to use the functions provided by UTFT_SdRaw.
//
// This program requires the UTFT, UTouch, UTFT_Buttons and SdFat libraries.
//
#include <SPI.h>
// SdFat lib from here :-
// https://github.com/greiman/SdFat/archive/master.zip
#include <SdFat.h>
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_SdRaw.h>
#include <SoftwareSerial.h>
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
#define SD_CHIP_SELECT 53 // SD chip select pin
// file system object
SdFat sd;
// print stream
SoftwareSerial XBee(10, 11); // RX, TX
// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!
//UTFT myGLCD(CPLD, 38, 39, 40, 41);
//UTFT myGLCD(CTE50, 38, 39, 40, 41);
UTFT myGLCD(CTE70, 38, 39, 40, 41);
// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due (JP10): 6, 5,32, 3, 2
// Teensy 3.x TFT Test Board : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch myTouch(6, 5, 4, 3, 2);
UTFT_SdRaw myFiles(&myGLCD);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
XBee.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for DUE & Leonardo only
}
Serial.println(F("Initialising SD card..."));
bool mysd = 0;
// see if the card is present and can be initialized:
while (!mysd) {
if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
Serial.println(F("Card failed, or not present"));
Serial.println(F("Retrying...."));
} else {
mysd = 1;
Serial.println(F("Card initialised."));
}
}
Serial.println(F("Initialising LCD."));
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
Serial.println(F("LCD initialised."));
myGLCD.clrScr();
myFiles.load(0, 0, 800, 480, "lcd1.raw", 2, 0);
}
byte dispScreen = 1; // Currently displayed screen, screens shown below //
// 1-home, 2-lights, , 3-temp, 4-fogger, 5-mister, 6-fan, 7-clock, 8-screen //
void processMyTouch() {
myTouch.read();
int x = myTouch.getX();
int y = myTouch.getY();
switch (dispScreen) { // //
// //
case 1: // home screen //
// do stuff here for screen 1
if ((x > 650 && x < 800) && (y > 20 && y < 460)) {
//button 'back'
myGLCD.clrScr();
dispScreen = 2; // <======================================= change screen each time you go to a new screen
myFiles.load(0, 0, 800, 480, "lcd_2.raw", 2, 0);
}
if ((x > 19 && x < 340) && (y > 21 && y < 212)) {
//button 'news'
Serial.println("Drinks");
}
if ((x > 400 && x < 5700) && (y > 21 && y < 212)) {
//button 'news'
Serial.println(F("Food"));
}
if ((x > 19 && x < 340) && (y > 240 && y < 448)) {
//button 'news'
Serial.println(F("Tea"));
}
if ((x > 400 && x < 550) && (y > 240 && y < 448)) {
//button 'news'
Serial.println(F("Water"));
}
break;
case 2:
// do stuff here for screen 2
if ((x > 170 && x < 430) && (y > 21 && y < 212)) {
//button 'news'
Serial.println(F("news"));
}
if ((x > 17 && x < 100) && (y > 30 && y < 400)) {
//button 'news'
myGLCD.clrScr();
dispScreen = 1; // <======================================= change screen each time you go to a new screen
myFiles.load(0, 0, 800, 480, "lcd1.raw", 2, 0);
}
if ((x > 530 && x < 750) && (y > 21 && y < 212)) {
//button 'news'
Serial.println(F("light."));
}
if ((x > 530 && x < 750) && (y > 240 && y < 448)) {
//button 'news'
Serial.println(F("Blanket"));
}
if ((x > 170 && x < 430) && (y > 240 && y < 448)) {
//button 'news'
Serial.println(F("Medic"));
}
break;
}
}
unsigned long prevMillisTouch = 0; // track time between touches //
unsigned long TouchRepeatDelay = 500; // millis //
void loop() {
unsigned long currentMillis = millis(); // get current millis //
if (myTouch.dataAvailable()) { // //
// make sure it's been .5 sec (initially) between touches //
if (currentMillis - prevMillisTouch > TouchRepeatDelay) { //
// //
prevMillisTouch = currentMillis; // reset the touch timer //
processMyTouch(); //
} // //
} //
//
if (Serial.available()) {
// If data comes in from serial monitor, send it out to XBee
XBee.write(Serial.read());
}
if (XBee.available()) {
// If data comes in from XBee, send it out to serial monitor
Serial.write(XBee.read());
}
}
示例:
if ((x > 19 && x < 340) && (y > 21 && y < 212)) {
//button 'news'
Serial.println("Drinks");
&#39;饮料&#34;如果我触摸上面的坐标,则显示在序列号上。但是,数据不会发送到其他Xbee模块。如果我在序列中手动写饮料,它会显示在另一个Xbee模块上。
任何解决方案。
由于
答案 0 :(得分:1)
简答:
您需要XBee.println("Drinks");
代替Serial.println("Drinks");
才能发送字符串&#39;饮料&#39;到另一个XBee模块。
Serial.println
仅在本地控制台上写入文本,XBee.println
只会将数据发送到其他XBee。如果您希望在控制台上发送和显示数据,则需要调用这两个函数。
答案很长:
if (Serial.available()) {
// If data comes in from serial monitor, send it out to XBee
XBee.write(Serial.read());
}
这部分代码读取您在串行控制台上写的任何内容,并将其发送给Xbee。
if (XBee.available()) {
// If data comes in from XBee, send it out to serial monitor
Serial.write(XBee.read());
}
这部分代码读取Xbee上收到的任何内容并将其写入串行控制台。
Serial
是您的PC和arduino之间的连接。 XBee
是arduino和XBee之间的连接。在控制台中手动编写文本是有效的,因为第一块代码通过调用XBee.write
函数重新传输您写入XBee的内容。如果您想将数据发送到XBee,则需要致电XBee.write
/ XBee.println
个功能。