Arduino For Dummies
Book image
Explore Book Buy On Amazon

The only thing better than sending signals to Processing is sending multiple signals, right? Sending multiple signals is often a stumbling block, though, because although sending values from multiple sensors is easy, handling them in the correct order on the other end can often be difficult.

You need:

  • An Arduino Uno

  • A breadboard

  • Two 10k ohm potentiometers

  • A pushbutton

  • A 10k ohm resistor

  • Jump wires

The circuit is a combination of three separate inputs. Although they all use the same power and ground, you can think of the inputs individually. Two potentiometers provide two values. These are wired in the same way you would wire a temperature sensor, with one side wired to 5V and the other wired to the analog input pin that is reading it as well as to GND via a resistor.


These could actually be replaced with any analog inputs with the appropriate resistors. The pushbutton provides a digital input as well. One side of the pushbutton is wired to 5V and the other is wired to the digital pin reading it as well as GND via a resistor.


How to set up the Arduino code

After you assemble your circuit, you need the appropriate software to use it. From the Arduino menu, choose File→Examples→04.Communication→SerialCallResponse. This sketch contains both Arduino code and the relevant Processing code for the sketch to work. The Processing code beneath the Arduino code is commented out to avoid interference with the Arduino sketch.

 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
 Created 26 Sept. 2005
 by Tom Igoe
 modified 24 April 2012
 by Tom Igoe and Scott Fitzgerald
 This example code is in the public domain.
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:
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 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 =;
 // read first analog input, divide by 4 to make the range 0-255:
 firstSensor = analogRead(A0)/4;
 // delay 10ms to let the ADC recover:
 // 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:
void establishContact() {
 while (Serial.available() <= 0) {
 Serial.print('A'); // send a capital A

Upload this code to your Arduino.

How to set up the Processing code

You find the Processing code within multiline comment markers (/* */) at the bottom of the Arduino SerialCallResponse sketch. Copy the code within the comment markers into a new Processing sketch and save with an appropriate name.

// 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:
 // 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() {
 // Draw the shape
 ellipse(xpos, ypos, 20, 20);
void serialEvent(Serial myPort) {
 // read a byte from the serial port:
 int inByte =;
 // 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;
 // 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:
  // Reset serialCount:
  serialCount = 0;

Click the Run button to execute the Processing sketch, and an applet appears. The applet has a black background, and whenever you press the pushbutton, a white dot appears. Move the potentiometers to move the dot horizontally and vertically. When you release the pushbutton, the dot disappears.

About This Article

This article is from the book:

About the book author:

John Nussey is a designer, a technologist, and an entrepreneur who loves using technology in new and interesting ways. A proud Arduino advocate, he has taught the craft of physical computing and prototyping to people of all ages, competencies, and abilities.

This article can be found in the category: