Ponieważ efektywna komunikacja to podstawa w życiu, postanowiłem sprawić, by Arduino dogadało się ze środowiskiem Processing. Od razu wiadomo było, że oba środowiska są podobne więc nie spodziewałem się kłopotów. I słusznie.
Skorzystałem tu ze świetnie napisanego artykułu: Connecting Arduino to Processing ze strony SparkFun.
ARDUINO ⇒ PROCESSING
Oto kod dla Arduino, banalnie prosty:
03 | //initialize serial communications at a 9600 baud rate |
10 | //send 'Hello, world!' over the serial port |
11 | Serial.println("Tu Arduino!"); |
12 | //wait 100 milliseconds so we don't drive ourselves crazy |
A to kod dla Processing:
01 | import processing.serial.*; |
03 | Serial myPort; // Create object from Serial class |
04 | String val; // Data received from the serial port |
10 | String portName = Serial.list()[1]; //ustawiamy numer portu 1,2.... |
11 | myPort = new Serial(this, portName, 9600); |
14 | text(portName, 10, 10, 70, 80); // Wyświetla numer portu |
20 | if ( myPort.available() > 0) // If data is available, |
22 | val = myPort.readStringUntil('\n'); // read it and store it in val |
25 | println(val); //print it out in the console |
Jedyna rzecz o której trzeba pamiętać w przypadku Processing to ustawienie właściwego numeru portu Serial.list()[x]. Na moim maku to jest numer 1. Dodatkowo dodałem linijkę text(portName, 10, 10, 70, 80);, która wyświetla nazwę portu, na którym środowiska komunikują się.
PROCESSING ⇒ARDUINO
Kod dla Arduino:
01 | char val; // Data received from the serial port |
02 | int ledPin = 13; // Set the pin to digital I/O 13 |
05 | pinMode(ledPin, OUTPUT); // Set pin as OUTPUT |
06 | Serial.begin(9600); // Start serial communication at 9600 bps |
11 | if (Serial.available()) |
12 | { // If data is available to read, |
13 | val = Serial.read(); // read it and store it in val |
16 | { // If 1 was received |
17 | digitalWrite(ledPin, HIGH); // turn the LED on |
19 | digitalWrite(ledPin, LOW); // otherwise turn it off |
21 | delay(10); // Wait 10 milliseconds for next reading |
kod dla Processing:
01 | import processing.serial.*; |
03 | Serial myPort; // Create object from Serial class |
07 | size(200,200); //make our canvas 200 x 200 pixels big |
08 | String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port |
09 | myPort = new Serial(this, portName, 9600); |
12 | if (mousePressed == true) |
13 | { //if we clicked in the window |
14 | myPort.write('1'); //send a 1 |
18 | myPort.write('0'); //send a 0 |
PROCESSING ⇔ARDUINO
Kod dla Arduino:
01 | char val; // Data received from the serial port |
02 | int ledPin = 13; // Set the pin to digital I/O 13 |
03 | boolean ledState = LOW; //to toggle our LED |
07 | pinMode(ledPin, OUTPUT); // Set pin as OUTPUT |
08 | //initialize serial communications at a 9600 baud rate |
10 | establishContact(); // send a byte to establish contact until receiver responds |
15 | if (Serial.available() > 0) { // If data is available to read, |
16 | val = Serial.read(); // read it and store it in val |
18 | if(val == '1') //if we get a 1 |
20 | ledState = !ledState; //flip the ledState |
21 | digitalWrite(ledPin, ledState); |
26 | Serial.println("Hello, world!"); //send back a hello world |
31 | void establishContact() { |
32 | while (Serial.available() <= 0) { |
33 | Serial.println("A"); // send a capital A |
Kod dla Processing:
01 | import processing.serial.*; //import the Serial library |
02 | Serial myPort; //the Serial port object |
04 | // since we're doing serial handshaking, |
05 | // we need to check if we've heard from the microcontroller |
06 | boolean firstContact = false; |
09 | size(200, 200); //make our canvas 200 x 200 pixels big |
10 | // initialize your serial port and set the baud rate to 9600 |
11 | myPort = new Serial(this, Serial.list()[1], 9600); |
12 | myPort.bufferUntil('\n'); |
17 | //we can leave the draw method empty, |
18 | //because all our programming happens in the serialEvent (see below) |
21 | void serialEvent( Serial myPort) { |
22 | //put the incoming data into a String - |
23 | //the '\n' is our end delimiter indicating the end of a complete packet |
24 | val = myPort.readStringUntil('\n'); |
25 | //make sure our data isn't empty before continuing |
27 | //trim whitespace and formatting characters (like carriage return) |
31 | //look for our 'A' string to start the handshake |
32 | //if it's there, clear the buffer, and send a request for data |
33 | if (firstContact == false) { |
34 | if (val.equals("A")) { |
41 | else { //if we've already established contact, keep getting and parsing data |
44 | if (mousePressed == true) |
45 | { //if we clicked in the window |
46 | myPort.write('1'); //send a 1 |
50 | // when you've parsed the data you have, ask for more: |