Tutorial.Dimmer History

Hide minor edits - Show changes to markup

February 02, 2014, at 11:37 AM by Scott Fitzgerald -
Changed lines 20-21 from:

An LED connected to pin 9. Use an appropriate resistor as needed. For most common LEDs, you can usually do without the resistor, as the current output of the digital I/O pins is limited.

to:

An LED connected to pin 9. Use an appropriate resistor as needed. For most common LEDs, a 220 or 330 ohm resistor will work.

May 02, 2012, at 04:10 PM by Scott Fitzgerald -
Changed line 38 from:
to:
November 16, 2011, at 04:13 AM by Scott Fitzgerald -
Changed line 38 from:
to:
September 30, 2011, at 03:13 AM by Scott Fitzgerald -
Changed line 38 from:
to:
September 23, 2010, at 10:34 PM by Christian Cerrito -
Changed lines 3-4 from:

Dimmer

to:

Dimmer

September 23, 2010, at 09:46 PM by Christian Cerrito -
Changed lines 33-36 from:

%

to:

%

September 23, 2010, at 09:44 PM by Christian Cerrito -
Changed lines 33-36 from:

%

to:

%

September 23, 2010, at 09:44 PM by Christian Cerrito -
Changed lines 24-25 from:
to:
Changed lines 33-35 from:

to:

%

September 17, 2010, at 09:00 PM by Tom Igoe -
Changed lines 49-52 from:

The Max/MSP patch in the code sample above looks like the image below. The text of the patch is linked behind the image. Copy it and paste it into a new patch window.

to:

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

September 17, 2010, at 08:53 PM by Tom Igoe -
Changed lines 45-88 from:

Run the following sketch in Processing. It will send bytes out the serial port to the Arduino to dim the LED.

 /* Processing code for this example */
  // Dimmer - sends bytes over a serial port
  // by David A. Mellis
  
  // This example code is in the public domain.

  import processing.serial.*;
  Serial port;
  
  void setup() {
  size(256, 150);
  
  println("Available serial ports:");
  println(Serial.list());
  
  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  
  
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
  }
  
  void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
  stroke(i);
  line(i, 0, i, 150);
  }
  
  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
  }
  */

to:

The Processing sketch in the code sample above will send bytes out the serial port to the Arduino to dim the LED.

Changed lines 49-50 from:

The Max/MSP patch looks like the image below. The text of the patch is linked behind the image. Copy it and paste it into a new patch window.

to:

The Max/MSP patch in the code sample above looks like the image below. The text of the patch is linked behind the image. Copy it and paste it into a new patch window.

September 17, 2010, at 08:51 PM by Tom Igoe -
Changed lines 5-6 from:

Demonstrates the sending data from the computer to the Arduino board, in this case to control the brightness of an LED. The data is sent in individual bytes, each of which ranges from 0 to 255. Arduino reads these bytes and uses them to set the brightness of the LED.

to:

This example shows how to send data from a personal computer to an Arduino board to control the brightness of an LED. The data is sent in individual bytes, each of which ranges in value from 0 to 255. Arduino reads these bytes and uses them to set the brightness of the LED.

Deleted line 18:
Changed lines 20-21 from:

An LED connected to pin 9. use appropriate resistor as needed. For most common LEDs, you can usually do without the resistor, as the current output of the digital I/O pins is limited.

to:

An LED connected to pin 9. Use an appropriate resistor as needed. For most common LEDs, you can usually do without the resistor, as the current output of the digital I/O pins is limited.

September 16, 2010, at 10:10 PM by Tom Igoe -
Changed line 40 from:
to:
August 27, 2010, at 07:23 AM by Christian Cerrito -
Changed lines 110-111 from:
to:
August 27, 2010, at 07:17 AM by Christian Cerrito -
Changed lines 110-111 from:
  • Serial Input? - How to take different actions based in characters received in the serial port.
to:
August 27, 2010, at 07:16 AM by Christian Cerrito -
Changed lines 105-106 from:
  • Serial Call and Response ASCII? - sending multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
to:
  • Graph - send data to the computer and graph it in Processing.
  • Physical Pixel - turn an LED on and off by sending data from Processing.
  • Virtual Color Mixer - send multiple variables from an Arduino to the computer and read them in Processing.
  • Serial Call Response - send multiple variables using a call and response (handshaking) method.
  • Serial Call and Response ASCII - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
  • Serial Input? - How to take different actions based in characters received in the serial port.
