【问题标题】:Nothing gets sent to Arduino from Processing没有任何东西从 Processing 发送到 Arduino
【发布时间】:2019-08-27 17:40:16
【问题描述】:

我有一个问题,我知道我的蓝牙模块 (HC-05) 已连接到处理草图,但处理草图没有发送任何内容,为什么?

没有错误消息,但信息永远不会被发送出去,因为我在 Arduino 中从未见过它,它会被打印到串行监视器上。

完整的处理代码:

import processing.serial.*;
import controlP5.*;

Serial myPort;
ControlP5 cp5;

int slider1 = 0;
int slider2 = 0;
int slider3 = 0;

void setup() {


  size(800, 800);
  cp5 = new ControlP5(this);
  PFont roboto = createFont("Roboto-Bold.ttf", 1, true);
  ControlFont font = new ControlFont(roboto, 28);
  Controller Aslider1 = cp5.addSlider("slider1")
    .setPosition(85, 100)
    .setCaptionLabel("Red")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider1").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider1").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

  Controller Aslider2 = cp5.addSlider("slider2")
    .setPosition(301, 100)
    .setCaptionLabel("Green")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider2").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider2").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

  Controller Aslider3 = cp5.addSlider("slider3")
    .setPosition(517, 100)
    .setCaptionLabel("Blue")
    .setRange(0, 255)
    .setWidth(191)
    .setHeight(50);
  cp5.getController("slider3").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  cp5.getController("slider3").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);


  myPort = new Serial(this, "COM5", 9600);
}


void draw() {
  background(255, 100, 100);

  fill(255);
  stroke(1);
  rectMode(CENTER);
  rect(width/2, 350, 200, 75);

  fill(0);
  textSize(32);
  text("Send", width/2 - textWidth("Send") / 2, 350 + 10);

  if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
    if (mousePressed) {
      fill(100);
      stroke(1);
      rectMode(CENTER);
      rect(width/2, 350, 200, 75);

      fill(0);
      textSize(32);
      text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
    } else {
      fill(170);
      stroke(1);
      rectMode(CENTER);
      rect(width/2, 350, 200, 75);

      fill(0);
      textSize(32);
      text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
    }
  }
}

void mouseReleased() {
  if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
    fill(100);
    stroke(1);
    rectMode(CENTER);
    rect(width/2, 350, 200, 75);

    fill(0);
    textSize(32);
    text("Send", width/2 - textWidth("Send") / 2, 350 + 10);

    println(hex(color(int(slider1), int(slider2), int(slider3))).toString().substring(2));
    myPort.write(hex(color(int(slider1), int(slider2), int(slider3))).toString().substring(2));
    myPort.clear();
  }
}

完整的 Arduino 代码:

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

const int PIN = 6;
const int NUMPIXELS = 30;

const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial mySerial (rxPin, txPin);

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

unsigned long currentMillis;
unsigned long loopMillis;
unsigned long waitMillis;
int interval = 100;
int waitInterval = 0;
int redBrightness = 0;
int greenBrightness = 0;
int blueBrightness = 0;
bool wait = false;
bool goToRed = true;
bool goToOrange = false;
bool goToYellow = false;
bool goToGreen = false;
bool goToAqua = false;
bool goToPurple = false;

String hexValue;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
  pixels.begin();
  pixels.show();
}

void loop() {

  if (mySerial.available() > 0) {
    hexValue = mySerial.read();
    Serial.println(hexValue);
    if (hexValue != "shiftN" || hexValue != "shiftY") {
      shiftMode = false;
      // value is a hex
      redBrightness = hexValue.substring(0, 2).toInt();
      greenBrightness = hexValue.substring(2, 4).toInt();
      greenBrightness = hexValue.substring(4, 6).toInt();
    } else {
      if (hexValue = "shiftY") {
        shiftMode = true;
      } else {
        shiftMode = false;
      }
    }
  }

  pixels.show();

  setColor(redBrightness, greenBrightness, blueBrightness);

}

void setColor(int red, int green, int blue) {
  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, red, green, blue);
  }
}

感谢您的帮助,干杯!

其他链接:

https://discourse.processing.org/t/nothing-gets-sent-to-arduino-from-processing/13654

https://arduino.stackexchange.com/questions/68233/nothing-gets-sent-to-arduino-from-processing

https://forum.arduino.cc/index.php?board=11.0

