Tutorial.SerialCallResponse History

Hide minor edits - Show changes to output

May 02, 2012, at 04:09 PM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/04.Communication/SerialCallResponse/SerialCallResponse.ino lang=arduino tabwidth=4:)
April 24, 2012, at 09:54 PM by Scott Fitzgerald -
Changed lines 56-57 from:
%width=600px%[[Attach:max-serialCallResponse.png|Attach:max-serialCallResponse.png]]
to:
%width=600px%[[Attach:Max5SerialCallResponse.png|Attach:Max5SerialCallResponse.png]]
Deleted line 71:
April 24, 2012, at 09:31 PM by Scott Fitzgerald -
Changed lines 56-57 from:
%width=600px%[[Attach:Max5SerialCallResponse.png|Attach:Max5SerialCallResponse.png]]
to:
%width=600px%[[Attach:max-serialCallResponse.png|Attach:max-serialCallResponse.png]]
Added line 72:
April 24, 2012, at 07:25 PM by Scott Fitzgerald - updating max5 image
Changed lines 56-57 from:
%width=600px%[[Attach:max-serialCallResponse.png|Attach:max-serialCallResponse.png]]
to:
%width=600px%[[Attach:Max5SerialCallResponse.png|Attach:Max5SerialCallResponse.png]]
Deleted line 71:
November 16, 2011, at 04:14 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:14 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.ino lang=arduino tabwidth=4:)
September 23, 2010, at 10:26 PM by Christian Cerrito -
Changed lines 3-4 from:
!!Serial Call and Response (handshaking)
to:
!!!Serial Call and Response (handshaking)
September 23, 2010, at 10:24 PM by Christian Cerrito -
Changed lines 36-37 from:
%width=400px%[[Attach:SerialCallResponse_schem.png |Attach:SerialCallResponse_schem.png]]
to:
%width=400px%[[Attach:SerialCallResponse_sch.png |Attach:SerialCallResponse_sch.png]]
September 17, 2010, at 09:19 PM by Tom Igoe -
Changed lines 5-8 from:
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.
to:
This example demonstrates multi-byte communication from the Arduino board to the computer using a call-and-response (handshaking) method.

This sketch 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.
Changed lines 25-26 from:
Analog inputs connected to analog input pin 0 and 1 with 10K ohm resistors used as voltage dividers. Switch/button connected to digital I/O pin 2 with a 10K ohm resistor as a reference to ground.
to:
Connect analog sensors to analog input pin 0 and 1 with 10K ohm resistors used as voltage dividers. Connect a pushbutton or switch to digital I/O pin 2 with a 10Kohm resistor as a reference to ground.
Deleted lines 37-39:


Changed lines 48-144 from:
[@
/**
* 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.
*
*This example code is in the public domain
*/


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;
}
}
}

@]

!!! Output

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:
to:
Copy the Processing sketch from the code sample above. 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:
Changed lines 54-57 from:
The max patch looks like this. The text of the patch is linked behind the image.

%width=600px%[[Attach:max-serialCallResponse.txt|Attach:max-serialCallResponse.png]]
to:
The max patch looks like this. Copy the text from the code sample above.

%width=600px%[[Attach:max-serialCallResponse.png|Attach:max-serialCallResponse.png]]
September 16, 2010, at 10:14 PM by Tom Igoe -
Changed line 44 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
August 27, 2010, at 08:52 AM by Christian Cerrito -
Changed line 166 from:
* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
to:
* [[Tutorial/VirtualColorMixer | Virtual Color Mixer]] - send multiple variables from an Arduino to the computer and read them in Processing.
August 27, 2010, at 08:46 AM by Christian Cerrito -
Changed line 11 from:
''Hardware Required'''
to:
!!!Hardware Required
August 27, 2010, at 08:45 AM by Christian Cerrito -
Added lines 155-170:

!!!See Also:

* [[Serial/begin | @@serial.begin()@@ ]]
* [[Serial/available | @@serial.available()@@]]
* [[Serial/print | @@serial.print()@@]]
* [[Reference/analogRead | @@analogRead()@@]]

* [[Tutorial/Dimmer]] - move the mouse to change the brightness of an LED.
* [[Tutorial/Graph | Graph ]] - send data to the computer and graph it in Processing
* [[Tutorial/PhysicalPixel | Physical Pixel ]] - turn an LED on and off by sending data from Processing.
* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
* [[Tutorial/SerialCallResponseASCII | Serial Call and Response ASCII]] - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
* [[Tutorial/SwitchCase2 | Serial Input (Switch (case) Statement)]] - How to take different actions based 0n characters received by the serial port.

August 27, 2010, at 08:38 AM by Christian Cerrito -
Added lines 11-22:
''Hardware Required'''
* Arduino Board
* (2) analog sensors (potentiometer, photocell, FSR, etc.)
* (1) momentary switch/button
* (3) 10K ohm resistors
* breadboard
* hook-up wire

