【发布时间】:2019-05-30 09:06:50
【问题描述】:
我正在尝试通过串行向 Arduino 发送一个数值来控制可单独寻址的 LEDstrip。使用 Arduino IDE“串行监视器”,我可以毫无问题地向灯条发送一个数字。但是,当我尝试通过在处理过程中从文本文件中读取来从外部执行它时,它不会通过。
经过一些调试,我可以看到 Processing 具有正确的数字并将其存储在变量中。然而,灯头只会在 LED 上亮起,而不是给定的数字。
处理代码:
import processing.serial.*;
import java.io.*;
Serial myPort; // Create object from Serial class
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
while(true) {
// Read data from the file
{
String[] lines = loadStrings("Lifenumber.txt");
int number = Integer.parseInt(lines[0]);
println(number);
myPort.write(number);
delay(5000);
}
}
}
Arduino 代码:
if ( Serial.available()) // Check to see if at least one character is available
{
char ch = Serial.read();
if(index < 3 && ch >= '0' && ch <= '9'){
strValue[index++] = ch;
}
else
{
Lnum = atoi(strValue);
Serial.println(Lnum);
for(i = 0; i < 144; i++)
{
leds[i] = CRGB::Black;
FastLED.show();
delay(1);
}
re = 1;
index = 0;
strValue[index] = 0;
strValue[index+1] = 0;
strValue[index+2] = 0;
}
}
我希望程序做的是从文本文件中读取一个数字,然后点亮 144led 灯条上的那个数量的 LED。
【问题讨论】:
-
write和println之间的巨大差异。 (你想要myPort.println(number);) -
@datafiddler 它说函数“println(int)”不存在
-
@EpsilonRho 文本文件中的数字是什么样的,它代表什么? (例如,它是您想要打开的 LED 数量吗?它是所有 LED 的亮度值吗?)
-
另外,您是否需要每 5 秒重新加载该文本文件?
-
@George Profenza 他们没有,我只是将它放在一个循环中进行测试。是的,文本文件中的数字代表应该点亮多少个 LED。
标签: arduino serial-port processing