【问题标题】:Arduino serial data not changing?Arduino串行数据没有改变?
【发布时间】:2024-01-14 19:08:01
【问题描述】:

我一直在研究 Arduino 入门套件示例,最近,我遇到了一个问题,其中一个电位器用于更改计算机屏幕上徽标的颜色。当徽标最初出现时,它是电位器设置的正确颜色,但是当我移动电位器时颜色不会改变。

我尝试仅将电位器的值输出到串行监视器,它们会正确更改,但是处理代码读取的值在输出到串行监视器时不会更改。

这是 Arduino 代码:

void setup() {
  // initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // read the value of A0, divide by 4 and 
  // send it as a byte over the serial connection
  Serial.write(analogRead(A0)/4);
  delay(1);
}

这里是处理代码:

 // import the serial library
 import processing.serial.*;

 // create an instance of the serial library
 Serial myPort;

 // create an instance of PImage
 PImage logo;

 // a variable to hold the background color
 int bgcolor = 0;

 void setup() {
 // set the color mode to Hue/Saturation/Brightness
 colorMode(HSB, 255);

 // load the Arduino logo into the PImage instance
 logo = loadImage("http://arduino.cc/en/pub/skins/arduinoWide/img/logo.png");

 // make the window the same size as the image
 size(logo.width, logo.height);

 // print a list of available serial ports to the 
 // Processing staus window
 println("Available serial ports:");
 println(Serial.list());

 // Tell the serial object the information it needs to communicate
 // with the Arduno. Change Serial.list()[0] to the correct 
 // port corresponding to your Arduino board.  The last
 // parameter (e.g. 9600) is the speed of the communication.  It
 // has to correspond to the value passed to Serial.begin() in your
 // Arduino sketch.  
 myPort = 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, "COM1", 9600);  

 }

 void draw() {
 background(255);
 // if there is information in the serial port
 if ( myPort.available() > 0) {
 // read the value and store it in a variable
 bgcolor = myPort.read();

 // print the value to the status window
 println(bgcolor); 
 }

 // Draw the background. the variable bgcolor
 // contains the Hue, determined by the value
 // from the serial port
 background(bgcolor, 255, 255);

 // draw the Arduino logo
 image(logo, 0, 0);
 }

所以我认为问题在于 Serial.write 或 Serial.read 方法,但可能完全不同。

【问题讨论】:

  • 您能否使用制表符格式化您的代码,使其更具可读性?

标签: serial-port arduino processing arduino-ide


【解决方案1】:

我有同样的问题;我只是在相同的原始Arduino代码中将延迟增加到50(即没有SerialEventparseInt,只需设置delay(50))。这解决了问题。

似乎取决于您的 PC,读取/解析串行缓冲区的处理速度可能会变慢,因此请告诉 Arduino 放慢速度!

【讨论】:

    【解决方案2】:

    我认为您的处理代码中需要一个名为 serialEvent 的东西。

    看起来像这样:

    void serialEvent(Serial myPort) {
       String inString = myPort.readStringUntil('\n');
    
       //do something with your string.
    }
    

    希望对你有帮助!

    【讨论】:

    • 我将 Processing draw 段更改为:void serialEvent(Serial myPort) { // read the value and store it in a variable bgcolor = Integer.parseInt(myPort.readStringUntil('\n')); // print the value to the status window println(bgcolor); } void draw() { // Draw the background. the variable bgcolor // contains the Hue, determined by the value // from the serial port background(bgcolor, 255, 255); // draw the Arduino logo image(logo, 0, 0); } 但出现 IncocationTargetException 错误。
    • 好的,我明白了,如果你想像你一样将它作为一个字符串接收,你必须在你的 Arduino 代码中输入Serial.println(analogRead(A0)/4);。您也可以尝试将更长的delay(); 像 500 开头。