เล่นเพลง Star War Buzzer

const int c = 261; //ความถี่โนต
const int d = 294; //ความถี่โนต
const int e = 329; //ความถี่โนต
const int f = 349; //ความถี่โนต
const int g = 391; //ความถี่โนต
const int gS = 415; //ความถี่โนต
const int a = 440; //ความถี่โนต
const int aS = 455; //ความถี่โนต
const int b = 466; //ความถี่โนต
const int cH = 523; //ความถี่โนต
const int cSH = 554; //ความถี่โนต
const int dH = 587;  //ความถี่โนต
const int dSH = 622; //ความถี่โนต
const int eH = 659;  //ความถี่โนต
const int fH = 698; //ความถี่โนต
const int fSH = 740; //ความถี่โนต
const int gH = 784; //ความถี่โนต
const int gSH = 830; //ความถี่โนต
const int aH = 880; //ความถี่โนต

const int buzzerPin = 8; //กำหนดขา buzzer
const int ledPin1 = 12; //กำหนดขา led
const int ledPin2 = 13; //กำหนดขา led

int counter = 0; //เก็บค่าโนต

void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT); //กำหนด buzzer ออก
  pinMode(ledPin1, OUTPUT);   //กำหนด led ออก
  pinMode(ledPin2, OUTPUT);   //กำหนด led ออก
}

void loop()//เล่นโนตเพลง
{

  //Play first section
  firstSection();

  //Play second section
  secondSection();

  //Variant 1
  beep(f, 250); 
  beep(gS, 500); 
  beep(f, 350); 
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375); 
  beep(cH, 125);
  beep(eH, 650);

  delay(500);

  //Repeat second section
  secondSection();

  //Variant 2
  beep(f, 250); 
  beep(gS, 500); 
  beep(f, 375); 
  beep(cH, 125);
  beep(a, 500); 
  beep(f, 375); 
  beep(cH, 125);
  beep(a, 650); 

  delay(650);
}

void beep(int note, int duration) //กระพิบ led ตามจังหวะเพลง
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);

  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }

  //Stop tone on buzzerPin
  noTone(buzzerPin);

  delay(50);

  //Increment counter
  counter++;
}

void firstSection() //เล่นเพลงตามโนตที่กำหนดไว้
{
  beep(a, 500);
  beep(a, 500);   
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150); 
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500); 
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);
}

void secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);   
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325); 
  beep(cSH, 175); 
  beep(cH, 125); 
  beep(b, 125); 
  beep(cH, 250); 

  delay(350);
}

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

การอินเตอร์เฟสเบื้องต้น

บทที่4

ESP8266 เชื่อมต่อ WIFI