'''Software Required'''
* [[http://www.processing.org |Processing]] or
* [[http://www.cycling74.com/products/max5| Max/MSP version 5]]
Changed lines 25-27 from:
Analog inputs connected to analog input pin 0 and 1.
Switch connected to digital I/O 2.
to:
Analog inputs connected to analog input pin 0 and 1 with 10K ohm resistors used as voltage dividers. Switch/button connected to digital I/O pin 2 with a 10K ohm resistor as a reference to ground.
Changed line 44 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
February 23, 2010, at 11:36 PM by Tom Igoe -
Changed lines 33-49 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Serial Call and Response
%color=#7e7e7e% Language: Wiring/Arduino
%color=#7e7e7e%
%color=#7e7e7e% This program sends an ASCII A (byte of value 65) on startup
%color=#7e7e7e% and repeats that until it gets some data in.
%color=#7e7e7e% Then it waits for a byte in the serial port, and
%color=#7e7e7e% sends three sensor values whenever it gets a byte in.
%color=#7e7e7e%
%color=#7e7e7e% Thanks to Greg Shakar and Scott Fitzgerald for the improvements
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * potentiometers attached to analog inputs 0 and 1
%color=#7e7e7e% * pushbutton attached to digital I/O 2
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/SerialCallResponse
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Communication/SerialCallResponse/SerialCallResponse.pde lang=arduino tabwidth=4:)
Deleted lines 34-80:
%color=#7e7e7e% Created 26 Sept. 2005
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e% Modified 14 April 2009
%color=#7e7e7e% by Tom Igoe and Scott Fitzgerald
%color=#7e7e7e% */

%color=#cc6600%int%% firstSensor = 0; %color=#7e7e7e%// first analog sensor
%color=#cc6600%int%% secondSensor = 0; %color=#7e7e7e%// second analog sensor
%color=#cc6600%int%% thirdSensor = 0; %color=#7e7e7e%// digital sensor
%color=#cc6600%int%% inByte = 0; %color=#7e7e7e%// incoming serial byte

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%()
{
%color=#7e7e7e%// start serial port at 9600 bps:
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600);
%color=#cc6600%pinMode%%(2, %color=#006699%INPUT%%); %color=#7e7e7e%// digital sensor is on digital pin 2
establishContact(); %color=#7e7e7e%// send a byte to establish contact until receiver responds
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%()
{
%color=#7e7e7e%// if we get a valid byte, read analog ins:
%color=#cc6600%if%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() > 0) {
%color=#7e7e7e%// get incoming byte:
inByte = %color=#cc6600%Serial%%.%color=#cc6600%read%%();
%color=#7e7e7e%// read first analog input, divide by 4 to make the range 0-255:
firstSensor = %color=#cc6600%analogRead%%(0)/4;
%color=#7e7e7e%// delay 10ms to let the ADC recover:
%color=#cc6600%delay%%(10);
%color=#7e7e7e%// read second analog input, divide by 4 to make the range 0-255:
secondSensor = %color=#cc6600%analogRead%%(1)/4;
%color=#7e7e7e%// read switch, map it to 0 or 255L
thirdSensor = %color=#cc6600%map%%(%color=#cc6600%digitalRead%%(2), 0, 1, 0, 255);
%color=#7e7e7e%// send sensor values:
%color=#cc6600%Serial%%.%color=#cc6600%print%%(firstSensor, %color=#006699%BYTE%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(secondSensor, %color=#006699%BYTE%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(thirdSensor, %color=#006699%BYTE%%);
}
}

%color=#cc6600%void%% establishContact() {
%color=#cc6600%while%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() <= 0) {
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%'A'%%, %color=#006699%BYTE%%); %color=#7e7e7e%// send a capital A
%color=#cc6600%delay%%(300);
}
}
Added line 37:
Added lines 54-55:
*
*This example code is in the public domain
August 27, 2009, at 08:56 PM by Tom Igoe -
Changed lines 19-21 from:
to:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

July 05, 2009, at 07:38 PM by Tom Igoe -
Changed lines 29-99 from:
to:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Serial Call and Response
%color=#7e7e7e% Language: Wiring/Arduino
%color=#7e7e7e%
%color=#7e7e7e% This program sends an ASCII A (byte of value 65) on startup
%color=#7e7e7e% and repeats that until it gets some data in.
%color=#7e7e7e% Then it waits for a byte in the serial port, and
%color=#7e7e7e% sends three sensor values whenever it gets a byte in.
%color=#7e7e7e%
%color=#7e7e7e% Thanks to Greg Shakar and Scott Fitzgerald for the improvements
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * potentiometers attached to analog inputs 0 and 1
%color=#7e7e7e% * pushbutton attached to digital I/O 2
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/SerialCallResponse

%color=#7e7e7e% Created 26 Sept. 2005
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e% Modified 14 April 2009
%color=#7e7e7e% by Tom Igoe and Scott Fitzgerald
%color=#7e7e7e% */

