如何写动态文本到LED显示屏串行w/ Arduino
How to write dynamic text to LED Display over Serial w/ Arduino
所以我想我接近了,但是我遇到了瓶颈。这是我想要完成的:我有一个计算点击次数的程序,并且在值更新时将串行数据发送到Arduino (UNO)。
到目前为止,我有以下工作:- LED显示屏上的滚动字幕(来自Freetronics - http://www.freetronics.com/products/dot-matrix-display-32x16-red#.UOBeKInjmdM)Python脚本通过Serial(使用PySerial)向Arduino写入新数据- Arduino是正确地接收串行监视器中的数据…
所以我的问题是我不能让它写到LED显示屏,请帮助!
下面是我的代码:
import serial
import argparse
myserial = serial.Serial('/dev/tty.usbmodemfd121', 9600)
parser = argparse.ArgumentParser(description='Example with non-optional arguments')
parser.add_argument('count', action="store", type=str)
results = parser.parse_args()
count = results.count
message = "total clicks: " + count
print message
myserial.write(message)
例子:$ python app.py 200
这将发送"total clicks: 200"到Arduino
这是我的Arduino草图:
/*
Scrolling text demonstration sketch for Freetronics DMD.
See http://www.freetronics.com/dmd for resources and a getting started guide.
Note that the DMD library uses the SPI port for the fastest, low overhead writing to the
display. Keep an eye on conflicts if there are any other devices running from the same
SPI port, and that the chip select on those devices is correctly set to be inactive
when the DMD is being written to.
*/
// you always need the code from here...........
#include <DMD.h> // for DMD
#include <SPI.h> // SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <TimerOne.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"
#include "Arial_14.h"
#define DISPLAYS_ACROSS 1 // change to 2 for two screens, etc.
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN); // creates instance of DMD to refer to in sketch
char message[] = "test string to be updated";
char serIn; //var that will hold the bytes in read from the serialBuffer
void ScanDMD() // necessary interrupt handler for refresh scanning of DMD
{
dmd.scanDisplayBySPI();
}
void setup()
{
//initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 5000 ); //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
dmd.clearScreen( true ); //true is normal (all pixels off), false is negative (all pixels on)
Serial.begin(9600);
}
void loop()
{
// only if there are bytes in the serial buffer execute the following code
if(Serial.available()) {
//keep reading and printing from serial untill there are bytes in the serial buffer
while (Serial.available()>0){
serIn = Serial.read(); //read Serial
Serial.write( byte(serIn));
}
//the serial buffer is over just go to the line (or pass your favorite stop char)
Serial.println();
}
// Now I want to write the Serial message ti the DMD Disply
dmd.selectFont(Arial_Black_16);
// the text in the quotes in the next line will be scrolled across the display(s).
// message writes the TEST message above, but I want it to write serIn variable (serial data)
dmd.drawMarquee(message,strlen(message),(32*DISPLAYS_ACROSS)-1,0);
// THIS IS WHAT I WANT, but I get this error "invalid conversion from 'char' to 'const char*'"
//dmd.drawMarquee(serIn,strlen(serIn),(32*DISPLAYS_ACROSS)-1,0);
long start=millis();
long timer=start;
boolean ret=false;
while(!ret){
if ((timer+30) < millis()) {
ret=dmd.stepMarquee(-1,0);
timer=millis();
}
}
delay(100);
}
我一直遇到的错误是dmd.drawMarquee()
将第一个参数作为字符串,我对c++一无所知,所以我认为我搞砸了数据类型。
因此,虽然我担心如何将串行读取与显示驱动混合在一起,但您可以将串行读取更改为字符串:
串行读取部分:
if(Serial.available()) {
...
}
只是读取单个字符。你需要将它们存储在缓冲区中。
改变:
char serIn; //var that will hold the bytes in read from the serialBuffer
:
char serIn[40]; //buffer that will hold the bytes in read from the serialBuffer
然后是串行循环:
if(Serial.available()) {
int chars_in = 0;
//keep reading and printing from serial untill there are bytes in the serial buffer
while (Serial.available()>0 && chars_in<39){
serIn[chars_in] = Serial.read(); //read Serial
Serial.write( byte(serIn[chars_in]));
chars_in++;
}
serIn[chars_in] = 0;
//the serial buffer is over just go to the line (or pass your favorite stop char)
Serial.println();
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 人脸跟踪arduino代码的优化
- vscode下的Arduino代码出现意外编译错误
- Arduino从Txt读取整数
- Arduino C++在构造函数中用参数声明对象数组
- C++ Boost::asio串行通信与Arduino无法写入
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 'val' Arduino 错误代码之前的预期'('
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- Arduino-C++ bool 不会从 false 变为 true
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- cmsis_compiler.h:在为 Arduino Due 编译 Arduino Tensorflow lite 库
- Arduino SocketIoClient - 没有用于调用的匹配函数
- Arduino LCD 显示屏显示混乱的字母
- 如何写动态文本到LED显示屏串行w/ Arduino