August 27, 2010, at 07:03 AM by Christian Cerrito -
Changed lines 96-98 from:

to:

See Also:

August 27, 2010, at 06:55 AM by Christian Cerrito -
Changed lines 16-19 from:
  • [http://www.cycling74.com/products/max5| Max/MSP version 5]]

to:
  • Max/MSP version 5

August 27, 2010, at 06:55 AM by Christian Cerrito -
Added lines 9-19:

Hardware Required

  • Arduino Board
  • LED
  • 220 ohm resistor

Software Required

  • Processing or
  • [http://www.cycling74.com/products/max5| Max/MSP version 5]]

August 27, 2010, at 06:33 AM by Christian Cerrito -
Changed line 29 from:
to:
February 23, 2010, at 08:09 PM by Tom Igoe -
Changed lines 29-30 from:
 /*
   Dimmer
to:
Changed lines 42-45 from:
  Demonstrates the sending data from the computer to the Arduino board,
  in this case to control the brightness of an LED.  The data is sent
  in individual bytes, each of which ranges from 0 to 255.  Arduino
  reads these bytes and uses them to set the brightness of the LED.
to:
  // This example code is in the public domain.

  import processing.serial.*;
  Serial port;
Changed lines 47-49 from:
  The circuit:
  LED attached from digital pin 9 to ground.
  Serial connection to Processing, Max/MSP, or another serial application
to:
  void setup() {
  size(256, 150);
Changed lines 50-53 from:
  created 2006
  by David A. Mellis
  modified 14 Apr 2009
  by Tom Igoe and Scott Fitzgerald
to:
  println("Available serial ports:");
  println(Serial.list());
Changed lines 53-75 from:
  http://www.arduino.cc/en/Tutorial/Dimmer
to:
  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  
  
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
  }
  
  void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
  stroke(i);
  line(i, 0, i, 150);
  }
  
  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
  }
Changed lines 78-145 from:
to:
August 27, 2009, at 08:55 PM by Tom Igoe -
Changed lines 16-18 from:
to:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:33 PM by Tom Igoe -
Changed line 76 from:
 /* Processing code for this example
to:
 /* Processing code for this example */
July 05, 2009, at 07:31 PM by Tom Igoe -
Changed lines 25-28 from:

[@ /*

  Dimmer
to:
Changed lines 47-50 from:
 Demonstrates the sending data from the computer to the Arduino board,
 in this case to control the brightness of an LED.  The data is sent
 in individual bytes, each of which ranges from 0 to 255.  Arduino
 reads these bytes and uses them to set the brightness of the LED.
to:
 const int ledPin = 9;      // the pin that the LED is attached to
Changed lines 49-51 from:
 The circuit:
 LED attached from digital pin 9 to ground.
 Serial connection to Processing, Max/MSP, or another serial application
to:
 void setup()
 {
   // initialize the serial communication:
   Serial.begin(9600);
   // initialize the ledPin as an output:
   pinMode(ledPin, OUTPUT);
 }
Changed lines 57-60 from:
 created 2006
 by David A. Mellis
 modified 14 Apr 2009
 by Tom Igoe and Scott Fitzgerald
to:
 void loop() {
   byte brightness;
Changed lines 60-86 from:
 http://www.arduino.cc/en/Tutorial/Dimmer
 */

const int ledPin = 9; // the pin that the LED is attached to

void setup() {

  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);

}

void loop() {

  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }

}

@]

to:
Changed lines 75-113 from:
// Dimmer - sends bytes over a serial port
// by David A. Mellis

import processing.serial.*;
Serial port;

void setup() {
  size(256, 150);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  

  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
}

to:
 /* Processing code for this example
  // Dimmer - sends bytes over a serial port
  // by David A. Mellis
  
  import processing.serial.*;
  Serial port;
  
  void setup() {
  size(256, 150);
  
  println("Available serial ports:");
  println(Serial.list());
  
  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  
  
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
  }
  
  void draw() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
  stroke(i);
  line(i, 0, i, 150);
  }
  
  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
  }
  */

June 25, 2009, at 11:20 PM by Tom Igoe -
Changed lines 47-48 from:
  1. define ledPin 9 // the pin that the LED is attached to
