Clapping music with Arduino: Difference between revisions
(→tone) |
(→tone) |
||
Line 59: | Line 59: | ||
* Starting point is the Example > Digital > toneMelody | * Starting point is the Example > Digital > toneMelody | ||
'''Important''': ''tone'' is a unique command in that it is ''asynchronous'', meaning that when you call the function it starts playing the tone, but immediately returns control to your program. In Bash terms, it's like starting the program with the '&' character at the end -- the command runs "in the background". This is important to know, because you '''need | '''Important''': ''tone'' is a unique command in that it is ''asynchronous'', meaning that when you call the function it starts playing the tone, but immediately returns control to your program. In Bash terms, it's like starting the program with the '&' character at the end -- the command runs "in the background". This is important to know, because you '''need to use the delay''' command to play consecutive tones, otherwise each call to tone undoes the previous ones. | ||
'''So, the wrong way would be:''' | '''So, the wrong way would be:''' |
Revision as of 18:22, 26 October 2010
This exercise relies on the following topics in C:
- variable types (int, char)
- character arrays (strings)
- loops
- nested loops
Hello world.c
Arduino is C!
#include <stdio.h>
main ()
{
printf("hello world\n");
}
Blink music
void setup() {
pinMode(8, OUTPUT);
for (int i=0; i<1000; i++) {
digitalWrite(8, HIGH);
delay(1);
digitalWrite(8, LOW);
delay(1);
}
}
void loop () {}
change the argument to the delay...
for my refined control, try delayMicroseconds
void setup() {
int x;
// initialize the digital pin as an output:
pinMode(8, OUTPUT);
for (int i=0; i<1000; i++) {
digitalWrite(8, HIGH);
delayMicroseconds(250);
digitalWrite(8, LOW);
delayMicroseconds(250);
}
}
void loop () {}
tone
- http://www.arduino.cc/en/Tutorial/Tone
- Starting point is the Example > Digital > toneMelody
Important: tone is a unique command in that it is asynchronous, meaning that when you call the function it starts playing the tone, but immediately returns control to your program. In Bash terms, it's like starting the program with the '&' character at the end -- the command runs "in the background". This is important to know, because you need to use the delay command to play consecutive tones, otherwise each call to tone undoes the previous ones.
So, the wrong way would be:
int speakerPin = 13;
void setup() {
tone(speakerPin, 500, 100);
tone(speakerPin, 600, 100);
tone(speakerPin, 700, 100);
tone(speakerPin, 800, 100);
tone(speakerPin, 900, 100);
}
void loop() {}
And the right way is instead:
int speakerPin = 13;
void setup() {
tone(speakerPin, 500, 100);
delay(100);
tone(speakerPin, 600, 100);
delay(100);
tone(speakerPin, 700, 100);
delay(100);
tone(speakerPin, 800, 100);
delay(100);
tone(speakerPin, 900, 100);
delay(100);
}
void loop() {}
Clapping music.c
Now, using a C string with the Clapping music pattern.
int speakerPin = 8;
int thedelay = 100;
// char keycode = 'x';
char pat[] = "x xx xxx xxxx xxxxx ";
int patlen = strlen(pat);
void setup()
{
}
void loop ()
{
int i = 0;
while (i<patlen) {
if (pat[i] == 'x') tone(speakerPin, 400, 10);
delay(thedelay);
i = i+1;
}
// can be written in "shorthand" using the for statement...
/*
for (i=0; i<12; i+=1) {
if (pat[i] == 'x') tone(speakerPin, 400, 10);
delay(thedelay);
}
*/
delay(thedelay*10);
}
char pat[] = "xxx xx x xx ";
void setup() {
int patlen = strlen(pat);
for (int p=0; p<patlen; p++) {
if (pat[p] == 'x') {
tone(8, 500, 10);
}
delay(100);
}
}
void loop () {}
int speakerPin = 8;
int thedelay = 100;
// char keycode = 'x';
char pat[] = "x xx xxx xxxx xxxxx ";
int patlen = strlen(pat);
void setup()
{
}
void loop ()
{
int i = 0;
int i2 = 0;
while (i<patlen) {
i2 = i+5;
if (i2 >= patlen) i2 = i2 - patlen;
// maybe the % modulo operator could help!
if (pat[i] == 'x') tone(speakerPin, 400, 10);
delay(10);
if (pat[i2] == 'x') tone(speakerPin, 800, 10);
delay(thedelay);
i = i+1;
}
delay(thedelay*10);
}
Tone only technically supports playing one tone at a time; how might it be possible to still produce something like the 2 parts necessary for clapping music.