2016-11-25 61 views
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 
} 
+0

你有没有做过任何调试?您是否使用过打印语句来确定您的Processing草图中正在运行哪部分代码?你确定你的代码是否运行在Arduino的一边?既然你在Arudino上调用'delay(10)',那么不会因为每个帧都发送来自Processing的信号而建立起来吗? –

+0

@NurImtiazulHaque Arduino和处理代码都有意义。唯一需要注意的是,您可以使用draw()方式发送数据,这意味着只要鼠标按下,LED应该亮起。如果您遇到LED问题,请暂时尝试上传* Examples> Basics> Blink *草图并确保仍然有效。有一个板载LED进行测试。如果附手动的LED,以销13个再次检查触点都OK和销(阴极/阳极)连接的正确方法 –

+0

我使用的上板上的LED(引脚13)的处理代码工作正常。它在鼠标按下和释放时同时显示0和1。和串口号。也是一样的。我在处理中打印以检查。但是数据没有被发送到串口。 :(它的工作一次。我不知道发生了什么:(@KevinWorkman –

回答

0

处理

你可以简单地说if(mousePressed)...,不需要说== true(暗示)

的Arduino

你是正确的尝试覆盖val你从那里读任何字符前检查if(Serial.available())。但是,无论此检查如何,loop()内的其余代码都正在执行。如果它已经在那里,没有理由反复将引脚写入LOW或HIGH。事实上,如果您只是在发现可供阅读的字符的循环中延迟,则您的响应速度会更快。

我建议您在Arduino代码中添加一些打印语句,以便查看您正在阅读的内容。

另外,难道你的硬件连接不当或你的LED灯被烧坏了吗?