to:

const int ledPin = 9; // the pin that the LED is attached to

June 25, 2009, at 11:15 PM by Tom Igoe -
Changed lines 21-23 from:

to:

June 25, 2009, at 11:15 PM by Tom Igoe -
Changed lines 12-13 from:

[-click the image to enlarge-]

to:

click the image to enlarge

Changed lines 17-23 from:
to:

Schematic

click the image to enlarge

April 16, 2009, at 07:51 PM by Tom Igoe -
Changed lines 10-14 from:

An LED connected to pin 9 (use appropriate resistor as needed).

to:

An LED connected to pin 9. use appropriate resistor as needed. For most common LEDs, you can usually do without the resistor, as the current output of the digital I/O pins is limited.

[-click the image to enlarge-]

April 15, 2009, at 11:34 PM by Tom Igoe -
Changed lines 11-12 from:
to:
April 15, 2009, at 11:33 PM by Tom Igoe -
Changed lines 11-12 from:
to:
April 15, 2009, at 11:32 PM by Tom Igoe -
Changed lines 11-12 from:
to:
April 15, 2009, at 11:32 PM by Tom Igoe -
Changed lines 13-14 from:

An LED connected to pin 9 (with appropriate resistor).

to:

An LED connected to pin 9 (use appropriate resistor as needed).

April 15, 2009, at 11:31 PM by Tom Igoe -
Changed lines 11-12 from:
to:
April 15, 2009, at 11:31 PM by Tom Igoe -
Added lines 11-12:
April 15, 2009, at 11:02 PM by Tom Igoe -
Changed lines 105-106 from:

The Max/MSP patch looks like the image below. copy it and paste it into a new patch window.

to:

The Max/MSP patch looks like the image below. The text of the patch is linked behind the image. Copy it and paste it into a new patch window.

April 15, 2009, at 11:02 PM by Tom Igoe -
Changed lines 107-111 from:
to:

April 15, 2009, at 11:01 PM by Tom Igoe -
Changed lines 107-111 from:
to:
April 15, 2009, at 11:01 PM by Tom Igoe -
Changed lines 107-111 from:
to:
April 15, 2009, at 10:59 PM by Tom Igoe -
Changed lines 105-390 from:

The Max/MSP patch looks like the image below. The text of the patch is below, copy it and paste it into a new patch window.


