📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้วงจร
📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้คำสั่งเขียน
const int RED_PIN = 12;
const int GREEN_PIN = 10;
const int WHITE_PIN = 11;
const int buzzerPin = 13;
const int songLength = 99;
const int buttonPin = 8;
int buttonState = 0;
int counter = 0;
char names[] = {'D', 'E', 'F', 'G', 'A', 'B', 'C', 'd', 'e', 'f', 'g', 'a', 'b', 'c'};
int frequencies[] = {587, 659, 699, 784, 880, 988, 1047, 1175, 1319, 1397, 1568, 1760, 1976, 2093};
char notes[] = "cffefaagCCCvagacffefaagCffeedcCCCfvaagCCCfvaagDDDCvavgavCffgaDDDCvavgavCffgf ";
int beats[] = {2,2,3,1,2,2,2,2,2,2,3,1,2,2,4,2,2,3,1,2,2,2,2,2,2,3,1,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,1,1,3,1,2,2,4,3,1,2,2,2,2,4,2,1,1,3,1,2,2,4,8};
int tempo = 100;
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(WHITE_PIN, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
counter++;
buttonState = 0;
}
else
{
Serial.println("Nothing happened!");
}
if(counter == 1)
{
playMusic();
}
else if(counter == 2)
{
Serial.println("Add a new song here!");
}
else
{
counter = 0;
}
}
void playMusic()c
{
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);
if (frequency(notes[i]) <= 1047)
{
digitalWrite(RED_PIN, HIGH);
delay(duration);
}
else if (frequency(notes[i]) <= 1319)
{
digitalWrite(GREEN_PIN, HIGH);
delay(duration);
}
else
{
digitalWrite(WHITE_PIN, HIGH);
delay(duration);
}
}
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(WHITE_PIN, LOW);
delay(tempo/2);
}
while(true)
noTone(buzzerPin);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(WHITE_PIN, LOW);
}
}
int frequency(char note)
{
int i;
const int numNotes = 14;
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
} return(0);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น
ครูเต้ง เทคโนฯ