2014年12月1日 星期一

Week12

接按鈕

void setup(){  pinMode(13,OUTPUT);  pinMode(2,INPUT);}void loop(){  if(digitalRead(2)==HIGH)  {    digitalWrite(13,HIGH);    tone(8,440,300);  }  else  {    digitalWrite(13,LOW);    noTone(8);  }}#2#include "pitches.h"const int buttonPin = 2;     // the number of the pushbutton pinconst int ledPin =  13;      // the number of the LED pin
// variables will change:int buttonState = 0;         // variable for reading the pushbutton status
// notes in the melody:int melody[] = {  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:int noteDurations[] = {  4, 8, 8, 4,4,4,4,4 };
void setup() {  // initialize the LED pin as an output:  pinMode(ledPin, OUTPUT);        // initialize the pushbutton pin as an input:  pinMode(buttonPin, INPUT);}
void loop() {  // read the state of the pushbutton value:  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.  // if it is, the buttonState is HIGH:  if (buttonState == HIGH) {         // turn LED on:        digitalWrite(ledPin, HIGH);  // iterate over the notes of the melody:  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // to calculate the note duration, take one second     // divided by the note type.    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.    int noteDuration = 1000/noteDurations[thisNote];    tone(8, melody[thisNote],noteDuration);
    // to distinguish the notes, set a minimum time between them.    // the note's duration + 30% seems to work well:    int pauseBetweenNotes = noteDuration * 1.30;    delay(pauseBetweenNotes);    // stop the tone playing:    noTone(8);  }    }   else {    // turn LED off:    digitalWrite(ledPin, LOW);    noTone(8);   }}
接蜂鳴器




沒有留言:

張貼留言