我一直在研究Arduino入門工具包的例子,最近我遇到了一個問題,那就是使用電位計來改變電腦屏幕上的標誌顏色。最初出現徽標時,它的顏色與電位計設置的顏色一致,但隨着我移動電位計,顏色不會改變。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方法中,但它可能是完全不同的東西。
你可以將你的代碼格式化爲使用標籤更易讀嗎? – ladislas