1
我想將處理IDE數據發送到arduino。但是領導不行。它運行良好一次。但現在不工作:(因爲它是由處理髮現串口名稱是Arduino的完全相同處理IDE數據沒有正確發送到arduino
處理代碼:
import processing.serial.*;
Serial myPort; // Create object from Serial class
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
//send a 1
void draw() {
if (mousePressed == true)
{ //if we clicked in the window
myPort.write('1'); //send a 1
println("1");
} else
{ //otherwise
myPort.write('0'); //send a 0
}
}
的Arduino代碼:
char val='0'; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
//digitalWrite(ledPin, HIGH); // turn the LED on
if (Serial.available())
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '1')
{ // If 1 was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // otherwise turn it off
}
delay(10); // Wait 10 milliseconds for next reading
}
你有沒有做過任何調試?您是否使用過打印語句來確定您的Processing草圖中正在運行哪部分代碼?你確定你的代碼是否運行在Arduino的一邊?既然你在Arudino上調用'delay(10)',那麼不會因爲每個幀都發送來自Processing的信號而建立起來嗎? –
@NurImtiazulHaque Arduino和處理代碼都有意義。唯一需要注意的是,您可以使用draw()方式發送數據,這意味着只要鼠標按下,LED應該亮起。如果您遇到LED問題,請暫時嘗試上傳* Examples> Basics> Blink *草圖並確保仍然有效。有一個板載LED進行測試。如果附手動的LED,以銷13個再次檢查觸點都OK和銷(陰極/陽極)連接的正確方法 –
我使用的上板上的LED(引腳13)的處理代碼工作正常。它在鼠標按下和釋放時同時顯示0和1。和串口號。也是一樣的。我在處理中打印以檢查。但是數據沒有被髮送到串口。 :(它的工作一次。我不知道發生了什麼:(@KevinWorkman –