{
	"boxes" : [ 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Dimmer\n\nThis patch sends a binary number from 0 to 255 out the serial port to an Arduino connected to the port.  It dims an LED attached to the Arduino.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
				"linecount" : 10,
				"patching_rect" : [ 209.0, 55.0, 344.0, 144.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-32",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "change the slider to alter the brightness of the LED",
				"linecount" : 3,
				"patching_rect" : [ 90.0, 235.0, 117.0, 48.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-7",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "number",
				"patching_rect" : [ 215.0, 385.0, 50.0, 19.0 ],
				"numoutlets" : 2,
				"fontsize" : 10.0,
				"outlettype" : [ "int", "bang" ],
				"id" : "obj-6",
				"fontname" : "Verdana",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "slider",
				"patching_rect" : [ 215.0, 235.0, 20.0, 140.0 ],
				"numoutlets" : 1,
				"outlettype" : [ "" ],
				"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
				"id" : "obj-1",
				"size" : 256.0,
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "select 0 1",
				"patching_rect" : [ 342.0, 305.0, 62.0, 20.0 ],
				"numoutlets" : 3,
				"fontsize" : 12.0,
				"outlettype" : [ "bang", "bang", "" ],
				"id" : "obj-30",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "click here to close the serial port",
				"patching_rect" : [ 390.0, 396.0, 206.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-26",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "click here to open the serial port",
				"patching_rect" : [ 415.0, 370.0, 206.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-27",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "close",
				"patching_rect" : [ 342.0, 396.0, 39.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-21",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "port a",
				"patching_rect" : [ 364.0, 370.0, 41.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-19",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Click here to get a list of serial ports",
				"patching_rect" : [ 435.0, 344.0, 207.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-2",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "toggle",
				"patching_rect" : [ 342.0, 268.0, 15.0, 15.0 ],
				"numoutlets" : 1,
				"outlettype" : [ "int" ],
				"id" : "obj-11",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "print",
				"patching_rect" : [ 384.0, 344.0, 36.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-13",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "serial a 9600",
				"patching_rect" : [ 259.0, 420.0, 84.0, 20.0 ],
				"numoutlets" : 2,
				"fontsize" : 12.0,
				"outlettype" : [ "int", "" ],
				"id" : "obj-14",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Click to start",
				"patching_rect" : [ 369.0, 268.0, 117.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-17",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "panel",
				"patching_rect" : [ 215.0, 235.0, 21.0, 139.0 ],
				"numoutlets" : 0,
				"mode" : 1,
				"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
				"id" : "obj-8",
				"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
				"numinlets" : 1,
				"angle" : 270.0
			}

		}
 ],
	"lines" : [ 		{
			"patchline" : 			{
				"source" : [ "obj-11", 0 ],
				"destination" : [ "obj-30", 0 ],
				"hidden" : 0,
				"midpoints" : [ 351.0, 296.0, 351.5, 296.0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-30", 1 ],
				"destination" : [ "obj-19", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-30", 0 ],
				"destination" : [ "obj-21", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-21", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 351.5, 416.5, 268.5, 416.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-19", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 373.5, 393.5, 268.5, 393.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-13", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 393.5, 365.5, 268.5, 365.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-1", 0 ],
				"destination" : [ "obj-6", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-6", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 224.5, 411.5, 268.5, 411.5 ]
			}

		}
 ]
}

to:

The Max/MSP patch looks like the image below. copy it and paste it into a new patch window.

April 15, 2009, at 10:56 PM by Tom Igoe -
Deleted lines 102-104:

Output

Changed lines 105-390 from:
to:

The Max/MSP patch looks like the image below. The text of the patch is below, copy it and paste it into a new patch window.


{
	"boxes" : [ 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Dimmer\n\nThis patch sends a binary number from 0 to 255 out the serial port to an Arduino connected to the port.  It dims an LED attached to the Arduino.\n\ncreated 2006\nby David A. Mellis\nmodified 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe",
				"linecount" : 10,
				"patching_rect" : [ 209.0, 55.0, 344.0, 144.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-32",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "change the slider to alter the brightness of the LED",
				"linecount" : 3,
				"patching_rect" : [ 90.0, 235.0, 117.0, 48.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-7",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "number",
				"patching_rect" : [ 215.0, 385.0, 50.0, 19.0 ],
				"numoutlets" : 2,
				"fontsize" : 10.0,
				"outlettype" : [ "int", "bang" ],
				"id" : "obj-6",
				"fontname" : "Verdana",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "slider",
				"patching_rect" : [ 215.0, 235.0, 20.0, 140.0 ],
				"numoutlets" : 1,
				"outlettype" : [ "" ],
				"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
				"id" : "obj-1",
				"size" : 256.0,
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "select 0 1",
				"patching_rect" : [ 342.0, 305.0, 62.0, 20.0 ],
				"numoutlets" : 3,
				"fontsize" : 12.0,
				"outlettype" : [ "bang", "bang", "" ],
				"id" : "obj-30",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "click here to close the serial port",
				"patching_rect" : [ 390.0, 396.0, 206.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-26",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "click here to open the serial port",
				"patching_rect" : [ 415.0, 370.0, 206.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-27",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "close",
				"patching_rect" : [ 342.0, 396.0, 39.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-21",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "port a",
				"patching_rect" : [ 364.0, 370.0, 41.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-19",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Click here to get a list of serial ports",
				"patching_rect" : [ 435.0, 344.0, 207.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-2",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "toggle",
				"patching_rect" : [ 342.0, 268.0, 15.0, 15.0 ],
				"numoutlets" : 1,
				"outlettype" : [ "int" ],
				"id" : "obj-11",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "message",
				"text" : "print",
				"patching_rect" : [ 384.0, 344.0, 36.0, 18.0 ],
				"numoutlets" : 1,
				"fontsize" : 12.0,
				"outlettype" : [ "" ],
				"id" : "obj-13",
				"fontname" : "Arial",
				"numinlets" : 2
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "newobj",
				"text" : "serial a 9600",
				"patching_rect" : [ 259.0, 420.0, 84.0, 20.0 ],
				"numoutlets" : 2,
				"fontsize" : 12.0,
				"outlettype" : [ "int", "" ],
				"id" : "obj-14",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "comment",
				"text" : "Click to start",
				"patching_rect" : [ 369.0, 268.0, 117.0, 20.0 ],
				"numoutlets" : 0,
				"fontsize" : 12.0,
				"id" : "obj-17",
				"fontname" : "Arial",
				"numinlets" : 1
			}

		}
, 		{
			"box" : 			{
				"maxclass" : "panel",
				"patching_rect" : [ 215.0, 235.0, 21.0, 139.0 ],
				"numoutlets" : 0,
				"mode" : 1,
				"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
				"id" : "obj-8",
				"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
				"numinlets" : 1,
				"angle" : 270.0
			}

		}
 ],
	"lines" : [ 		{
			"patchline" : 			{
				"source" : [ "obj-11", 0 ],
				"destination" : [ "obj-30", 0 ],
				"hidden" : 0,
				"midpoints" : [ 351.0, 296.0, 351.5, 296.0 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-30", 1 ],
				"destination" : [ "obj-19", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-30", 0 ],
				"destination" : [ "obj-21", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-21", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 351.5, 416.5, 268.5, 416.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-19", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 373.5, 393.5, 268.5, 393.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-13", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 393.5, 365.5, 268.5, 365.5 ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-1", 0 ],
				"destination" : [ "obj-6", 0 ],
				"hidden" : 0,
				"midpoints" : [  ]
			}

		}
, 		{
			"patchline" : 			{
				"source" : [ "obj-6", 0 ],
				"destination" : [ "obj-14", 0 ],
				"hidden" : 0,
				"midpoints" : [ 224.5, 411.5, 268.5, 411.5 ]
			}

		}
 ]
}

April 15, 2009, at 10:52 PM by Tom Igoe -
Added lines 7-8:

You can send bytes to the Arduino from any software that can access the computer serial port. Examples for Processing and Max/MSP version 5 are shown below.

Changed lines 16-17 from:

int ledPin = 9;

to:

/*

  Dimmer

 Demonstrates the sending data from the computer to the Arduino board,
 in this case to control the brightness of an LED.  The data is sent
 in individual bytes, each of which ranges from 0 to 255.  Arduino
 reads these bytes and uses them to set the brightness of the LED.

 The circuit:
 LED attached from digital pin 9 to ground.
 Serial connection to Processing, Max/MSP, or another serial application

 created 2006
 by David A. Mellis
 modified 14 Apr 2009
 by Tom Igoe and Scott Fitzgerald

 http://www.arduino.cc/en/Tutorial/Dimmer
 */

  1. define ledPin 9 // the pin that the LED is attached to
Changed line 40 from:
  // begin the serial communication
to:
  // initialize the serial communication:
Added line 42:
  // initialize the ledPin as an output:
Changed lines 46-50 from:

void loop() {

  byte val;

  // check if data has been sent from the computer
to:

void loop() {

  byte brightness;

  // check if data has been sent from the computer:
Changed lines 51-54 from:
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    // set the brightness of the LED
    analogWrite(ledPin, val);
to:
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
Added line 57:
Added lines 62-63:

Run the following sketch in Processing. It will send bytes out the serial port to the Arduino to dim the LED.

Deleted line 68:
Changed lines 71-72 from:

void setup() {

to:

void setup() {

Changed line 73 from:
to:
Changed line 76 from:
to:
Changed line 83 from:
to:
Changed lines 89-90 from:

void draw() {

to:

void draw() {

Changed line 95 from:
to:
Changed lines 100-108 from:

@]

to:

@]

Output

Max code

April 11, 2007, at 05:36 PM by David A. Mellis -
Added lines 1-78:

Examples > Communication

Dimmer

Demonstrates the sending data from the computer to the Arduino board, in this case to control the brightness of an LED. The data is sent in individual bytes, each of which ranges from 0 to 255. Arduino reads these bytes and uses them to set the brightness of the LED.

Circuit

An LED connected to pin 9 (with appropriate resistor).

Code

int ledPin = 9;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  byte val;

  // check if data has been sent from the computer
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    // set the brightness of the LED
    analogWrite(ledPin, val);
  }
}

Processing Code

// Dimmer - sends bytes over a serial port
// by David A. Mellis

import processing.serial.*;

Serial port;

void setup()
{
  size(256, 150);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  

  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw()
{
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
}

Share