// wiring_out // David A. Mellis // 22 May 2005 // A example using the Wiring library for output. // For more information, see: http://dam.mellis.org/w4p/ // The position of the mouse controls the outputs of pins on the // Wiring board. Move the mouse cursor into the upper half of the // window to write 5 volts (HIGH) to digital pin 48, which controls // the orange LED. Move the mouse cursor to the bottom half of the // window to turn off the LED. // Moving the mouse cursor from left to right controls the analog // out (PWM) value written to pin 0. When the cursor is at the left // side of the window, a value of 0 or 0 volts (always off) is written // to the pin. When the cursor is at the right side of the window, // a value of 255 or 5 volts (always on) is written to the pin. When // the mouse cursor is in the middle of the window, a value of 128 is // written to the pin, which causes it alternate rapidly between on // and off, spending an equal time at each, for an average output of // 2.5 volts. If the mouse is a quarter of the way from the left of // the window, a value of 64 is written, which causes the pin to // be on (5 volts) one quarter of the time, and off (0 volts), three // quarters of the time, for an average of 1.25 volts. To see this, // follow the instructions at http://www.potemkin.org/cms/Wiring/SimpleInterfaces // but hook the LED up to analog out (PWM) pin 0. Or use an oscilliscope. // This example doesn't use the serial library directly, but this import // statement must be included or Processing won't find the native serial // library. import processing.serial.*; import org.mellis.wiring.*; // declare a global Wiring object Wiring wiring; void setup() { size(255, 255); // initialize the W4P library wiring = new Wiring(this, "/dev/tty.usbserial-1B1"); // use digital pin 48 as an output wiring.pinMode(48, Wiring.OUTPUT); } void draw() { background(200); noStroke(); if (mouseY < height / 2) { // if the mouse is in the upper half of the window // make that half of the window white fill(255); rect(0, 0, width, height / 2); // and turn on the orange LED by outputting 5 volts on pin 48 wiring.digitalWrite(48, Wiring.HIGH); } else { // otherwise, make the bottom half of the window black fill(0); rect(0, height / 2, width, height); // and turn off the LED by outputting 0 volts on pin 48 wiring.digitalWrite(48, Wiring.LOW); } // draw the axis line across the middle of the window stroke(255, 0, 0); line(0, height / 2, width, height / 2); // draw a rectangle at the horizontal mouse location fill(0); rect(mouseX, height / 2 - 5, 10, 10); // and output a value on analog out (PWM) pin 0 equal to // the mouse's X coordinate (from 0 to 255) wiring.analogWrite(0, mouseX); }