rhoadley.net music research software blogs
aru seminars m&t critski focm1a cmc circuit bending mic2b sensor technology comp 3 sonic art major project
youtube vimeo facebook
Task 10 | Vibrating a piezo | Set: w8 | Due: w13 | Weighting: logbook (50%) | Courses: stech |
Prev Task: "Hello world!" | Next Task: Connecting the Arduino, MaxMSP and SuperCollider | ||||
Task Summary | All cbhh tasks |
To cover:
Using a Piezo disc to make sounds Give the sounds a duration Generate and Modulate Pulses Notes Over the Serial Port with Duration Controlled by a Potentiometer (Use the Arduino Board with Max) Also see this tutorial: http://arduino.cc/en/Tutorial/tone
First, use a Piezo to make sounds via the Arduino, (piezos can be used to create sounds as well as 'receive' them: see the knock example from Arduino).
This is a task in creating sound in one type of way using the Arduino. It's also used to introduce us to some more examples of Arduino code.
/* Play notes over serial * v0.2 February 2010 * arduino example: arduino.cc * mods by rhoadley, rhoadley.net, rhoadley.net/stech * * Arduino Code Example - playing sounds * Example 1: just playing sounds * The following program is an example that will play notes * out of the characters received over the serial port. * To try it out, upload this file to Arduino, hook up a piezo * or a speaker to digital pin 8 (black to ground) and send characters to play * each note. * Do this by opening the Serial Monitor (button in bar above), * typing a letter in the field that appears below and clicking * on the 'Send' button. * * It's tricky to get two piezos/speakers working together. Why is this? * * * ---------------------- * * note - half period (uS) * ----------------------- * c - 1,911 * d - 1,703 * e - 1,517 * f - 1,432 * g - 1,276 * a - 1,136 * b - 1,012 * C - 956 */ int val = 0; // to check if there is data available from the serial port int val2 = 0; // stores the data that came from the port int piezoPin = 8; // pin number where to hook the speaker int pulso = 0; // value of half pulse void setup() { Serial.begin(9600); pinMode(piezoPin, OUTPUT); } void loop() { val = Serial.available(); if (val > 0) { val2 = Serial.read(); if (val2 == 'c') { pulso = 1911; } if (val2 == 'd') { pulso = 1703; } if (val2 == 'e') { pulso = 1517; } if (val2 == 'f') { pulso = 1432; } if (val2 == 'g') { pulso = 1276; } if (val2 == 'a') { pulso = 1136; } if (val2 == 'b') { pulso = 1012; } if (val2 == 'C') { pulso = 956; } } digitalWrite(piezoPin, HIGH); delayMicroseconds(pulso); digitalWrite(piezoPin, LOW); delayMicroseconds(pulso); }
/* Play Notes Over Serial Port with Duration * v0.2 October 2010 * arduino example: arduino.cc * mods by rhoadley, rhoadley.net, rhoadley.net/stech * * Example 2: upgrading to notes * Notes have duration in time, this program will play a tone for some time, * and then go silent, instead of keeping it sounding forever, * as the previous program does. * This example also plays strings of letters. * * To try it out, upload this file to Arduino, hook up a piezo * or a speaker to digital pin 8 (black to ground) and send characters to play * each note. * * Send the Arduino letters by opening the Serial Monitor (button in bar above), * typing a letter in the field that appears below and clicking * on the 'Send' button. * * ---------------------------- * * note - half period (uS) * ----------------------- * c - 1,911 * d - 1,703 * e - 1,517 * f - 1,432 * g - 1,276 * a - 1,136 * b - 1,012 * C - 956 * * We create a function that will take care of * playing a tone for a while and go silent * afterwards */ int val = 0; // to check if there is data available from the serial port int val2 = 0; // stores the data that came from the port int piezoPin = 8; // pin number where to hook the speaker int dur = 150; // int randDur = 500; // Use if you want random values void playNote(int note) { // in the following line, dur represents the duration of the note in milliseconds // randDur = random(dur); // Use if you want random values // in the below change 'dur' to 'randDur' if you want random values for( int a=0; a <= dur; a++) { digitalWrite(piezoPin, HIGH); delayMicroseconds(note); digitalWrite(piezoPin, LOW); delayMicroseconds(note); } } void setup() { Serial.begin(9600); pinMode(piezoPin, OUTPUT); // randomSeed(analogRead(0)); // Use if you want to use random durations (see above) } void loop() { val = Serial.available(); if (val > 0) { val2 = Serial.read(); if (val2 == 'c') { playNote(1911); } if (val2 == 'd') { playNote(1703); } if (val2 == 'e') { playNote(1517); } if (val2 == 'f') { playNote(1432); } if (val2 == 'g') { playNote(1276); } if (val2 == 'a') { playNote(1136); } if (val2 == 'b') { playNote(1012); } if (val2 == 'C') { playNote(956); } } }
/* Play Siren-like Pulses * v0.2 October 2010 * arduino example: arduino.cc * mods by rhoadley, rhoadley.net * * Example 3: Playing Pulses * The following program is an example that will play notes * out of the characters received over the serial port. * To try it out, upload this file to Arduino, hook up a piezo * or a speaker to digital pin 8 (black to ground) and send characters to play * each note. * Do this by opening the Serial Monitor (button in bar above), * typing a letter in the field that appears below and clicking * on the 'Send' button. * * * ---------------------- * * note - half period (uS) * ----------------------- * c - 1,911 * d - 1,703 * e - 1,517 * f - 1,432 * g - 1,276 * a - 1,136 * b - 1,012 * C - 956 */ int val = 0; // to check if there is data available from the serial port int val2 = 0; // stores the data that came from the port int piezoPin = 8; // pin number where to hook the speaker int pulso = 0; // initialise the pulse int del1 = 400; // delay int pulso2; // initialise // int dur = 800; // uncomment if you want random duration // int randDur = 800; // uncomment if you want random duration // so you can send values to the board void setup() { Serial.begin(9600); pinMode(piezoPin, OUTPUT); // randomSeed(analogRead(0)); // uncomment if you want random duration } void loop() { val = Serial.available(); // randDur = random(dur); // uncomment if you want random duration if (val > 0) { val2 = Serial.read(); if (val2 == 'c') { pulso = 1911; } if (val2 == 'd') { pulso = 1703; } if (val2 == 'e') { pulso = 1517; } if (val2 == 'f') { pulso = 1432; } if (val2 == 'g') { pulso = 1276; } if (val2 == 'a') { pulso = 1136; } if (val2 == 'b') { pulso = 1012; } if (val2 == 'C') { pulso = 956; } } digitalWrite(piezoPin, HIGH); delayMicroseconds(pulso2); digitalWrite(piezoPin, LOW); delayMicroseconds(pulso2); delayMicroseconds(del1); pulso2 = (pulso2-10); delayMicroseconds(del1); if (pulso2 < 200) // change 200 to 'randDur' if you want random Duration { pulso2 = pulso; } }
/* Play Notes Over Serial Port with Duration * v0.2 December 2010 * arduino example: arduino.cc * mods by rhoadley, rhoadley.net * * Example 2: upgrading to notes * Notes have duration in time, this program will play a tone for some time, * and then go silent, instead of keeping it sounding forever, * as the previous program does. * This example also plays strings of letters. * * To try it out, upload this file to Arduino, hook up a piezo * or a speaker to digital pin 8 (black to ground) and send characters to play * each note. * Then, connect a potentiometer (for example, 470K log or lin) in the following fashion: the outer connections to 5v and ground, * the central connection to Analogue In 0. * * Send the Arduino letters by opening the Serial Monitor (button in bar above), * typing a letter in the field that appears below and clicking * on the 'Send' button. * * ---------------------------- * * note - half period (uS) * ----------------------- * c - 1,911 * d - 1,703 * e - 1,517 * f - 1,432 * g - 1,276 * a - 1,136 * b - 1,012 * C - 956 * * We create a function that will take care of * playing a tone for a while and go silent * afterwards */ int val = 0; // to check if there is data available from the serial port int val2 = 0; // stores the data that came from the port int piezoPin = 8; // pin number where to hook the speaker int potVal = 150; // the value of the potentiometer int potPin = 0; // select the input pin for the potentiometer void playNote(int note) { // in the following line, dur represents the duration of the note in milliseconds for( int a=0; a <= potVal; a++) { digitalWrite(piezoPin, HIGH); delayMicroseconds(note); digitalWrite(piezoPin, LOW); delayMicroseconds(note); } } void setup() { Serial.begin(9600); pinMode(piezoPin, OUTPUT); } void loop() { potVal = analogRead(potPin); // read the value from the sensor Serial.println(potVal); val = Serial.available(); if (val > 0) { val2 = Serial.read(); if (val2 == 'c') { playNote(1911); } if (val2 == 'd') { playNote(1703); } if (val2 == 'e') { playNote(1517); } if (val2 == 'f') { playNote(1432); } if (val2 == 'g') { playNote(1276); } if (val2 == 'a') { playNote(1136); } if (val2 == 'b') { playNote(1012); } if (val2 == 'C') { playNote(956); } } }
/* Play Notes Over the Serial Port with Frequency Controlled by a Potentiometer * v0.2 February 2010 * arduino example: arduino.cc * mods by rhoadley, rhoadley.net * * Example 2: upgrading to notes * Notes have duration in time, this program will play a tone for some time, * and then go silent, instead of keeping it sounding forever, * as the previous program does. * This example also plays strings of letters. * * To try it out, upload this file to Arduino, hook up a piezo * or a speaker to digital pin 8 (black to ground) and send characters to play * each note. * Then, connect a potentiometer (for example, 470K log or lin) in the following fashion: the outer connections to 5v and ground, * the central connection to Analogue In 0. * * Send the Arduino letters by opening the Serial Monitor (button in bar above), * typing a letter in the field that appears below and clicking * on the 'Send' button. * * ---------------------------- * * note - half period (uS) * ----------------------- * c - 1,911 * d - 1,703 * e - 1,517 * f - 1,432 * g - 1,276 * a - 1,136 * b - 1,012 * C - 956 * * We create a function that will take care of * playing a tone for a while and go silent * afterwards */ int val = 0; // to check if there is data available from the serial port int val2 = 0; // stores the data that came from the port int piezoPin = 8; // pin number where to hook the speaker int potVal = 150; // the value of the potentiometer int potPin = 0; // select the input pin for the potentiometer void playNote(int note) { // in the following line, dur represents the duration of the note in milliseconds for( int a=0; a <= 20; a++) { digitalWrite(piezoPin, HIGH); delayMicroseconds(note); digitalWrite(piezoPin, LOW); delayMicroseconds(note); } } void setup() { Serial.begin(9600); pinMode(piezoPin, OUTPUT); } void loop() { potVal = analogRead(potPin); // read the value from the sensor Serial.println(potVal+1); val = Serial.available(); if (val > 0) { playNote(potVal+1); } }
For those of you eager to get on with connecting to Max, here are some patches to be playing with. Incidentally, you need to be a little careful with properly opening and closing your serial ports, whether USB or Bluetooth. If you don't, you stand the very real risk of crashing one or other requiring a reboot of your computer.
Please see here for the latest versions of these patches, as well as other methods of communicating.
You might also be interested in:
The projects and tasks are designed to help you through the various courses and materials that you'll have to deal with, and also to provide an active and practical element to what could otherwise become a rather dry and technical exercise. Tasks are small exercises - you may be asked to complete one or two per week. Projects are larger and carry a higher percentage of the mark. We will undertake two, three, four or more projects and tasks. The final project is usually an individual choice project, and will be worth significantly more than the others in terms of percentages in your portfolio. We will usually try to set aside a time to perform the projects in a public setting.