Hide minor edits - Show changes to markup
The max patch looks like this. The text of the patch is linked behind the image.
El resultado de un programa Max se vería como esto. El texto del programa está enlazado en la imagen.
// trim off any whitespace:
// elimina cualquier espacio en blanco:
// convert to an int and map to the screen height:
// convierte a entero y mapea al alto de la pantalla:
// draw the line:
// traza la linea:
// at the edge of the screen, go back to the beginning:
// en el borde de la pantalla, vuelve al principio:
// increment the horizontal position:
// incrementa la posición horizontal:
As you change the value of the analog sensor, you'll get a graph something like this:
Si cambia el valor del sensor analógico, obtendrás un gráfico del estilo a este:
// This program takes ASCII-encoded strings // from the serial port at 9600 baud and graphs them. It expects values in the // range 0 to 1023, followed by a newline, or newline and carriage return
// Este programa recoge cadenas codificadas en ASCII // desde el puerto serie a 9600 baudios y hace gráficos con ellos. Espera valores del // rango de 0 a 1023, seguidos por un salto de línea, o de salto de línea y retorno de carro
Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph
Serial myPort; // El puerto serie int xPos = 1; // posición horizontal del gráfico
// set the window size:
// establece el tamaño de la ventana:
// List all the available serial ports
// lista los puertos serie disponibles
// I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using.
// Sé que el primer puerto serie de la lista en mi mac // es siempre mi Arduino, así que abro Serial.list()[0]. // Abre el puerto que estás usando.
// don't generate a serialEvent() unless you get a newline character:
// no genera un serialEvent() a menos que reciba un caracter de salto de linea:
// set inital background:
// Establece el fondo inicial:
// everything happens in the serialEvent()
// todo se hace en serialEvent()
// get the ASCII string:
// recoge la cadena ASCII:
Examples > Communication
A simple example of communication from the Arduino board to the computer: the value of an analog input is printed. We call this "serial" communication because the connection appears to both the Arduino and the computer as an old-fashioned serial port, even though it may actually use a USB cable.
You can use the Arduino serial monitor to view the sent data, or it can be read by Processing (see code below), Flash, PD, Max/MSP, etc.
An analog input connected to analog input pin 0.
click the image to enlarge
Ejemplos > Comunicaciones
Un ejemplo sencillo de comunicaciones desde la placa Arduino al ordenador: el valor de una entrada analógica es visualizado. Llamomos a esto comunicación "serie" porque la conexión tanto en Arduino como en el ordenador es un tradicional y modernizado puerto serie, aunque en realidad actualmente uses un cable USB.
Puedes utilizar el monitor serie de Arduino para ver los datos enviados, o pueden ser leidos mediante Processing (ver código mas abajo), Flash, PD, Max/MSP, etc.
Una entrada analógica conectada al pin 0 analógico.
haz click en la imagen para ampliarla
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic
click the image to enlarge
imagen desarrollada utilizando Fritzing. Para más circuitos de ejemplo, visita la página del proyecto Fritzing
Esquema
haz click en la imagen para ampliarla
/* Graph A simple example of communication from the Arduino board to the computer: the value of analog input 0 is sent out the serial port. We call this "serial" communication because the connection appears to both the Arduino and the computer as a serial port, even though it may actually use a USB cable. Bytes are sent one after another (serially) from the Arduino to the computer. You can use the Arduino serial monitor to view the sent data, or it can be read by Processing, PD, Max/MSP, or any other program capable of reading data from a serial port. The Processing code below graphs the data received so you can see the value of the analog input changing over time. The circuit: Any analog input sensor is attached to analog in pin 0. created 2006 by David A. Mellis modified 14 Apr 2009 by Tom Igoe and Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Graph */
(:includeurl http://arduino.googlecode.com/svn/trunk/build/shared/examples/Communication/Graph/Graph.pde border=0:)
void setup() { // initialize the serial communication: Serial.begin(9600); }
void loop() { // send the value of analog input 0: Serial.println(analogRead(0)); // wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(10); }
// This example code is in the public domain.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
image developed using Fritzing. For more circuit examples, see the Fritzing project page
image developed using Fritzing. For more circuit examples, see the Fritzing project page
http://www.arduino.cc/en/Tutorial/Graph
(:div class=code :)
/* Graph A simple example of communication from the Arduino board to the computer: the value of analog input 0 is sent out the serial port. We call this "serial" communication because the connection appears to both the Arduino and the computer as a serial port, even though it may actually use a USB cable. Bytes are sent one after another (serially) from the Arduino to the computer. You can use the Arduino serial monitor to view the sent data, or it can be read by Processing, PD, Max/MSP, or any other program capable of reading data from a serial port. The Processing code below graphs the data received so you can see the value of the analog input changing over time. The circuit: Any analog input sensor is attached to analog in pin 0. http://www.arduino.cc/en/Tutorial/Graph created 2006 by David A. Mellis modified 14 Apr 2009 by Tom Igoe and Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Graph */
void setup() { // initialize the serial communication: Serial.begin(9600); }
void loop() { // send the value of analog input 0: Serial.println(analogRead(0)); // wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(10); }
(:divend:)
void setup() { // initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0: Serial.println(analogRead(0)); // wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(10);
} @]
[@
click the image to enlarge
click the image to enlarge
void setup() {
void setup() { // initialize the serial communication:
void loop() {
void loop() {
// send the value of analog input 0:
delay(20);
// wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(10);
// Graph // by David A. Mellis // // Demonstrates reading data from the Arduino board by graphing the // values received. // // based on Analog In // by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
// Graphing sketch
// This program takes ASCII-encoded strings // from the serial port at 9600 baud and graphs them. It expects values in the // range 0 to 1023, followed by a newline, or newline and carriage return
// Created 20 Apr 2005 // Updated 18 Jan 2008 // by Tom Igoe
Serial port; String buff = ""; int NEWLINE = 10;
// Store the last 64 values received so we can graph them. int[] values = new int[64];
void setup() {
size(512, 256);
println("Available serial ports:");
Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size: size(400, 300);
// List all the available serial ports
// 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);
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
void draw() {
background(53); stroke(255);
// Graph the stored values by drawing a lines between them.
for (int i = 0; i < 63; i++)
line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
while (port.available() > 0)
serialEvent(port.read());
void draw () {
// everything happens in the serialEvent()
void serialEvent(int serial) {
if (serial != NEWLINE) {
// Store all the characters on the line.
buff += char(serial);
} else {
// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff = buff.substring(0, buff.length()-1);
// Parse the String into an integer. We divide by 4 because
// analog inputs go from 0 to 1023 while colors in Processing
// only go from 0 to 255.
int val = Integer.parseInt(buff)/4;
// Clear the value of "buff"
buff = "";
// Shift over the existing values to make room for the new one.
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];
// Add the received value to the array.
values[63] = val;
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
@]
Examples > Communication
A simple example of communication from the Arduino board to the computer: the value of an analog input is printed. We call this "serial" communication because the connection appears to both the Arduino and the computer as an old-fashioned serial port, even though it may actually use a USB cable.
You can use the Arduino serial monitor to view the sent data, or it can be read by Processing (see code below), Flash, PD, Max/MSP, etc.
An analog input connected to analog input pin 0.
Serial.print(analogRead(0) / 4, BYTE);
Serial.println(analogRead(0));
/*
@]
[@ // Graph // by David A. Mellis // // Demonstrates reading data from the Arduino board by graphing the // values received. // // based on Analog In // by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
String buff = ""; int NEWLINE = 10;
// Store the last 64 values received so we can graph them.
// Print a list in case COM1 doesn't work out //println("Available serial ports:"); //printarr(PSerial.list());
println("Available serial ports:");
println(Serial.list());
//port = new Serial(this, "COM1", 9600);
// Uses the first available port
// 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.
// 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);
// Graph the stored values by drawing a lines between them.
println(serial);
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];
if (serial != NEWLINE) {
// Store all the characters on the line.
buff += char(serial);
} else {
// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff = buff.substring(0, buff.length()-1);
values[63] = serial;
// Parse the String into an integer. We divide by 4 because
// analog inputs go from 0 to 1023 while colors in Processing
// only go from 0 to 255.
int val = Integer.parseInt(buff)/4;
// Clear the value of "buff"
buff = "";
// Shift over the existing values to make room for the new one.
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];
// Add the received value to the array.
values[63] = val;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(analogRead(0) / 4, BYTE);
delay(20);
}
/*
import processing.serial.*;
Serial port;
int[] values = new int[64];
void setup()
{
size(512, 256);
// Print a list in case COM1 doesn't work out
//println("Available serial ports:");
//printarr(PSerial.list());
//port = new Serial(this, "COM1", 9600);
// Uses the first available port
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(53);
stroke(255);
for (int i = 0; i < 63; i++)
line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
while (port.available() > 0)
serialEvent(port.read());
}
void serialEvent(int serial)
{
println(serial);
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];
values[63] = serial;
}
*/