我一直在使用Arduino Mega 2560和WS2812B可寻址LED灯进行项目。 Arduino IDE代码根据相应字符串中的字母来确定每个LED的颜色,例如“ ABACBABABCABAB”。用于点亮254个LED并按预期工作。
我遇到的问题是下一步,能够创建一个GUI,该GUI允许用户从100s的列表中选择一个模式,并允许Arduino拾取新的字符串来更改LED。模式。
我正在使用串行端口将此字符串发送到Arduino,当我在串行监视器中输入模式时,它的效果很好。但是,我在使用处理将字符串传递到串行端口时遇到麻烦。
该计划是使用处理来创建一个GUI,该GUI允许用户简单地选择模式并将其发送到串行端口。我被困在为什么它不发送并且无法进行故障排除,因为我无法在Arduino上打开串行监视器并同时发送来自Processing的字符串。
这是Arduino代码:
#include <FastLED.h>
#define NUM_LEDS 50
// Data pin that led data will be written out over
#define DATA_PIN 6
#define BRIGHTNESS 32
CRGB leds[NUM_LEDS];
const byte numChars = 200;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
String str = receivedChars;
for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
// Turn our current led on to white, then show the leds
if (str.charAt(whiteLed) == 'A') {
leds[whiteLed] = CRGB::Green;
}
if (str.charAt(whiteLed) == 'B') {
leds[whiteLed] = CRGB::Blue;
}
if (str.charAt(whiteLed) == 'C') {
leds[whiteLed] = CRGB::Yellow;
}
if (str.charAt(whiteLed) == 'D') {
leds[whiteLed] = CRGB::Pink;
}
if (str.charAt(whiteLed) == 'E') {
leds[whiteLed] = CRGB::Orange;
}
if (str.charAt(whiteLed) == 'F') {
leds[whiteLed] = CRGB::White;
}
if (str.charAt(whiteLed) == 'G') {
leds[whiteLed] = CRGB::Red;
}
if (str.charAt(whiteLed) == 'H') {
leds[whiteLed] = CRGB::Brown;
}
if (str.charAt(whiteLed) == 'X') {
leds[whiteLed] = CRGB::Black;
}
}
// Show the leds (only one of which is set to white, from above)
FastLED.show();
newData = false;
}
}
这是简单的处理代码
import processing.serial.*; //use the Serial library
Serial myPort; // Create object from Serial class
void setup() {
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600); //initialize the serial port object
}
void draw() { //this is like the 'loop' section of code on Arduino
String pattern = "<AAABBBABCCCAAABCCCCBAAAABCCCCCAAAABBB>";
myPort.write(pattern);
}
非常感谢您的帮助,对于所有这些问题,我都是陌生的,因此对任何错误深表歉意。
D
首先检查是否可以通过此simple tutorial进行通讯。然后将您的代码与教程代码进行比较。