Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Processing 2 Creative Coding Hotshot.pdf
Скачиваний:
10
Добавлен:
17.03.2016
Размер:
5.28 Mб
Скачать

The Smilie-O-Mat Controller

17. And this is what your wiring currently should look like the following picture:

Objective Complete - Mini Debriefing

For this task of our current mission, we have created a prototype version of the hardware we need for our Smilie-O-Mat controller. We have added three variable resistors, and in step 8 we defined a protocol to send the values of these inputs to a computer via the serial port. The protocol also allows us to detect which input knob has been turned.

Starting with step 11, we extended the circuit and added a button to it. We also extended the messages we send to the computer with a click message. This message gets transmitted when the value of the button changes from pressed to released, and we will use it to trigger our tweet() method in the next task.

Changing your face

The current task of our mission is to integrate the prototyped hardware controller we have just created into the Smilie-O-Mat controller. We will extend the program to parse the messages that our Arduino-based controller is sending via the serial port and adjust the sliders to match the values of the hardware knobs. When the user of our controller presses the button on the board, we are going to call the tweet() method.

126

Project 5

Engage Thrusters

Let's change our smiley using our controller:

1.Open the Smilie-O-Mat sketch from our previous mission.

2.To get access to the serial port, we need to import the serial library, so use the Sketch | Import library … | serial menu to import it.

3.Now add a Serial object to your sketch:

import twitter4j.*;

import processing.serial.*;

Serial port;

Twitter twitter; String[] tweets = {

"I feel like so #SmilieOMat",

"I currently feel like this #SmilieOMat", "This is how I feel #SmilieOMat"

};

...

4.In the setup() method, we need to initialize the serial port. Be sure to change the port name of your serial port like you did in the first task of this mission:

void setup() { size( 300,390 ); smooth(); background( 255 ); colorMode( HSB );

textFont( createFont( "Georgia", 20 )); initTwitter();

port = new Serial( this, "/dev/ttyUSB1", 9600);

}

5.We need to parse the messages from the serial port; so let's add the serialEvent() method to get notified every time a message is available. We are checking the first letter of the string we get, and if it starts with a capital A, B, or C, then we will parse the rest of the string and store it in the control variables for the facial parameters:

void serialEvent( Serial p ) {

 

String

in

= p.readStringUntil(

'\n' );

if (

in

!= null ) {

 

if

(

in.charAt( 0 ) == 'A' )

{

 

eye

=

int(in.substring( 1,in.length()-2 ));

127

The Smilie-O-Mat Controller

}else if ( in.charAt( 0 ) == 'B' ) {

mouth = int(in.substring( 1,in.length()-2 ));

}else if ( in.charAt( 0 ) == 'C' ) {

col = int(in.substring( 1,in.length()-2 ));

}

}

6.Connect your Arduino board to the USB port and start your Smilie-O-Mat sketch. Each variable resistor controls one facial parameter.

7.Now we are going to add support for the button, so add the following code to the serialEvent() method to make it react to the click messages we send from our Arduino:

void serialEvent( Serial p ) {

String

in

= p.readStringUntil( '\n' );

if (

in

!= null ) {

if

(

in.charAt(0) == 'A' ) {

 

eye

=

int(in.substring(1,in.length()-2));

}else if ( in.charAt(0) == 'B' ) {

mouth = int(in.substring(1,in.length()-2));

}else if ( in.charAt(0) == 'C' ) {

col = int(in.substring(1,in.length()-2));

}else if ( "click".equals( in.trim() )) {

tweet();

}

}

}

8.If we would run the code now, every now and then our Arduino would send two or three click messages in quick succession, because when a button closes or opens, there is a short phase where it has very little contact and switches very fast between the open and closed state. This behavior is called bouncing. To prevent multiple messages from being sent to Twitter, we are going to implement a little countermeasure in our Arduino sketch called debouncing. Open the Arduino sketch for our controller and add the following two variables:

int a = 0;

int lasta = a;

int b = 0;

int lastb = b;

int c = 0;

int lastc = c;

128

Project 5

int button = LOW; int buttonLast = LOW;

long buttonTime = 0; long debounceDelay = 50;

9.In our loop() method, we are going to save the time we use sending a click event and check the button's state only if some time has passed. In our case, we have set the debounceDelay variable to 50 milliseconds:

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;

}

if ( millis() > buttonTime + debounceDelay ) { button = digitalRead( 2 );

if ( buttonLast == HIGH && button == LOW ) { Serial.println("click");

}

buttonLast = button; buttonTime = millis();

}

}

10. Compile and upload the Arduino code and switch back to our Processing sketch.

129

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]