基本上,我正在关注LED条形图中 BarGraph 中的教程代码。我没有电位器,所以我想根据 Processing 中的调光器示例,使用Dimmer串行写入来模仿它。我已将sensorReading
值设置为Processing应用程序的输入(将其网格更新为1023个元素),如下所示:
int sensorReading;
if (Serial.available()) {
// Read the most recent byte (which will be from 0 to 1023):
sensorReading = Serial.read();
}
这会根据我在处理应用程序中的网格中的鼠标位置点亮LED。然而,LED非常暗淡。如果我更改将sensorReading
值设置为:
int sensorReading = random(0, 1023);
然后LED点亮得更亮。由于LED都在数字输出引脚上,我认为它只会根据sensorReading
值发送/关闭,并且与亮度无关。我错过了什么?
以下是处理代码:
// Dimmer - sends bytes over a serial port
// by David A. Mellis
//
// This example code is in the public domain.
import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (for example, 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
//port = new Serial(this, Serial.list()[0], 9600);
// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
port = new Serial(this, "COM6", 9600);
}
void draw() {
// Draw a gradient from black to white
for (int i = 0; i < 1024; i++) {
stroke(i);
line(i, 0, i, 150);
}
// Write the current X-position of the mouse to the serial port as
// a single byte.
port.write(mouseX);
}
这是Arduino代码:
// These constants won't change:
const int analogPin = A0; // The pin that the potentiometer is attached to.
const int ledCount = 10; // The number of LEDs in the bar graph.
int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11 }; // An array of pin numbers to which LEDs are attached.
void setup() {
Serial.begin(9600);
// Loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// Read the potentiometer:
// int sensorReading = random(0, 1023);
// delay(250);
byte streamReading;
if (Serial.available()) {
// Read the most recent byte (which will be from 0 to 255):
sensorReading = Serial.read();
}
//Serial.println(sensorReading);
// Map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 255, 0, ledCount);
// Loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// If the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// Turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
答案 0 :(得分:1)
问题:您的处理代码为sending data constantly,一直向您的Arduino 发送串行数据:
在setup()之后直接调用,draw()函数连续执行 执行其块内包含的代码行,直到 程序停止或调用noLoop()。调用draw() 自动,绝不应该明确调用。
这会导致您的Arduino草图频繁更新LED开/关状态,并且考虑到您如何读取数据,这将导致LED脉冲非常快。
解决方案:最简单的解决方法是为Arduino或Processing草图添加延迟。更好的解决方案是将Processing代码修改为仅在值发生变化时发送数据;虽然请注意,由于鼠标值几乎不断变化,并且这些更改对于Arduino代码不会很重要,因此您可能仍会有很多不必要的闪烁。但是,如果你在Arduino代码中修复了串行读取功能,那么闪烁也不会是一个问题。)
代码:修改您的处理代码以跟踪上次阅读,只有在不同时才更新:
int lastMouseX;
void draw() {
// draw a gradient from black to white ...
int newMouseX = mouseX;
if (newMouseX != lastMouseX) {
lastMouseX = newMouseX
// write the current X-position of the mouse to the serial port as
// a single byte
port.write(mouseX);
}
其他问题:首先,如果您期望值0-1024,则会出现问题:Arduino的analogWrite()函数需要0到255字节。
其次,正如Martin Thompson指出的那样,您可能正在从处理应用程序发送128
之类的字符串,然后使用其ASCII值来设置强度。由于0
到9
的ASCII值在48-57范围内,因此强度相对较低。请注意,当一个字符串包含多个字节(例如128
)时,您只使用一个字节来表示强度。所以你有两个选择:
此代码可能如下所示:
#include <stdlib.h>
int idxChar = 0;
#define BUFFER_SIZE 10
char strIntensity[BUFFER_SIZE];
...
while (Serial.available()) {
// read the string representation of a byte
// assuming bytes are separated by non-numeric characters
// and never overflow
char ch = Serial.read();
if ( (ch >= '0') && (ch <= '9') ) {
strIntensity[idxChar++] = ch;
} else {
strIntensity[idxChar] = 0;
sensorReading = atoi(strIntensity);
idxChar = 0;
}
if (idxChar>=BUFFER_SIZE-1) {
// (need space for the null char at the end too
// Buffer overflow. Bail
idxChar = 0;
}
}
答案 1 :(得分:0)
//读取最近的字节(从0到1023)
字节从0到255.它们(通常)代表ASCII字符集中的字符。
如果您希望读取0到1023之间的数字,则可以一次传输一个字符(即字符1
后跟字符0
代表数字10) - 在在哪种情况下你必须解析它们,把它们变成一个可以按照你的预期使用的数字。
parseInt函数可能就是您所需要的 - a tutorial on reading ASCII integers can be found here