Tutotial.KeyboardSerial History

Hide minor edits - Show changes to output

October 18, 2005, at 08:56 AM by 195.178.229.25 -
Changed lines 5-6 from:
A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 10, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.
to:
A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 9, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.
Changed lines 11-12 from:
''Example of connection of a Piezo to pin 10''
to:
''Example of connection of a Piezo to pin 9''
Added line 51:
pinMode(speakerOut, OUTPUT);
Changed line 56 from:
analogWrite(speakerOut, 0);
to:
digitalWrite(speakerOut, LOW);
Changed line 66 from:
analogWrite(speakerOut,500);
to:
digitalWrite(speakerOut, HIGH);
Changed line 68 from:
analogWrite(speakerOut, 0);
to:
digitalWrite(speakerOut, LOW);
Deleted line 73:
October 18, 2005, at 12:37 AM by 195.178.229.25 -
Added lines 1-74:
!!Keyboard Serial

This example makes use of a Piezo Speaker in order to play tones. We are taking advantage of the processors capability to produde PWM signals in order to play music. There is more information about how PWM works written by David Cuartielles [[http://webzone.k3.mah.se/k3dacu/projects/ivrea/motor/pwm.html | here]] and even at K3's old [[http://proto.labbs.net/modules.php?op=modload&name=News&file=article&sid=13 | course guide]]

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the pin number 10, that supports the functionality of writing a PWM signal to it, and not just a plain HIGH or LOW value.

The tones can be lauched using any program able of sending ascii values over the serial port. Terminal software packages, Processing, Pure Data, Director, or the serial proxy + Flash combination can be used to launch the tones.

[[http://static.flickr.com/31/53523608_3d4268ba68_o.jpg|http://static.flickr.com/31/53523608_3d4268ba68.jpg]]

''Example of connection of a Piezo to pin 10''

[=
/* Keyboard Serial
* ----------------
*
* Program to play tones depending on the
* data coming from the serial port.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* (cleft) 2005 D. Cuartielles for K3
*/

int ledPin = 13;
int speakerOut = 9;
byte names[] ={'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte val = 0;
int serByte = -1;
int statePin = LOW;
int count = 0;

void setup() {
pinMode(ledPin, OUTPUT);
beginSerial(9600);
}

void loop() {
analogWrite(speakerOut, 0);
serByte = serialRead();
if (serByte != -1) {
val = serByte;
printByte(val);
statePin = !statePin;
digitalWrite(ledPin, statePin);
}
for (count=0;count<=8;count++) {
if (names[count] == val) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count]);
}
}
}


=]

Share