Vous êtes sur la page 1sur 3

const int buzzerPin = 8;

const int RED_PIN = 9;


const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
const int songLength = 167;

char notes[] = "gFeeF sgFeeFdesseFgeCdessF gFeeF sgFeeFdes eFgeCdesdefedc


skcfeddcdcccskcfgfeddeffgaAAagfgaagfdcdffeeFF aaabaFdgaa aaabaFdgaa aaabaFdgaa
aaabaFdgaa aaabaFdgaa gFF"; // a space represents a rest
int beats[] =
{1,1,1,1,4,7,1,1,1,1,2,3,2,1,8,1,2,1,3,1,2,3,2,1,2,7,4,1,1,1,1,4,7,1,1,1,1,3,1,3,1,5,4,2,1,3,1,3,1,2,
1,1,1,1,1,1,1,2,1,1,2,2,1,1,1,1,1,1,2,2,1,1,2,2,1,1,1,1,1,1,2,2,1,1,1,1,2,2,1,1,1,1,2,2,1,1,1,1,1,2,1,
1,5,1,1,1,1,1,1,1,2,1,1,5,1,1,1,1,1,1,1,2,1,1,5,1,1,1,1,1,1,1,2,1,1,5,1,1,1,1,1,1,1,2,1,1,5,1,1,1,1,1,
1,1,2,1,1,3,3,1,1,3,3,1,1,5};
int tempo = 200;

void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
int i, duration;
int LEDcolor;
for (i = 0; i < songLength; i++)
{
LEDcolor = random(4);
if (LEDcolor == 0) {
yellow();
}
if (LEDcolor == 1) {
blue();
}
if (LEDcolor == 2) {
green();
}
if (LEDcolor == 3) {
purple();
}

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 = 23;

char names[] = {'c','d','e','f','g','a','b','C','D','F','G','A','1','2','3','4','5','6','7','8','9','s','k'};


int frequencies[] =
{261,293,329,349,392,440,494,277,311,370,415,466,523,554,587,622,659,698,740,783,830,22
0,233};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}

void yellow() {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}

void blue() {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

void purple() {
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);

void green() {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);

Vous aimerez peut-être aussi