%color=#cc6600%int%% firstSensor = 0; %color=#7e7e7e%// first analog sensor
%color=#cc6600%int%% secondSensor = 0; %color=#7e7e7e%// second analog sensor
%color=#cc6600%int%% thirdSensor = 0; %color=#7e7e7e%// digital sensor
%color=#cc6600%int%% inByte = 0; %color=#7e7e7e%// incoming serial byte

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%()
{
%color=#7e7e7e%// start serial port at 9600 bps:
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600);
%color=#cc6600%pinMode%%(2, %color=#006699%INPUT%%); %color=#7e7e7e%// digital sensor is on digital pin 2
establishContact(); %color=#7e7e7e%// send a byte to establish contact until receiver responds
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%()
{
%color=#7e7e7e%// if we get a valid byte, read analog ins:
%color=#cc6600%if%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() > 0) {
%color=#7e7e7e%// get incoming byte:
inByte = %color=#cc6600%Serial%%.%color=#cc6600%read%%();
%color=#7e7e7e%// read first analog input, divide by 4 to make the range 0-255:
firstSensor = %color=#cc6600%analogRead%%(0)/4;
%color=#7e7e7e%// delay 10ms to let the ADC recover:
%color=#cc6600%delay%%(10);
%color=#7e7e7e%// read second analog input, divide by 4 to make the range 0-255:
secondSensor = %color=#cc6600%analogRead%%(1)/4;
%color=#7e7e7e%// read switch, map it to 0 or 255L
thirdSensor = %color=#cc6600%map%%(%color=#cc6600%digitalRead%%(2), 0, 1, 0, 255);
%color=#7e7e7e%// send sensor values:
%color=#cc6600%Serial%%.%color=#cc6600%print%%(firstSensor, %color=#006699%BYTE%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(secondSensor, %color=#006699%BYTE%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(thirdSensor, %color=#006699%BYTE%%);
}
}

%color=#cc6600%void%% establishContact() {
%color=#cc6600%while%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() <= 0) {
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%'A'%%, %color=#006699%BYTE%%); %color=#7e7e7e%// send a capital A
%color=#cc6600%delay%%(300);
}
}

(:divend:)

!!! Processing Code
Deleted lines 100-145:
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);
}
}

@]

!!! Processing Code

[@
June 25, 2009, at 10:51 PM by Tom Igoe -
Added lines 20-27:
!!!Schematic
[-click the image to enlarge-]

%width=400px%[[Attach:SerialCallResponse_schem.png |Attach:SerialCallResponse_schem.png]]



April 16, 2009, at 07:44 PM by Tom Igoe -
Changed lines 17-19 from:
%width=400px%[[Attach:SerialCallResponse-circuit2.png |Attach:SerialCallResponse-circuit2.png]]

to:
%width=400px%[[Attach:SerialCallResponse-circuit3.png |Attach:SerialCallResponse-circuit3.png]]

April 16, 2009, at 01:23 PM by Tom Igoe -
Added line 15:
April 16, 2009, at 01:23 PM by Tom Igoe -
Deleted lines 12-13:
%width=400px%[[Attach:SerialCallResponse-circuit2.png |Attach:SerialCallResponse-circuit2.png]]
Changed lines 15-18 from:
to:
[-click on the image to enlarge -]
%width=400px%[[Attach:SerialCallResponse-circuit2.png |Attach:SerialCallResponse-circuit2.png]]

April 16, 2009, at 01:23 PM by Tom Igoe -
Changed lines 13-14 from:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit.png]]
to:
%width=400px%[[Attach:SerialCallResponse-circuit2.png |Attach:SerialCallResponse-circuit2.png]]
April 16, 2009, at 12:45 AM by Tom Igoe -
Changed lines 159-160 from:
As you change the value of the analog sensor, you'll get a graph something like this:
to:
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:
April 16, 2009, at 12:42 AM by Tom Igoe -
Changed lines 13-14 from:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit2.png]]
to:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit.png]]
April 16, 2009, at 12:42 AM by Tom Igoe -
Changed lines 13-14 from:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit.png]]
to:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit2.png]]
April 16, 2009, at 12:41 AM by Tom Igoe -
Changed lines 7-8 from:
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.
to:
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.
Deleted line 20:
 
April 16, 2009, at 12:40 AM by Tom Igoe -
Changed line 21 from:
to:
 
April 16, 2009, at 12:40 AM by Tom Igoe -
Added line 62:
April 16, 2009, at 12:39 AM by Tom Igoe -
Changed lines 13-14 from:
%width=400px%[[Attach:graph-circuit.png |Attach:graph-circuit.png]]
to:
%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-circuit.png]]
April 16, 2009, at 12:38 AM by Tom Igoe -
Added lines 1-167:
''Examples > Communication''

!!Serial Call and Response (handshaking)

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.

!!! Circuit

%width=400px%[[Attach:graph-circuit.png |Attach:graph-circuit.png]]

Analog inputs connected to analog input pin 0 and 1.
Switch connected to digital I/O 2.

!!! Code

[@

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);
}
}
@]

!!! Processing Code

[@
/**
* 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;
}
}
}

@]

!!! Output

As you change the value of the analog sensor, you'll get a graph something like this:

Attach:serialCallResponse-output.png

!!! Max Code

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

%width=600px%[[Attach:max-serialCallResponse.txt|Attach:max-serialCallResponse.png]]

Share