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:
void setup() { //initialize serial communications at a 9600 baud rate Serial.begin(9600); } void loop() { //send 'Hello, world!' over the serial port Serial.println("Tu Arduino!"); //wait 100 milliseconds so we don't drive ourselves crazy delay(100); }
A to kod dla Processing:
import processing.serial.*; Serial myPort; // Create object from Serial class String val; // Data received from the serial port void setup() { String portName = Serial.list()[1]; //ustawiamy numer portu 1,2.... myPort = new Serial(this, portName, 9600); fill(50); text(portName, 10, 10, 70, 80); // Wyświetla numer portu } void draw() { if ( myPort.available() > 0) // If data is available, { val = myPort.readStringUntil('\n'); // read it and store it in val } 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:
char val; // 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() { 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 }
kod dla Processing:
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()[1]; //change the 0 to a 1 or 2 etc. to match your port myPort = new Serial(this, portName, 9600); } 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 } }
PROCESSING ⇔ARDUINO
Kod dla Arduino:
char val; // Data received from the serial port int ledPin = 13; // Set the pin to digital I/O 13 boolean ledState = LOW; //to toggle our LED void setup() { pinMode(ledPin, OUTPUT); // Set pin as OUTPUT //initialize serial communications at a 9600 baud rate Serial.begin(9600); establishContact(); // send a byte to establish contact until receiver responds } void loop() { if (Serial.available() > 0) { // If data is available to read, val = Serial.read(); // read it and store it in val if(val == '1') //if we get a 1 { ledState = !ledState; //flip the ledState digitalWrite(ledPin, ledState); } delay(100); } else { Serial.println("Hello, world!"); //send back a hello world delay(50); } } void establishContact() { while (Serial.available() <= 0) { Serial.println("A"); // send a capital A delay(300); } }
Kod dla Processing:
import processing.serial.*; //import the Serial library Serial myPort; //the Serial port object String val; // since we're doing serial handshaking, // we need to check if we've heard from the microcontroller boolean firstContact = false; void setup() { size(200, 200); //make our canvas 200 x 200 pixels big // initialize your serial port and set the baud rate to 9600 myPort = new Serial(this, Serial.list()[1], 9600); myPort.bufferUntil('\n'); } void draw() { //we can leave the draw method empty, //because all our programming happens in the serialEvent (see below) } void serialEvent( Serial myPort) { //put the incoming data into a String - //the '\n' is our end delimiter indicating the end of a complete packet val = myPort.readStringUntil('\n'); //make sure our data isn't empty before continuing if (val != null) { //trim whitespace and formatting characters (like carriage return) val = trim(val); println(val); //look for our 'A' string to start the handshake //if it's there, clear the buffer, and send a request for data if (firstContact == false) { if (val.equals("A")) { myPort.clear(); firstContact = true; myPort.write("A"); println("contact"); } } else { //if we've already established contact, keep getting and parsing data println(val); if (mousePressed == true) { //if we clicked in the window myPort.write('1'); //send a 1 println("1"); } // when you've parsed the data you have, ask for more: myPort.write("A"); } } }