title: Processing: How to write to the serial port?
body: <pre class="lang-java prettyprint-override"><code>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 &gt; x) &amp;&amp; (mouseX &lt; x + w) &amp;&amp;
              (mouseY &gt; y) &amp;&amp; (mouseY &lt; y + h))) {
          // if mouse clicked inside square
          squareVisible = !squareVisible;  // toggle square visibility
          port.write("8");
      }
  }
  
  void mouseMoved() {
      if (((mouseX &gt; x) &amp;&amp; (mouseX &lt; x + w) &amp;&amp;
              (mouseY &gt; y) &amp;&amp; (mouseY &lt; y + h))) {
          port.write("2");                
      }
  }
  </code></pre>
  
  <p>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...</p>