Physical prototyping lab3-serious_serial
-
Upload
tony-olsson -
Category
Technology
-
view
616 -
download
0
Transcript of Physical prototyping lab3-serious_serial
PHYSICAL PROTOTYPING
© 2011 K3 Creative Commons v3.0 SA-NC
Lab 3: Serious Serial …and small sounds
With the Arduino prototyping board
SERIAL COMMUNICATION Serial.xxxx
Serial is a class designed to use serial communication
Serial.begin(baud);
Placed in setup() to initiate serial communication at a certain baudrate (speed)
Typical transmission speed for many existing devices is 9600 bps (bits per second)
Serial.print();
Writes (sends) anything to the serial port
Serial.println();
Writes (sends) with CR (carriage return) and LF (line feed)
Serial.available();
Used to check if data has arrived over the serial port
Serial.read();
Reads the first byte that arrived to the port and erases it from the communication buffer
2
SERIAL COMMUNICATION
is used to have your Arduino and your personal computer ‘talk’ to each other
(send data)
SERIAL
4
ASCII vs. RAW BYTES
ASCII stands for American Standard Code for Information Interchange
Alphanumeric characters are represented by numbers ranging from 0 to 127 and are translated into a 7-bit binary code. ASCII allows for easy transfer of text-only files between different kinds of computers
ASCII can be extended to cover symbol tables for other languages
5
ASCII vs. RAW BYTES
One could say that ASCII is kind of a markup language for symbols
The symbol 'A' (which represents the capital letter A) is represented by numeric code 65, the symbol for number '1' by numeric code 49, etc.
The communication between devices is encoded using this standard
This allows including special symbols like: LineFeed (LF), Carriage Return, Tab(ulator) (T)...
6
ASCII vs. RAW BYTES
Arduino uses 8N1 serial communication, this means that information is encoded in packages of 8-bits (1 byte)
With 8-bits we can encode 255 different symbols
If we were just sending numbers, smaller than 255, we wouldn't need to encode the data. We could just send bytes
Most of the times, this is not the case, therefore we need to use ASCII as a code
8
ASCII vs. RAW BYTES
Serial.print() or Serial.println() will, by default, encode all the data as ASCII
Therefore, the command: Serial.print(1) will send the code 49 over the serial line
Serial.println(1) will send, in this order, 49-10-13
Terminal programs like Bray-Terminal for Windows, or Gnome Serial for Linux can show the pure data sent
9
ASCII vs. RAW BYTES
• Arduino's IDE Serial Monitor is ready to present only ASCII data
• This means that the black window at the IDE's bottom will display only the ASCII representation of numbers
• Therefore, Serial.print(67, BYTE) will show character 'C'
• Serial.print(1) will show character '1'• BYTE is commonly used to send information to
programs like Processing and Puredata
10
ABOUT THE SPEEDArduino is a small but powerful computer designed to do just three tasks at the same time:
– send/receive serial data
– operate analog outputs
– compute your program
Arduino’s processor works at 16MHz, and makes about 1.000.000 operations/second.
If you send data at that pace, there is no computer in the world that can follow
11
ABOUT THE SPEEDIt is necessary to wait a short amount of time between messages when sending data from your Arduino to your computer otherwise you might run the risk of hanging up your computer.
You should use delay(50)if you want the Serial Monitor to display what comes through the port. If the delay is any shorter, it’s hard to read the values.
Personal computers are capable of doing many things at the same time. Arduino is good at doing only one particular task very quickly, over and over again.
12
LIVECODING
• Serial.begin();
• Serial.print();
• Serial.println();
• Speed issues
• Monitor
• Remember the delay(50);
13
SENDING SERIAL BACK
• We can also send data from the computer to Arduino
• The best is to make use of a simple character or symbol-based communication protocol
• It is of course possible to send full strings between the computer and Arduino, but that will require too much effort in parsing the information
14
LIVECODING
See:
Examples / Communication / PhysicalPixel
Serial.available();
Serial.read();
Using serial input switch on and off the onboard LED at port 13
15
SOFTWARE TO USE
Connecting to Flash and other programs like PureData, Processing, MAX/MSP, VVVV, Isadora, etc is a story for another time, it involves serial communication in both directions.
There are plenty of examples of installations that make use of serial communication
Also, two Arduinos can communicate with each other through serial communication, using the TX and RX pins.
16
EXERCISE ►MAKING 2 ARDUINOS TALK
• In pairs, connect your Arduinos
• Connect RX to TX
• Connect TX to RX
• GND to GND
• Create a protocol to do the Physical pixel between two Arduinos
17
SMALL SOUNDSPIEZO ELEMENT/BUZZER a.k.a. CONTACT MICROPHONE
The piezo element is both a sensor and an actuator
As sensor: Check the example ANALOG/KNOCK
As actuator: Electronic music greeting cards
18
SMALL SOUNDS
Piezo speaker can be replaced by a stereo plug which you connect to another sound output
*BEWARE* the sound output is VERY high when connected to a stereo or headphones
=
19
HOW TO PLAY SOUNDS
Piezos consist of materials which attract / repel each other (vibrate) when flows through them.
Quick vibrations on the piezo element produce sounds
We only need to connect the piezo directly to an output and write the pin HIGH and LOW
We will use the method delayMicroseconds(time)to create soundwaves
20
EXAMPLE ►PLAY “A”int outputPin = 9; // plug the piezo to pin 9
void setup() {
pinMode(outputPin, OUTPUT); // declare the piezo pin as an output
}
void loop() {
digitalWrite(outputPin, HIGH);
delayMicroseconds(1136); // the time here will // determine the tone
digitalWrite(outputPin, LOW);
delayMicroseconds(1136); // the time here will // determine the tone
}21
EXERCISE ►FM OSCILLATOR
Connect a potentiometer to an analog input, the Piezo element to a ‘digital’ input and make a sound generator which lets you change the tone by turning the potentiometer
22
potentiometer
EXERCISE ►OSCILLATORint outputPin = 9; // plug the piezo to pin 9
int inputPin = 2; // the potentiometer at analog 2
void setup() {
pinMode(outputPin, OUTPUT); // declare the piezo pin as an output
}
void loop() {
int val = analogRead(inputPin); // read the potentiometer into a variable
digitalWrite(outputPin, HIGH);
delayMicroseconds(val); // the time here will determine the tone
digitalWrite(outputPin, LOW);
delayMicroseconds(val); // the time here will determine the tone
}23
HOMEWORK: SERIAL KEYBOARD
Make a program in Arduino that plays sounds when receiving characters over the serial port
e.g. the character 'E' should play the tone 'E4'
The interaction with the program will happen entirely through serial communication
HINT: the table on next slide shows the delayMicroseconds value for the notes
Go wild on your serial synthesizer!
25