- •Credits
- •About the Author
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Mission Briefing
- •Making Processing talk
- •Reading Shakespeare
- •Adding more actors
- •Building robots
- •Mission Accomplished
- •Mission Briefing
- •Connecting the Kinect
- •Making Processing see
- •Making a dancer
- •Dance! Dance! Dance!
- •Mission Accomplished
- •Mission Briefing
- •Can you hear me?
- •Blinking to the music
- •Making your disco dance floor
- •Here come the dancers
- •Mission Accomplished
- •Mission Briefing
- •Drawing your face
- •Let me change it
- •Hello Twitter
- •Tweet your mood
- •Mission Accomplished
- •Mission Briefing
- •Connecting your Arduino
- •Building your controller
- •Changing your face
- •Putting it in a box
- •Mission Accomplished
- •Mission Briefing
- •Drawing a sprite
- •Initiating the landing sequence
- •Running your sketch in the browser
- •Running the game on an Android phone
- •Mission Accomplished
- •Mission Briefing
- •Rotating a sphere
- •Let there be light
- •From sphere to globe
- •From globe to neon globe
- •Mission Accomplished
- •Mission Briefing
- •Reading a logfile
- •Geocoding IP addresses
- •Red Dot Fever
- •Interactive Red Dot Fever
- •Mission Accomplished
- •Mission Briefing
- •Beautiful functions
- •Generating an object
- •Exporting the object
- •Making it real
- •Mission Accomplished
- •Index
The Smilie-O-Mat Controller
Starting from step 10, we removed the blinking code and replaced it with a call to the println() method of the Serial object, which uses the built-in serial port to send the resistor value to the computer. To read the values, we used a serial monitor that the Arduino IDE provides.
In step 16, we started writing a Processing application that listens to the serial port instead of the serial monitor and displays the values it reads on the serial port using the Processing's serial library.
Classified Intel
Arduino sends values from the variable resistor in a range from 0 to 1,023. Usually, we expect the values to be zero on the left-hand side of the knob and the maximum on the right-hand side. If your controller sends the value the other way around, you have two possibilities to fix this. On the hardware side, you can switch the leads that go to Gnd and 5V on the Arduino board. On the software side, you can calculate the value by subtracting it from the maximum value:
val = 1023 – val;
The software side is especially helpful if you have already soldered your circuit.
Building your controller
In the first task of this mission, we learned how to connect a variable resistor to Arduino and send its value to the computer using the serial port. Our next task is to extend this basic circuit and add two more resistors, because we want to use them to control all the facial parameters of our Smilie-O-Mat sketch in the next task.
We will also add a button to our circuit and learn how to detect the click events and send them to our sketch. We will extend our communication protocol a bit and write a new Processing sketch that parses the messages—our Arduino generates—and displays the values we send.
118
Project 5
Engage Thrusters
Let's build our controller:
1.For this task, we need to expand our circuit and add two more variable resistors. So let's start by connecting the 5V pin to one of the outer power rails of the breadboard and the Gnd pin to the other one. Connect your three resistors to the breadboard as shown in the following diagram:
119
The Smilie-O-Mat Controller
2.Connect the left-outer lead of each resistor to the 5V rail and the other one to Gnd, as shown in the following diagram:
120
Project 5
3.Now connect the Arduino pins, analog inputs 0, 1, and 2 to the middle pins of your resistors. Your wiring should look as shown in the following diagram:
4.In our loop() method, we need to copy the code that reads the value and sends it to the serial port to read the other two ports as well:
int a = 0;
int lastA = a;
int b = 0;
int lastB = b;
int c = 0;
int lastC = c;
void setup() { Serial.begin( 9600 );
}
121
The Smilie-O-Mat Controller
void loop() {
a = analogRead( 0 );
if (abs( a - lastA ) > 10 ) { Serial.println( a );
lastA = a;
}
b = analogRead( 1 );
if (abs( b - lastB ) > 10 ) { Serial.println( b );
lastB = b;
}
c = analogRead( 2 );
if (abs( c - lastC ) > 10 ) { Serial.println( c );
lastC = c;
}
}
5.Compile the code and upload it to your Arduino.
6.Now run the processing sketch we created for the first task, Connecting your Arduino, and turn the knobs of the resistors. We see that the value changes, but our Processing sketch isn't able to recognize which resistor sent the value. So, switch back to the Arduino IDE and let's add the letterA,B, orC in front of the value:
void loop() {
a = analogRead( 0 );
if (abs( a - lastA ) > 10 ) {
Serial.print("A");
Serial.println( a ); lastA = a;
}
b = analogRead( 1 );
if (abs( b - lastB ) > 10 ) {
Serial.print("B");
Serial.println( b ); lastB = b;
}
c = analogRead( 2 );
if (abs( c - lastC ) > 10 ) {
Serial.print("C");
Serial.println( c ); lastC = c;
}
}
122
Project 5
7.Compile and upload the code to your Arduino.
8.Now run the Processing sketch and turn the knobs. Each knob controls one corresponding variable in our processing sketch. Here you can see the value of the input B has changed:
9.To complete the circuit of our controller, we need to add a button. Connect the two leads of your push button to the breadboard and add the 10 kOhm resistor as shown in the following diagram:
123
The Smilie-O-Mat Controller
10.Now make a connection from the Gnd line to the resistor you just added and a connection from the 5V line to the second leg of your button.
11.Connect the digital 2 pin of your Arduino board between the resistor and the button, as shown in the following diagram:
12.The digital pins of your Arduino can be configured for input and output, so add the following highlighted code line to the setup() method of your Arduino sketch to configure the pin 2 for being used as an input:
void setup() { pinMode( 2, INPUT );
Serial.begin( 9600 );
}
13. Now add a variable to store the last state of our button and initialize it to LOW:
int button = LOW; int buttonLast = LOW;
124
Project 5
14.In our loop() method, we are going to read the current state of the button and compare it to the last value we saved. If the button state changes from pressed to released, we are going to send a message to the serial bus:
void loop() {
a = analogRead( 0 );
if (abs( a - lastA ) > 10 ) { Serial.print( "A" ); Serial.println( a );
lastA = a;
}
b = analogRead(1);
if (abs( b - lastB ) > 10 ) { Serial.print( "B" ); Serial.println( b );
lastB = b;
}
c = analogRead(2);
if (abs( c - lastC ) > 10 ) { Serial.print( "C" ); Serial.println( c );
lastC = c;
}
button = digitalRead( 2 );
if ( buttonLast == HIGH && button == LOW ) { Serial.println( "click" );
}
buttonLast = button;
}
15.Compile and upload the code to your Arduino and switch to Processing.
16.Run our Processing sketch, turn the knobs, and press the button. Each knob sends its own message now, and when the button is pressed, we receive a message on the Processing side. This is what our Processing sketch looks like when a button is pressed:
125
