Tutorial.SerialCallResponseASCII History

Hide minor edits - Show changes to output

May 02, 2012, at 04:09 PM by Scott Fitzgerald -
Changed line 46 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/04.Communication/SerialCallResponseASCII/SerialCallResponseASCII.ino lang=arduino tabwidth=4:)
April 24, 2012, at 09:55 PM by Scott Fitzgerald -
Changed lines 59-60 from:
%width=600px%[[Attach:max-serialCallResponseASCII.png|Attach:max-serialCallResponseASCII.png]]
to:
%width=600px%[[Attach:Max5SerialCallResponseASCII.png|Attach:Max5SerialCallResponseASCII.png]]
Deleted line 74:
April 24, 2012, at 09:32 PM by Scott Fitzgerald -
Changed lines 59-60 from:
%width=600px%[[Attach:Max5SerialCallResponseASCII.png|Attach:Max5SerialCallResponseASCII.png]]
to:
%width=600px%[[Attach:max-serialCallResponseASCII.png|Attach:max-serialCallResponseASCII.png]]
Added line 75:
April 24, 2012, at 07:26 PM by Scott Fitzgerald -
Changed lines 59-60 from:
%width=600px%[[Attach:max-serialCallResponseASCII.png|Attach:max-serialCallResponseASCII.png]]
to:
%width=600px%[[Attach:Max5SerialCallResponseASCII.png|Attach:Max5SerialCallResponseASCII.png]]
Deleted line 74:
November 16, 2011, at 04:15 AM by Scott Fitzgerald -
Changed line 46 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:17 AM by Scott Fitzgerald -
September 30, 2011, at 03:15 AM by Scott Fitzgerald -
Changed line 46 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.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) with ASCII-encoded output
to:
!!!Serial Call and Response (handshaking) with ASCII-encoded output
September 23, 2010, at 10:25 PM by Christian Cerrito -
Changed lines 39-42 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:23 PM by Tom Igoe -
Changed lines 59-60 from:
%width=600px%[[Attach:max-serialCallResponseASCII.txt|Attach:max-serialCallResponseASCII.png]]
to:
%width=600px%[[Attach:max-serialCallResponseASCII.png|Attach:max-serialCallResponseASCII.png]]
September 17, 2010, at 09:22 PM by Tom Igoe -
Changed lines 5-8 from:
An example of string-based communication from the Arduino board to the computer using a call-and-response (handshaking) method.

This program sends an ASCII string on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as ASCII-encoded numbers, separated by commas and terminated by a linefeed and carriage return, and waits for another response from the computer.
to:
This example demonstrates string-based communication from the Arduino board to the computer using a call-and-response (handshaking) method.

The sketch sends an ASCII string on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as ASCII-encoded numbers, separated by commas and terminated by a linefeed and carriage return, and waits for another response from the computer.
Changed lines 11-12 from:
Compare this to the Serial call and response example. They are similar, in that both use a handshaking method, but this one encodes the sensor readings as strings, while the other sends them as binary values. While sending as ASCII-encoded strings takes more bytes, it means you can easily send values larger than 255 for each sensor reading.
to:
Compare this to the [[SerialCallResponse|Serial call and response example]]. They are similar, in that both use a handshaking method, but this one encodes the sensor readings as strings, while the other sends them as binary values. While sending as ASCII-encoded strings takes more bytes, it means you can easily send values larger than 255 for each sensor reading. It's also easier to read in a serial terminal program.
Changed lines 27-28 from:
Analog sensors 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 10Kohm resistors used as voltage dividers. Connect a pushbutton or switch connected to digital I/O pin 2 with a 10Kohm resistor as a reference to ground.
Changed line 36 from:
!!!Schematic
to:
!!!Schematic
Changed lines 51-135 from:

[@
/*
Serial Call and Response in ASCII
Language: Processing

Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.

created 2 Jun 2005
modified 14 Apr 2009
by Tom Igoe

This example code is in the public domain.
*/

import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port

float bgcolor; // Background color
float fgcolor; // Fill color
float xpos, ypos; // Starting position of the ball

void setup() {
size(640,480);

// List all the available serial ports
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');

// draw with smooth edges:
smooth();
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}

// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
xpos = map(sensors[0], 0,1023,0,width);
ypos = map(sensors[1], 0,1023,0,height);
fgcolor = sensors[2];
}
// send a byte to ask for more data:
myPort.write("A");
}
@]

!!! 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 57-58 from:
The max patch looks like this. The text of the patch is linked behind the image.
to:
The max patch looks like this. Copy the text from the code sample above and paste it into a new Max window
September 16, 2010, at 10:14 PM by Tom Igoe -
Changed line 46 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
August 27, 2010, at 09:03 AM by Christian Cerrito -
Changed lines 157-158 from:
* [[Tutorial/SwitchCase2 | Serial Input (Switch (case) Statement)]] - How to take different actions based 0n characters received by the serial port.
to:
* [[Tutorial/SwitchCase2 | Serial Input (Switch (case) Statement)]] - How to take different actions based on characters received by the serial port.
August 27, 2010, at 09:03 AM by Christian Cerrito -
Changed lines 150-151 from:
to:
* [[Reference/map | @@map()@@]]
Changed line 156 from:
* [[Tutorial/SerialCallResponseASCII | Serial Call and Response ASCII]] - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
to:
* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
August 27, 2010, at 08:57 AM by Christian Cerrito -
Changed lines 26-28 from:
* Analog inputs connected to analog input pin 0 and 1.
* Switch connected to digital I/O 2.
to:

Analog sensors 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 46 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
Added lines 143-157:

!!!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/VirtualColorMixer | Virtual Color Mixer]] - send multiple variables from an Arduino to the computer and read them in Processing.
* [[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:55 AM by Christian Cerrito -
Added lines 13-24:
!!!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]]
February 23, 2010, at 11:39 PM by Tom Igoe -
Changed lines 34-58 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Serial Call and Response in ASCII
%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 ASCII-encoded, comma-separated sensor values,
%color=#7e7e7e% truncated by a linefeed and carriage return,
%color=#7e7e7e% 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/SerialCallResponseASCII
%color=#7e7e7e%
%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% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Communication/SerialCallResponseASCII/SerialCallResponseASCII.pde lang=arduino tabwidth=4:)
Changed lines 36-39 from:
%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
to:
(:divend:)

!!! Processing Code

[@
/*
Serial Call and Response in ASCII
Language: Processing
Changed lines 45-51 from:
%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
}
to:
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Deleted lines 49-91:
%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%DEC%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%","%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(secondSensor, %color=#006699%DEC%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%","%%);
%color=#cc6600%Serial%%.%color=#cc6600%println%%(thirdSensor, %color=#006699%DEC%%);
}
}

%color=#cc6600%void%% establishContact() {
%color=#cc6600%while%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() <= 0) {
%color=#cc6600%Serial%%.%color=#cc6600%println%%(%color=#006699%"0,0,0"%%); %color=#7e7e7e%// send an initial string
%color=#cc6600%delay%%(300);
}
}

(:divend:)
!!! Processing Code

[@
/*
Serial Call and Response in ASCII
Language: Processing

Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Added lines 53-54:

This example code is in the public domain.
August 27, 2009, at 08:57 PM by Tom Igoe -
Changed lines 21-23 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:43 PM by Tom Igoe -
Changed lines 30-103 from:
to:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Serial Call and Response in ASCII
%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 ASCII-encoded, comma-separated sensor values,
%color=#7e7e7e% truncated by a linefeed and carriage return,
%color=#7e7e7e% 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/SerialCallResponseASCII
%color=#7e7e7e%
%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%DEC%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%","%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(secondSensor, %color=#006699%DEC%%);
%color=#cc6600%Serial%%.%color=#cc6600%print%%(%color=#006699%","%%);
%color=#cc6600%Serial%%.%color=#cc6600%println%%(thirdSensor, %color=#006699%DEC%%);
}
}

%color=#cc6600%void%% establishContact() {
%color=#cc6600%while%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() <= 0) {
%color=#cc6600%Serial%%.%color=#cc6600%println%%(%color=#006699%"0,0,0"%%); %color=#7e7e7e%// send an initial string
%color=#cc6600%delay%%(300);
}
}

(:divend:)
!!! Processing Code
Deleted lines 104-150:
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, DEC);
Serial.print(",");
Serial.print(secondSensor, DEC);
Serial.print(",");
Serial.println(thirdSensor, DEC);
}
}

void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(300);
}
}
@]

!!! Processing Code

[@
June 25, 2009, at 10:50 PM by Tom Igoe -
Changed lines 17-18 from:
[-[-click the image to enlarge-]-]
to:
[-click the image to enlarge-]
Changed lines 22-28 from:
to:
!!!Schematic
[-click the image to enlarge-]

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


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

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

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.
* Switch connected to digital I/O 2.

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


April 16, 2009, at 12:53 AM by Tom Igoe -
Changed line 157 from:
%width=600px%[[Attach:max-serialCallResponse.txt|Attach:max-serialCallResponse.png]]
to:
%width=600px%[[Attach:max-serialCallResponseASCII.txt|Attach:max-serialCallResponseASCII.png]]
April 16, 2009, at 12:52 AM by Tom Igoe -
Added lines 1-157:
''Examples > Communication''

!!Serial Call and Response (handshaking) with ASCII-encoded output

An example of string-based communication from the Arduino board to the computer using a call-and-response (handshaking) method.

This program sends an ASCII string on startup and repeats that until it gets a serial response from the computer. Then it sends three sensor values as ASCII-encoded numbers, separated by commas and terminated by a linefeed and carriage return, 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. The examples below split the incoming string on the commas and convert the string into numbers again.

Compare this to the Serial call and response example. They are similar, in that both use a handshaking method, but this one encodes the sensor readings as strings, while the other sends them as binary values. While sending as ASCII-encoded strings takes more bytes, it means you can easily send values larger than 255 for each sensor reading.

!!! Circuit

%width=400px%[[Attach:SerialCallResponse-circuit.png |Attach:SerialCallResponse-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, DEC);
Serial.print(",");
Serial.print(secondSensor, DEC);
Serial.print(",");
Serial.println(thirdSensor, DEC);
}
}

void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(300);
}
}
@]

!!! Processing Code

[@
/*
Serial Call and Response in ASCII
Language: Processing

Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.

created 2 Jun 2005
modified 14 Apr 2009
by Tom Igoe
*/

import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port

float bgcolor; // Background color
float fgcolor; // Fill color
float xpos, ypos; // Starting position of the ball

void setup() {
size(640,480);

// List all the available serial ports
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);

// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');

// draw with smooth edges:
smooth();
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}

// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);

// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));

// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
xpos = map(sensors[0], 0,1023,0,width);
ypos = map(sensors[1], 0,1023,0,height);
fgcolor = sensors[2];
}
// send a byte to ask for more data:
myPort.write("A");
}
@]

!!! 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:

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