【问题讨论】:

    标签: string performance arduino hex processing


    【解决方案1】:

    假设myPort 是一个输出流,它可能会被缓冲。如果是这样,写完后尝试刷新它:

    myPort.flush();
    

    【讨论】:

    • 端口被缓冲是什么意思?
    • 表示flush函数不存在
    • myPort 的类是什么? hex() 来自哪个班级?如果是你的方法,请显示它的代码。
    • Hex 正在运行,参数是 int,myPort 的类是 Serial
    【解决方案2】:

    检查 hc-05 是 9600 波特还是 38400 波特

    【讨论】:

      【解决方案3】:

      对于想知道的每个人,这里是更新的代码:

      阿杜诺:

      #include <SoftwareSerial.h>
      #include <Adafruit_NeoPixel.h>
      
      const int pin = 6;
      const int numOfPixels = 30;
      
      int r = 0;
      int g = 0;
      int b = 0;
      
      Adafruit_NeoPixel pixels(numOfPixels, pin, NEO_GRB + NEO_KHZ800);
      
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);
        pixels.begin();
        pixels.show();
      
        pinMode(11, OUTPUT);
      
      }
      
      void loop() {
      
        pixels.show();
      
        // put your main code here, to run repeatedly:
        if (Serial.available() > 0) {
      
          Serial.println(String(Serial.read()));
      
          r = Serial.parseInt();
          g = Serial.parseInt();
          b = Serial.parseInt();
      
      
          setColor(r, g, b);
        }
      
      
          serialFlush();
      
      }
      
      void setColor(int red, int green, int blue) {
        pixels.fill(pixels.Color(red, green, blue), 0, numOfPixels);
        Serial.println("Red");
        Serial.println(red);
        Serial.println("Green");
        Serial.println(green);
        Serial.println("Blue");
        Serial.println(blue);
      }
      
      void serialFlush() {
        while (Serial.available() > 0) {
          char t = Serial.read();
        }
      }
      

      处理:

      import processing.serial.*;
      import controlP5.*;
      
      Serial myPort;
      ControlP5 cp5;
      
      int slider1 = 0;
      int slider2 = 0;
      int slider3 = 0;
      
      void setup() {
      
      
        size(800, 800);
        cp5 = new ControlP5(this);
        PFont roboto = createFont("Roboto-Bold.ttf", 1, true);
        ControlFont font = new ControlFont(roboto, 28);
        Controller Aslider1 = cp5.addSlider("slider1")
          .setPosition(85, 100)
          .setCaptionLabel("Red")
          .setRange(0, 255)
          .setWidth(191)
          .setHeight(50);
        cp5.getController("slider1").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
        cp5.getController("slider1").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
      
        Controller Aslider2 = cp5.addSlider("slider2")
          .setPosition(301, 100)
          .setCaptionLabel("Green")
          .setRange(0, 255)
          .setWidth(191)
          .setHeight(50);
        cp5.getController("slider2").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
        cp5.getController("slider2").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
      
        Controller Aslider3 = cp5.addSlider("slider3")
          .setPosition(517, 100)
          .setCaptionLabel("Blue")
          .setRange(0, 255)
          .setWidth(191)
          .setHeight(50);
        cp5.getController("slider3").getValueLabel().setFont(font).align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
        cp5.getController("slider3").getCaptionLabel().setFont(font).align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
      
      
        myPort = new Serial(this, "COM4", 9600);
      }
      
      
      void draw() {
        background(slider1, slider2, slider3);
      
        fill(255);
        stroke(1);
        rectMode(CENTER);
        rect(width/2, 350, 200, 75);
      
        fill(0);
        textSize(32);
        text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
      
        if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
          if (mousePressed) {
            fill(100);
            stroke(1);
            rectMode(CENTER);
            rect(width/2, 350, 200, 75);
      
            fill(0);
            textSize(32);
            text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
          } else {
            fill(170);
            stroke(1);
            rectMode(CENTER);
            rect(width/2, 350, 200, 75);
      
            fill(0);
            textSize(32);
            text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
          }
        }
      }
      
      void mouseReleased() {
        if (mouseX > width/2 - 100 && mouseX < width/2 + 100 && mouseY > 350 - 75/2 && mouseY < 350 + 75/2) {
          fill(100);
          stroke(1);
          rectMode(CENTER);
          rect(width/2, 350, 200, 75);
      
          fill(0);
          textSize(32);
          text("Send", width/2 - textWidth("Send") / 2, 350 + 10);
      
          myPort.write(str(slider1) + " " + str(slider2) + " " + str(slider3));
          println(str(slider1) + " " + str(slider2) + " " + str(slider3));
        }
      }
      

      然后看这个链接: https://forum.arduino.cc/index.php?topic=634061.0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-21
        相关资源
        最近更新 更多