开发者

Processing: How to write to the serial port?

boolean squareVisible = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;

void setup() {
    size(200, 200);
    noStroke();
    fill(255, 0, 0);
    rect(x, y, w, h);

    port = new Serial(this, 9600);
}

void draw() {
    background(255);
    if (squareVisible) {
        fill(40, 80, 90);
    } else {
        fill(255, 0, 0);
    }
    rect(x, y, w, h); // Draw a square
}


void mousePressed() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        // if mouse clicked inside square
        squareVisible = !squareVisible;  // toggle square visibility
        port.write("8");
    }
}

void mouseMoved() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
      开发者_如何学编程  port.write("2");                
    }
}

I'm a total noobie in processing. This is a simple toggle switch and I'm trying to write to the serial port when the switch is made. I'm trying to integrate this with the arduino but I can't seem to get it to read anything coming from the serial port. Is there a different way to write to the serial port with every switch I make, do I have something wrong? Thanks in advance...


I've spotted one problem: port = new Serial(this, 9600); which should be port = new Serial(this, Serial.list()[0], 9600);. You were missing an (important) argument from the Serial constructor. Always check for errors in the Processing Console (bellow your code), especially if the code doesn't work :)

I would starting with the SimpleWrite sample that comes with Processing so you understand how communication between Processing/Arduino works first, then move on and use the knowledge gained with your project.

The basic setup is this: In Processing you initialize the Serial instance in setup(), and in draw you send values using Serial's write() method. In Arduino, in setup() you initialize Serial (Serial.begin(yourBaudRate)) and in loop() you check if there's data available and read() values. It is VERY important to use the same baud rate in both Processing and Arduino, otherwise you won't be able to make out much of the data transfered.

Also, you're not stuck to sending Strings, you can also send ints, bytes, etc. If you want to display these, don't forget to add the type as the as the second argument of Serial.print() or Serial.println() (e.g. Serial.println(myByte,BYTE); or Serial.println(myInt,DEC));

I've setup a very basic sketch in Arduino to blink an LED once when your square is toggled and do nothing otherwise. Also, the incoming data is printed in the Serial Monitor: int incoming = 0;//this will store the value from Serial

void setup(){
  pinMode(13,OUTPUT);//add an LED on PIN 13 for kicks
  Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
}
void loop(){
  if(Serial.available() > 0){//look for Serial data 
    incoming = Serial.read();//read and store teh value
    Serial.print(incoming,DEC);//print it to the Serial monitor, change DEC to the type of variable you're using
    if(incoming == 1){//if it's a 1 blink once
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);    
      delay(500);
    } 
  }
}

And I've tweaked you're Processing sketch a bit:

boolean squareVisible = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;

void setup() {
    size(200, 200);
    noStroke();
    fill(255, 0, 0);
    rect(x, y, w, h);

    String portName = Serial.list()[0];
    port = new Serial(this, portName, 9600);
}

void draw() {
    background(255);
    if (squareVisible) {
        fill(40, 80, 90);
    } else {
        fill(255, 0, 0);
    }
    rect(x, y, w, h); // Draw a square
}


void mousePressed() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        // if mouse clicked inside square
        squareVisible = !squareVisible;  // toggle square visibility
        if(squareVisible) port.write(0);
        else              port.write(1);
    }
}
/*
void mouseMoved() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        port.write(2);                
    }
}*/

Goodluck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