Physical Pixel

This example example uses the Arduino board to receive data from the computer. The board turns on an LED when it receives the character 'H', and turns off the LED when it receives the character 'L'.

The data can be sent from the Arduino Software (IDE) serial monitor, or another program like Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.

Hardware Required

  • Arduino Board

  • LED (optional)

  • 220 ohm resistor (optional)

Software Required

Circuit

Many Arduino boards have a built-in LED connected to pin 13; if your board has no built-in LED, attach an external LED to pin 13. The long leg, or anode, goes to pin 13 through a 220 resistor. The short leg, or cathode, goes to ground.

ExampleCircuit bb

Schematic

ExampleCircuit sch

Code


/*

  Physical Pixel

  An example of using the Arduino board to receive data from the computer. In

  this case, the Arduino boards turns on an LED when it receives the character

  'H', and turns off the LED when it receives the character 'L'.

  The data can be sent from the Arduino Serial Monitor, or another program like

  Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.

  The circuit:

  - LED connected from digital pin 13 to ground

  created 2006

  by David A. Mellis

  modified 30 Aug 2011

  by Tom Igoe and Scott Fitzgerald

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/PhysicalPixel

*/

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {

  // initialize serial communication:

  Serial.begin(9600);

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);
}

void loop() {

  // see if there's incoming serial data:

  if (Serial.available() > 0) {

    // read the oldest byte in the serial buffer:

    incomingByte = Serial.read();

    // if it's a capital H (ASCII 72), turn on the LED:

    if (incomingByte == 'H') {

      digitalWrite(ledPin, HIGH);

    }

    // if it's an L (ASCII 76) turn off the LED:

    if (incomingByte == 'L') {

      digitalWrite(ledPin, LOW);

    }

  }
}

/* Processing code for this example

  // Mouse over serial

  // Demonstrates how to send data to the Arduino I/O board, in order to turn ON

  // a light if the mouse is over a square and turn it off if the mouse is not.

  // created 2003-4

  // based on examples by Casey Reas and Hernando Barragan

  // modified 30 Aug 2011

  // by Tom Igoe

  // This example code is in the public domain.

  import processing.serial.*;

  float boxX;

  float boxY;

  int boxSize = 20;

  boolean mouseOverBox = false;

  Serial port;

  void setup() {

    size(200, 200);

    boxX = width / 2.0;

    boxY = height / 2.0;

    rectMode(RADIUS);

    // List all the available serial ports in the output pane.

    // You will need to choose the port that the Arduino board is connected to

    // from this list. The first port in the list is port #0 and the third port

    // in the list is port #2.

    // if using Processing 2.1 or later, use Serial.printArray()

    println(Serial.list());

    // Open the port that the Arduino board is connected to (in this case #0)

    // Make sure to open the port at the same speed Arduino is using (9600bps)

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

  }

  void draw() {

    background(0);

    // Test if the cursor is over the box

    if (mouseX > boxX - boxSize && mouseX < boxX + boxSize &&

        mouseY > boxY - boxSize && mouseY < boxY + boxSize) {

      mouseOverBox = true;

      // draw a line around the box and change its color:

      stroke(255);

      fill(153);

      // send an 'H' to indicate mouse is over square:

      port.write('H');

    }

    else {

      // return the box to its inactive state:

      stroke(153);

      fill(153);

      // send an 'L' to turn the LED off:

      port.write('L');

      mouseOverBox = false;

    }

    // Draw the box

    rect(boxX, boxY, boxSize, boxSize);

  }

*/

