Hide minor edits - Show changes to markup
Schematic click the image to enlarge
Esquemas Hacer click para agrandar la imagen
Analog inputs connected to analog input pin 0 and 1. Switch connected to digital I/O 2.
click on the image to enlarge
las entradas analógicas conectadas a lpines de entrada analógica 0 y 1. Interruptor conectado a la I/O digital 2.
Hacer click para agrandar la imagen
image developed using Fritzing. For more circuit examples, see the Fritzing project page
imagen desarrollada utilizando Fritzing. paramás ejemplos de circuitos, mirar página de proyecto Fritzing
Examples > Communication
An example of multi-byte communication from the Arduino board to the computer using a call-and-response (handshaking) method.
This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as single bytes, and waits for another response from the computer.
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 (see example below), etc.
Ejemplos > Comunicación
Un ejemplo de la comunicación multi-byte desde la placa de Arduino al ordenador utilizando un metodo de llamada-y-respuesta (handshaking).
Este programa envía un ASCII A (byte de valor 65) en el arranque y lo repite hasta que el serial responde desde el ordenador. Entonces envía tres valores del sensor como bits simples, y queda esperando otra respuesta del ordenador.
Puedes uilizar el monitor de serie de Arduino para ver los datos enviados, o puede ser leido por processing (ver código siguiente), flash, PD, Max/MSP (ver siguiente ejemplo), etc.
/* Serial Call and Response Language: Wiring/Arduino This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets some data in. Then it waits for a byte in the serial port, and sends three sensor values whenever it gets a byte in. Thanks to Greg Shakar and Scott Fitzgerald for the improvements The circuit: * potentiometers attached to analog inputs 0 and 1 * pushbutton attached to digital I/O 2 http://www.arduino.cc/en/Tutorial/SerialCallResponse
(:includeurl http://arduino.googlecode.com/svn/trunk/build/shared/examples/Communication/SerialCallResponse/SerialCallResponse.pde border=0:)
Created 26 Sept. 2005 by Tom Igoe Modified 14 April 2009 by Tom Igoe and Scott Fitzgerald */
int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // digital sensor int inByte = 0; // incoming serial byte
void setup() { // start serial port at 9600 bps: Serial.begin(9600); pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds }
void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); // read first analog input, divide by 4 to make the range 0-255: firstSensor = analogRead(0)/4; // delay 10ms to let the ADC recover: delay(10); // read second analog input, divide by 4 to make the range 0-255: secondSensor = analogRead(1)/4; // read switch, map it to 0 or 255L thirdSensor = map(digitalRead(2), 0, 1, 0, 255); // send sensor values: Serial.print(firstSensor, BYTE); Serial.print(secondSensor, BYTE); Serial.print(thirdSensor, BYTE); } }
void establishContact() { while (Serial.available() <= 0) { Serial.print('A', BYTE); // send a capital A delay(300); } }
image developed using Fritzing. For more circuit examples, see the Fritzing project page
(:div class=code :)
/* Serial Call and Response Language: Wiring/Arduino This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets some data in. Then it waits for a byte in the serial port, and sends three sensor values whenever it gets a byte in. Thanks to Greg Shakar and Scott Fitzgerald for the improvements The circuit: * potentiometers attached to analog inputs 0 and 1 * pushbutton attached to digital I/O 2 http://www.arduino.cc/en/Tutorial/SerialCallResponse
Created 26 Sept. 2005 by Tom Igoe Modified 14 April 2009 by Tom Igoe and Scott Fitzgerald */
int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // digital sensor int inByte = 0; // incoming serial byte
void setup() { // start serial port at 9600 bps: Serial.begin(9600); pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds }
void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); // read first analog input, divide by 4 to make the range 0-255: firstSensor = analogRead(0)/4; // delay 10ms to let the ADC recover: delay(10); // read second analog input, divide by 4 to make the range 0-255: secondSensor = analogRead(1)/4; // read switch, map it to 0 or 255L thirdSensor = map(digitalRead(2), 0, 1, 0, 255); // send sensor values: Serial.print(firstSensor, BYTE); Serial.print(secondSensor, BYTE); Serial.print(thirdSensor, BYTE); } }
void establishContact() { while (Serial.available() <= 0) { Serial.print('A', BYTE); // send a capital A delay(300); } }
(:divend:)
int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // digital sensor int inByte = 0; // incoming serial byte
void setup() {
// start serial port at 9600 bps: Serial.begin(9600); pinMode(2, INPUT); // digital sensor is on digital pin 2 establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A', BYTE); // send a capital A
delay(300);
}
}
@]
[@
As you change the value of the analog sensor, you'll get a graph something like this:
As you change the value of the analog sensor, you'll get a ball moving onscreen something like this. When you turn the switch off, the ball will disappear:
This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as single bytes, and waits for another response from the computer.
This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as single bytes, and waits for another response from the computer.
Examples > Communication
An example of multi-byte communication from the Arduino board to the computer using a call-and-response (handshaking) method.
This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as single bytes, and waits for another response from the computer.
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 (see example below), etc.
Analog inputs connected to analog input pin 0 and 1. Switch connected to digital I/O 2.
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A', BYTE); // send a capital A
delay(300);
}
}
/**
* Serial Call-Response
* by Tom Igoe.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman and Greg Shakar for the improvements.
*
* Note: This sketch assumes that the device on the other end of the serial
* port is going to send a single byte of value 65 (ASCII A) on startup.
* The sketch waits for that byte, then sends an ASCII A whenever
* it wants more data.
*/
import processing.serial.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
fgcolor = serialInArray[2];
// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + fgcolor);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
As you change the value of the analog sensor, you'll get a graph something like this:

The max patch looks like this. The text of the patch is linked behind the image.