{"id":1241,"date":"2018-02-21T15:30:46","date_gmt":"2018-02-21T14:30:46","guid":{"rendered":"http:\/\/yarogniew.net\/arduino\/?p=1241"},"modified":"2018-02-22T23:11:36","modified_gmt":"2018-02-22T22:11:36","slug":"komunikacja-arduino-processing","status":"publish","type":"post","link":"https:\/\/arduino.net.pl\/index.php\/komunikacja-arduino-processing\/","title":{"rendered":"Arduino dogaduje si\u0119 z Processing"},"content":{"rendered":"<p>Poniewa\u017c efektywna komunikacja to podstawa w \u017cyciu, postanowi\u0142em sprawi\u0107, by Arduino dogada\u0142o si\u0119 ze \u015brodowiskiem Processing. Od razu wiadomo by\u0142o, \u017ce oba \u015brodowiska s\u0105 podobne wi\u0119c nie spodziewa\u0142em si\u0119 k\u0142opot\u00f3w. I s\u0142usznie.<br \/>\nSkorzysta\u0142em tu ze \u015bwietnie napisanego artyku\u0142u: <a href=\"https:\/\/learn.sparkfun.com\/tutorials\/connecting-arduino-to-processing\">Connecting Arduino to Processing<\/a> ze strony SparkFun.<\/p>\n<h4>ARDUINO\u00a0\u21d2\u00a0PROCESSING<\/h4>\n<h6>Oto kod dla Arduino, banalnie prosty:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n\r\nvoid setup() \r\n{\r\n\/\/initialize serial communications at a 9600 baud rate\r\nSerial.begin(9600);\r\n\r\n}\r\n\r\nvoid loop()\r\n{\r\n\/\/send 'Hello, world!' over the serial port\r\nSerial.println(&quot;Tu Arduino!&quot;);\r\n\/\/wait 100 milliseconds so we don't drive ourselves crazy\r\ndelay(100);\r\n}\r\n<\/pre>\n<h6>A to kod dla Processing:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\nimport processing.serial.*;\r\n\r\nSerial myPort;  \/\/ Create object from Serial class\r\nString val;     \/\/ Data received from the serial port\r\n\r\n\r\nvoid setup()\r\n{\r\n\r\n  String portName = Serial.list()&#x5B;1]; \/\/ustawiamy numer portu 1,2....\r\n  myPort = new Serial(this, portName, 9600);\r\n\r\nfill(50);\r\ntext(portName, 10, 10, 70, 80);  \/\/ Wy\u015bwietla numer portu\r\n\r\n}\r\n\r\nvoid draw()\r\n{\r\n  if ( myPort.available() &amp;gt; 0) \/\/ If data is available,\r\n  {  \r\n  val = myPort.readStringUntil('\\n');         \/\/ read it and store it in val\r\n  }\r\n\r\nprintln(val); \/\/print it out in the console\r\n}\r\n<\/pre>\n<p>Jedyna rzecz o kt\u00f3rej trzeba pami\u0119ta\u0107 w przypadku Processing to ustawienie w\u0142a\u015bciwego numeru portu <em>Serial.list()[x]<\/em>. Na moim maku to jest numer 1. Dodatkowo doda\u0142em linijk\u0119\u00a0<em>text(portName, 10, 10, 70, 80);,\u00a0<\/em>kt\u00f3ra wy\u015bwietla nazw\u0119 portu, na kt\u00f3rym \u015brodowiska komunikuj\u0105 si\u0119.<\/p>\n<h4>PROCESSING \u21d2ARDUINO<\/h4>\n<h6>Kod dla Arduino:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n char val; \/\/ Data received from the serial port\r\n int ledPin = 13; \/\/ Set the pin to digital I\/O 13\r\n \r\n void setup() {\r\n   pinMode(ledPin, OUTPUT); \/\/ Set pin as OUTPUT\r\n   Serial.begin(9600); \/\/ Start serial communication at 9600 bps\r\n }\r\n\r\n\r\n void loop() {\r\n   if (Serial.available()) \r\n   { \/\/ If data is available to read,\r\n     val = Serial.read(); \/\/ read it and store it in val\r\n   }\r\n   if (val == '1') \r\n   { \/\/ If 1 was received\r\n     digitalWrite(ledPin, HIGH); \/\/ turn the LED on\r\n   } else {\r\n     digitalWrite(ledPin, LOW); \/\/ otherwise turn it off\r\n   }\r\n   delay(10); \/\/ Wait 10 milliseconds for next reading\r\n}\r\n\r\n<\/pre>\n<h6>kod dla Processing:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\nimport processing.serial.*;\r\n\r\nSerial myPort;  \/\/ Create object from Serial class\r\n\r\nvoid setup() \r\n{\r\n  size(200,200); \/\/make our canvas 200 x 200 pixels big\r\n  String portName = Serial.list()&#x5B;1]; \/\/change the 0 to a 1 or 2 etc. to match your port\r\n  myPort = new Serial(this, portName, 9600);\r\n}\r\nvoid draw() {\r\n  if (mousePressed == true) \r\n  {                           \/\/if we clicked in the window\r\n   myPort.write('1');         \/\/send a 1\r\n   println(&quot;1&quot;);   \r\n  } else \r\n  {                           \/\/otherwise\r\n  myPort.write('0');          \/\/send a 0\r\n  }   \r\n}\r\n\r\n<\/pre>\n<h4>PROCESSING \u21d4ARDUINO<\/h4>\n<h6>Kod dla Arduino:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n\r\n\r\nchar val; \/\/ Data received from the serial port\r\nint ledPin = 13; \/\/ Set the pin to digital I\/O 13\r\nboolean ledState = LOW; \/\/to toggle our LED\r\n\r\nvoid setup() \r\n{\r\n  pinMode(ledPin, OUTPUT); \/\/ Set pin as OUTPUT\r\n  \/\/initialize serial communications at a 9600 baud rate\r\n  Serial.begin(9600);\r\n  establishContact();  \/\/ send a byte to establish contact until receiver responds \r\n}\r\n\r\nvoid loop()\r\n{\r\n  if (Serial.available() &gt; 0) { \/\/ If data is available to read,\r\n    val = Serial.read(); \/\/ read it and store it in val\r\n\r\n    if(val == '1') \/\/if we get a 1\r\n    {\r\n       ledState = !ledState; \/\/flip the ledState\r\n       digitalWrite(ledPin, ledState); \r\n    }\r\n    delay(100);\r\n  } \r\n    else {\r\n    Serial.println(&quot;Hello, world!&quot;); \/\/send back a hello world\r\n    delay(50);\r\n    }\r\n}\r\n\r\nvoid establishContact() {\r\n  while (Serial.available() &lt;= 0) {\r\n  Serial.println(&quot;A&quot;);   \/\/ send a capital A\r\n  delay(300);\r\n  }\r\n}\r\n<\/pre>\n<h6>Kod dla Processing:<\/h6>\n<pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\r\n\r\n\r\nimport processing.serial.*; \/\/import the Serial library\r\n Serial myPort;  \/\/the Serial port object\r\n String val;\r\n\/\/ since we're doing serial handshaking, \r\n\/\/ we need to check if we've heard from the microcontroller\r\nboolean firstContact = false;\r\n\r\nvoid setup() {\r\n  size(200, 200); \/\/make our canvas 200 x 200 pixels big\r\n  \/\/  initialize your serial port and set the baud rate to 9600\r\n  myPort = new Serial(this, Serial.list()&#x5B;1], 9600);\r\n  myPort.bufferUntil('\\n'); \r\n}\r\n\r\n\r\nvoid draw() {\r\n  \/\/we can leave the draw method empty, \r\n  \/\/because all our programming happens in the serialEvent (see below)\r\n}\r\n\r\nvoid serialEvent( Serial myPort) {\r\n\/\/put the incoming data into a String - \r\n\/\/the '\\n' is our end delimiter indicating the end of a complete packet\r\nval = myPort.readStringUntil('\\n');\r\n\/\/make sure our data isn't empty before continuing\r\nif (val != null) {\r\n  \/\/trim whitespace and formatting characters (like carriage return)\r\n  val = trim(val);\r\n  println(val);\r\n\r\n  \/\/look for our 'A' string to start the handshake\r\n  \/\/if it's there, clear the buffer, and send a request for data\r\n  if (firstContact == false) {\r\n    if (val.equals(&quot;A&quot;)) {\r\n      myPort.clear();\r\n      firstContact = true;\r\n      myPort.write(&quot;A&quot;);\r\n      println(&quot;contact&quot;);\r\n    }\r\n  }\r\n  else { \/\/if we've already established contact, keep getting and parsing data\r\n    println(val);\r\n\r\n    if (mousePressed == true) \r\n    {                           \/\/if we clicked in the window\r\n      myPort.write('1');        \/\/send a 1\r\n      println(&quot;1&quot;);\r\n    }\r\n\r\n    \/\/ when you've parsed the data you have, ask for more:\r\n    myPort.write(&quot;A&quot;);\r\n    }\r\n  }\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Poniewa\u017c efektywna komunikacja to podstawa w \u017cyciu, postanowi\u0142em sprawi\u0107, by Arduino dogada\u0142o si\u0119 ze \u015brodowiskiem Processing. Od razu wiadomo by\u0142o, \u017ce oba \u015brodowiska s\u0105 podobne wi\u0119c nie spodziewa\u0142em si\u0119 k\u0142opot\u00f3w&#8230;.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,47],"tags":[],"class_list":["post-1241","post","type-post","status-publish","format-standard","hentry","category-arduino","category-processing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/1241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1241"}],"version-history":[{"count":8,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":1253,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions\/1253"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}