将字符串从处理发送到Arduino

将字符串从处理发送到Arduino,arduino,serial-port,processing,led,Arduino,Serial Port,Processing,Led,我一直在用我的Arduino Mega 2560和WS2812B可寻址LED灯进行一个项目。Arduino IDE代码根据相应字符串(如ABAB)中的字母确定每个LED的颜色。这用于点亮254个LED,并按预期工作 我遇到的麻烦是,下一步要创建一个GUI,允许用户从100个字符列表中选择一个模式,并允许Arduino使用新的字符串来更改LED模式 我正在使用串行端口将此字符串发送到Arduino,当我在串行监视器中输入模式时,效果非常好。但是,我在使用Processing将此字符串传输到串行端口

我一直在用我的Arduino Mega 2560和WS2812B可寻址LED灯进行一个项目。Arduino IDE代码根据相应字符串(如ABAB)中的字母确定每个LED的颜色。这用于点亮254个LED,并按预期工作

我遇到的麻烦是,下一步要创建一个GUI,允许用户从100个字符列表中选择一个模式,并允许Arduino使用新的字符串来更改LED模式

我正在使用串行端口将此字符串发送到Arduino,当我在串行监视器中输入模式时,效果非常好。但是,我在使用Processing将此字符串传输到串行端口时遇到问题

计划是使用processing创建一个GUI,允许用户简单地选择模式并将字符串发送到串行端口。我被困在为什么这不发送,也无法排除故障,因为我无法打开Arduino上的串行监视器,同时从处理中发送字符串

以下是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

首先检查您是否可以通过此功能获得通信。 然后将您的代码与教程代码进行比较