2020年2月7日 星期五

[Arduino] 測試按鈕

測試按鈕

目的:按下喇叭之後才能發出聲音
  1. 環境準備
  2. 額外材料
    • 無源蜂鳴器 *1
    • 麵包版 * 1
    • 傾斜開關 * 1
    • 電阻 * 1
    • 線材
      • 公對母 * 3
      • 公對公 * 3
  3. 程式
    • 開新檔案
    • 另存新檔
      • 檔名:02_DoReMi_Btn
      • 填入內容

const int buttonPin = 2;     // button pin
const int beePin =  13;      // bee pin
const int NOTE_C5 = 523;     // Do

void setup() {
  // initialize the BEE pin as an output:
  pinMode(beePin, OUTPUT);      
  // initialize the push button pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  
  // check if the push button is pressed.
  // if it is, the buttonState is HIGH:
  if (digitalRead(buttonPin) == HIGH) {     
      tone(13, NOTE_C5 , 500);
  } 
}

接法



挑戰
如果要按按鈕輸出一首音樂該怎麼做
#include "pitches.h"

int kunde[]={
  NOTE_G5,NOTE_E5,NOTE_E5,NOTE_F5,NOTE_D5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_G5,NOTE_G5,
  NOTE_G5,NOTE_E5,NOTE_E5,NOTE_F5,NOTE_D5,NOTE_D5,NOTE_C5,NOTE_E5,NOTE_G5,NOTE_G5,NOTE_C5};
int kunde_time[]={
  500,500,1000,500,500,1000,500,500,500,500,500,500,1500,
  500,500,1000,500,500,1000,500,500,500,500,1500};
int duration = 500;  // 500 milliseconds
const int buttonPin = 2;     // button pin
const int beePin =  13;      // bee pin

// variables will change:
int buttonState = 0;         // variable for reading the push button status

void setup() {
  // initialize the BEE pin as an output:
  pinMode(beePin, OUTPUT);      
  // initialize the push button pin as an input:
  pinMode(buttonPin, INPUT);     

}

void loop() {
  // read the state of the push button value:
  buttonState = digitalRead(buttonPin);

  // check if the push button is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
       
    for (int i= 0; i < 24; i++) 
    {
      // 在 pin13 上輸出聲音,每個音階響 duration 豪秒
      tone(beePin, kunde[i], kunde_time[i]);
      
      // 間隔一段時間後再播放下一個音階
      delay(kunde_time[i]+100);
    }
    // 兩秒後重新播放 
    delay(2000);
    totalCount=0;
  }
      

}

如果要按三次按鈕才輸出音樂呢
#include "pitches.h"

int kunde[]={
  NOTE_G5,NOTE_E5,NOTE_E5,NOTE_F5,NOTE_D5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_G5,NOTE_G5,
  NOTE_G5,NOTE_E5,NOTE_E5,NOTE_F5,NOTE_D5,NOTE_D5,NOTE_C5,NOTE_E5,NOTE_G5,NOTE_G5,NOTE_C5};
int kunde_time[]={
  500,500,1000,500,500,1000,500,500,500,500,500,500,1500,
  500,500,1000,500,500,1000,500,500,500,500,1500};
int duration = 500;  // 500 milliseconds
int totalCount =0; 
const int buttonPin = 2;     // button pin
const int beePin =  13;      // bee pin

// variables will change:
int buttonState = 0;         // variable for reading the push button status

void setup() {
  // initialize the Bee pin as an output:
  pinMode(beePin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

}

void loop() {
  // read the state of the push button value:
  buttonState = digitalRead(buttonPin);

  // check if the push button is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
        totalCount++;
        delay(500);
  } 
  
  if(totalCount==3)  
  {
    for (int i= 0; i < 24; i++) 
    {
      // 在 pin13 上輸出聲音,每個音階響 duration 豪秒
      tone(beePin, kunde[i], kunde_time[i]);
      
      // 間隔一段時間後再播放下一個音階
      delay(kunde_time[i]+100);
    }
    // 兩秒後重新播放 
    delay(2000);
    totalCount=0;
  }
      

}


沒有留言:

張貼留言