/* Max/MSP version 5 patch to run with this example:

----------begin_max5_patcher----------

1672.3oc2ZszaaiCD9ryuBBebQVCQRYao8xhf1cQCPVfBzh8RRQ.sDsM2HSZ

HQmlzh9eu7gjsjsEk7y0oWjiHoHm4aluYHGlueUmtiDuPy5B9Cv8fNc99Uc5

XZR2Pm726zcF4knDRlYXciDylQ4xtWa6SReQZZ+iSeMiEQR.ej8BM4A9C7OO

kkAlSjQSAYTdbFfvA27o2c6sfO.Doqd6NfXgDHmRUCKkolg4hT06BfbQJGH3

5Qd2e8d.QJIQSow5tzebZ7BFW.FIHow8.2JAQpVIIYByxo9KIMkSjL9D0BRT

sbGHZJIkDoZOSMuQT.8YZ5qpgGI3locF4IpQRzq2nDF+odZMIJkRjpEF44M3

A9nWAum7LKFbSOv+PSRXYOvmIhYiYpg.8A2LOUOxPyH+TjPJA+MS9sIzTRRr

QP9rXF31IBZAHpVHkHr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OTKf

3DLvnvRLDLpWeEOYXMfAZqfQ0.qsnlUdmA33t8CNJ7MZEb.u7fiZHLYzDkJp

R7CqEVLGN75U+1JXxFUY.xEEBcRCqhOEkz2bENEWnh4pbh0wY25EefbD6EmW

UA6Ip8wFLyuFXx+Wrp8m6iff1B86W7bqJO9+mx8er4E3.abCLrYdA16sBuHx

vKT6BlpIGQIhL55W7oicf3ayv3ixQCm4aQuY1HZUPQWY+cASx2WZ3f1fICuz

vj5R5ZbM1y8gXYN4dIXaYGq4NhQvS5MmcDADy+S.j8CQ78vk7Q7gtPDX3kFh

3NGaAsYBUAO.8N1U4WKycxbQdrWxJdXd10gNIO+hkUMmm.CZwknu7JbNUYUq

0sOsTsI1QudDtjw0t+xZ85wWZd80tMCiiMADNX4UzrcSeK23su87IANqmA7j

tiRzoXi2YRh67ldAk79gPmTe3YKuoY0qdEDV3X8xylCJMTN45JIakB7uY8XW

uVr3PO8wWwEoTW8lsfraX7ZqzZDDXCRqNkztHsGCYpIDDAOqxDpMVUMKcOrp

942acPvx2NPocMC1wQZ8glRn3myTykVaEUNLoEeJjVaAevA4EAZnsNgkeyO+

3rEZB7f0DTazDcQTNmdt8aACGi1QOWnMmd+.6YjMHH19OB5gKsMF877x8wsJ

hN97JSnSfLUXGUoj6ujWXd6Pk1SAC+Pkogm.tZ.1lX1qL.pe6PE11DPeMMZ2

.P0K+3peBt3NskC

description: ''
featuredImage: ''
-----------end_max5_patcher-----------

*/

Processing Code

Copy the Processing code from the code sample above. As you mouse over the center square, the LED on pin 13 should turn on and off. The Processing applet looks like this:

physicalPixel output

Mouse over the square to turn the LED on and off.

Max patch

The Max/MSP patch looks like the image below. Copy it from the code sample above and paste it into a new patch window.

max physicalPixel

See Also

  • serial.read()

  • serial.available()

  • if statement

  • ASCIITable - Demonstrates Arduino's advanced serial output functions.

  • Dimmer - Move the mouse to change the brightness of an LED.

  • Graph - Send data to the computer and graph it in Processing.

  • Midi - Send MIDI note messages serially.

  • MultiSerialMega - Use two of the serial ports available on the Arduino Mega.

  • ReadASCIIString - Parse a comma-separated string of integers to fade an LED.

  • SerialCallResponse - Send multiple variables using a call-and-response (handshaking) method.

  • SerialCallResponseASCII - Send multiple variables using a call-and-response (handshaking) method, and ASCII-encode the values before sending.

  • SerialEvent - Demonstrates the use of serialEvent().

  • VirtualColorMixer - Send multiple variables from Arduino to your computer and read them in Processing or Max/MSP.

Last revision 2015/07/29 by SM