Katse 6.1
Komponendid:
Arduino UNO 1 tk.
Arendusplaat 1 tk.
Juhtmed 4 tk.
Buzzer 1 tk.

code:
const int buzzerPin = 9;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 150;
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10);
}
while(true){}
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
Ülesanne Muusikaline piiksutaja
Tänu potentsiomeetri režiimidele (1 – 3) mängib see erinevaid meloodiaid.
1. “aaggeedeggaff”
2. “agfggafddceda”
3. “fdegaddgeeade”
Komponendid:
Arduino UNO 1 tk.
Arendusplaat 1 tk.
Juhtmed 19 tk.
Potensiomeetr 1 tk.
Buzzer 1 tk.

code:
const int buzPin = 9;
const int songLength = 18;
int sensorPin = 0;
int sensorValue = 0;
char music1[] = "aaggeedeggaff";
int beats1[] = {1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2};
char music2[] = "agfggafddceda";
int beats2[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
char music3[] = "fdegaddgeeade";
int beats3[] = {1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2};
int tempo = 150;
void setup()
{
Serial.begin(9600);
pinMode(buzPin, OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, 0, 1023, 1, 3);
Serial.println(sensorValue);
if (sensorValue == 1)
{
play_Song(music1, beats1);
}
else if (sensorValue == 2)
{
play_Song(music2, beats2);
}
else if (sensorValue == 3)
{
play_Song(music3, beats3);
}
}
void play_Song(char notes[], int beats[])
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo / 10);
}
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return frequencies[i];
}
}
return 0;
}