Arduino 数字输出的亮度因电平输入类型而异
基本上,我遵循的是LED条形图的教程代码。我没有电位计,所以我想根据中的调光器示例,通过串行写入来模拟它。我已将Arduino 数字输出的亮度因电平输入类型而异,arduino,Arduino,基本上,我遵循的是LED条形图的教程代码。我没有电位计,所以我想根据中的调光器示例,通过串行写入来模拟它。我已将sensorReading值设置为处理应用程序的输入(将其网格更新为1023个元素),如下所示: 这会根据我在处理应用程序网格中的鼠标位置点亮LED。然而,LED非常暗淡。如果我将传感器读数设置方式更改为: int sensorReading = random(0, 1023); 然后LED会亮得更亮。由于LED都在数字输出引脚上,我认为它只会根据传感器读数值发出开/关信号,而与
sensorReading
值设置为处理应用程序的输入(将其网格更新为1023个元素),如下所示:
这会根据我在处理应用程序网格中的鼠标位置点亮LED。然而,LED非常暗淡。如果我将传感器读数设置方式更改为:
int sensorReading = random(0, 1023);
然后LED会亮得更亮。由于LED都在数字输出引脚上,我认为它只会根据传感器读数值发出开/关信号,而与亮度无关。我错过了什么
以下是处理代码:
// 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);
}
//调光器-通过串行端口发送字节
//大卫·A·梅利斯
//
//此示例代码位于公共域中。
输入处理。串行。*;
串口;
无效设置(){
大小(256150);
println(“可用串行端口:”);
println(Serial.list());
//使用此列表中的第一个端口(编号0)。将其更改为
//选择与Arduino板对应的端口。最后
//参数(例如9600)是通信的速度
//必须对应于在中传递给Serial.begin()的值
//阿杜伊诺素描。
//端口=新的串行(这是Serial.list()[0],9600);
//如果您知道Arduino板使用的端口的名称,您可以
//可以像这样直接指定它。
端口=新的串行端口(此“COM6”,9600);
}
作废提款(){
//从黑色到白色绘制渐变
对于(int i=0;i<1024;i++){
中风(i);
线(i,0,i,150);
}
//将鼠标的当前X位置写入串行端口,如下所示
//一个字节。
端口写入(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);
}
}
}
//这些常量不会更改:
常数int analogPin=A0;//连接电位计的针脚。
常数int ledCount=10;//条形图中指示灯的数量。
int LED引脚[]={
2, 3, 4, 5, 6, 7,8,9,10,11 }; // 连接LED的管脚编号数组。
无效设置(){
Serial.begin(9600);
//在管脚阵列上循环并将其全部设置为输出:
对于(int-thisLed=0;thisLed
//读取最近的字节(从0到1023)
字节从0到255。它们(通常)表示ASCII字符集中的字符
如果您希望读取0到1023之间的数字,则可能一次传输一个字符(即字符1
后跟字符0
表示数字10)-在这种情况下,您必须解析这些数字,以将其转换为可按预期使用的数字
该功能可能就是您所需要的-问题:您的处理代码是,始终向您的Arduino发送串行数据:
在setup()之后直接调用draw()函数
执行其块中包含的代码行,直到
程序停止或调用noLoop()。调用draw()
自动且不应显式调用
这会导致您的Arduino草图频繁更新LED开/关状态,并且根据您读取数据的方式,这将导致LED脉冲非常快
解决方案:最简单的解决方案是在Arduino或处理草图中添加延迟。更好的解决方案是修改处理代码,使其仅在值更改时发送数据;尽管需要注意的是,由于鼠标值几乎是不断变化的,而且这些变化对于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函数的字节为0-255
其次,正如所指出的,您可能正在从处理应用程序发送一个字符串,如128
,然后使用其ASCII值设置强度。由于0
到9
的ASCII值在48-57范围内,因此强度相对较低。请注意,当一个包含多个字节的字符串出现时(例如128
),仅使用一个字节表示强度。所以你有两个选择:
- 发送一个真正的二进制字节,而不是字符串。这就是您所展示的调光器示例中所做的
- 发送一个字符串,然后将其转换为二进制表示形式。为此,您需要读取所有字符,直到某个分隔符(如CR或空格)收集它们为止
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);
}
#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;
}
}