2020年11月30日 星期一

WEEK12

 WEEK12

安裝驅動程式




下載原始程式檔

https://github.com/CytronTechnologies/MakerUno_Examples

#define NOTE_C5 523

#define NOTE_E5 659

#define NOTE_G5 784


#define BUTTON 2

#define BUZZER 8


int melody[] = {

  NOTE_E5, NOTE_E5, 0, NOTE_E5, 0, NOTE_C5, NOTE_E5, 0, NOTE_G5

};


int noteDurations[] = {

  10, 10, 10, 10, 10, 10, 10, 10, 10

};


int pin;

int ledArrayHigh;

int ledArrayLow;

boolean mode = false;

boolean buttonPressed = false;


void setup()

{

  delay(1000);

  

  pinMode(BUTTON, INPUT_PULLUP);

  for (pin = 3; pin < 14; pin++) { 

    pinMode(pin, OUTPUT);

  }


  for (int thisNote = 0; thisNote < 9; thisNote++) {

    int noteDuration = 1000 / noteDurations[thisNote];

    tone(BUZZER, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;

    delay(pauseBetweenNotes);

    noTone(BUZZER);

  }

}


void loop()

{

  for (pin = 0; pin < 5; pin++) {

    if (digitalRead(BUTTON) == LOW &&

        buttonPressed == false) {

      buttonPressed = true;

      mode = !mode;

      pin = 0;

      if (mode == false) {

        tone(BUZZER, NOTE_C5, 100);

        delay(100);

        tone(BUZZER, NOTE_G5, 100);

        delay(100);

        noTone(BUZZER);

      }

      else if (mode == true) {

        tone(BUZZER, NOTE_G5, 100);

        delay(100);

        tone(BUZZER, NOTE_C5, 100);

        delay(100);

        noTone(BUZZER);

      }

    }


    if (mode == false) {

      ledArrayHigh = 13 - pin;

      ledArrayLow = 7 - pin;

    }

    else if (mode == true) {

      ledArrayHigh = 9 + pin;

      ledArrayLow = 3 + pin;

    }

    digitalWrite(ledArrayHigh, HIGH);

    digitalWrite(ledArrayLow, HIGH);

    delay(100);

    digitalWrite(ledArrayHigh, LOW);

    digitalWrite(ledArrayLow, LOW);

 
  if (pin == 4) delay(100);

  }

  if (buttonPressed == true) {

    buttonPressed = false;

  }

}

小鋼琴









7個音色



沒有留言:

張貼留言