2012年12月6日 星期四

Week13,Hw11

1.程式碼

int inpin1=6;
int inpin2=7;
int inpin3=9;
int inpin4=3;
int inpin5=1;


int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
 
pinMode(inpin1, INPUT);
pinMode(inpin2, INPUT);
pinMode(inpin3, INPUT);
pinMode(inpin4, INPUT);
pinMode(inpin5, INPUT);


}


void loop() {
  buttonState1 = digitalRead(inpin1);
  buttonState2 = digitalRead(inpin2);
  buttonState3 = digitalRead(inpin3);
  buttonState4 = digitalRead(inpin4);
  buttonState5 = digitalRead(inpin5);

  if (buttonState1 == HIGH)  tone(8, 524,4);
  else if (buttonState2 == HIGH)  tone(8, 588,4);
  else if (buttonState3 == HIGH)  tone(8, 660,4);
  else if (buttonState4 == HIGH)  tone(8, 698,4);
  else if (buttonState5 == HIGH)  tone(8, 784,4);
  else {
  noTone(9);
  }
 
 
 
 
}


2.影片
 

2012年12月4日 星期二

Week12,HW11


#include "pitches.h"

const int threshold = 10;    // minimum reading of the sensors that generates a note

// notes to play, corresponding to the 3 sensors:
int notes[] = {
  NOTE_A4, NOTE_B4,NOTE_C3,NOTE_D4,NOTE_E4 };

void setup() {
 
 pinMode(3, INPUT);
 pinMode(4, INPUT);  
 pinMode(5, INPUT);
 pinMode(6, INPUT);
 pinMode(7, INPUT);
 Serial.begin(9600);
}

void loop() {
  for (int thisSensor = 3; thisSensor < 8; thisSensor++) {
    // get a sensor reading:
    int sensorReading = digitalRead(thisSensor);

    // if the sensor is pressed hard enough:
    if (digitalRead(thisSensor) ==HIGH) {
      // play the note corresponding to this sensor:
      tone(8, notes[thisSensor], 20);
      Serial.println(thisSensor, DEC);
    }else if(digitalRead(thisSensor) ==LOW){
    noTone(8);
  }
   delay(20);
   
  }
}
心得:多虧老師的指點才得以debug,不過做完之後有點得意忘形就是了 呵呵

hw11,week13


1.將Arduino做出的鋼琴錄影上傳到youtube,再貼回部落格

















2.發揮你的創意做出最特別的音樂


















我們有成功做出鋼琴的五個按鈕,每個按鍵代表每個音階,
然後我們就異想天開的加入了旋鈕想要改變音階的變化,
因為我們認為按鍵數量不夠,不知道能不能利用程式的撰寫,
利用旋鈕來改變按鈕的音階,讓五個按鈕可以各代表兩個音階,
這樣音域就會變廣,我們就朝這個方向邁進,但是最後試出來的結果,
真的讓我們覺得有些小失望,因為改變旋鈕後,它只是改變發聲的頻率,
並不如預期的改變其音階,還有很可惜沒有拍成影片,其實這次失敗的計畫中,
我們有學到旋鈕的配合真正的用處在哪邊,並且了解這樣的方式並不可行。



3.貼上程式碼



*/
#include "pitches.h"

const int buttonPin1 = 3; // the number of the pushbutton pin
const int buttonPin2 = 4; 
const int buttonPin3 = 5; 
const int buttonPin4 = 6; 
const int buttonPin5 = 7; 

const int ledPin = 8; // the number of the LED pin

// variables will change:
int buttonState1 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; 

// 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() {
// iterate over the notes of the melody:
// initialize the LED pin as an output:
pinMode(8, OUTPUT); 
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);

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];


// 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);
}
}

void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) { 
// turn LED on: 
tone(8,NOTE_C3,4);
digitalWrite(8, HIGH); 
} 


else if (buttonState2 == HIGH) { 
// turn LED on: 
tone(8,NOTE_D3,4);
digitalWrite(8, HIGH); 
} 


else if (buttonState3 == HIGH) { 
// turn LED on: 
tone(8,NOTE_E3,4);
digitalWrite(8, HIGH); 
} 


else if (buttonState4 == HIGH) { 
// turn LED on: 
tone(8,NOTE_F3,4);
digitalWrite(8, HIGH); 
} 

else if (buttonState5 == HIGH) { 
// turn LED on: 
tone(8,NOTE_G3,4);
digitalWrite(8, HIGH); 
} 
else {
// turn LED off:
digitalWrite(8, LOW); 
}

}

4.本周上課心得

今天利用到五個按鈕,做出鋼琴的樣子,其實一開始還是有卡關,
在於程式部分和案件硬體的理解稍微慢了一些些,
硬體的輸出腳以及程式的搭配,要需要小心一點,
因為很容易就被卡住,所以我們這次學到了必須先建立好程式邏輯才開始動手
我們之後練習的步驟就打算利用這樣的做法來試試看,
已有最有效率的方法達到最好的學習成效,
很開心我這周又學到了一點東西。




HW11,Week13

Week11, HW09

作業一:
Arduino可以做甚麼?

作業二:
Arduino相關影片

week13,HW11

1. 程式碼

int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2);
  }
}

98160860黃麒, 98163021張耀璇, HW11, Week13,

int inpin1=3;
int inpin2=4;
int inpin3=5;
int inpin4=6;
int inpin5=7;

int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
  pinMode(inpin1, INPUT);
 pinMode(inpin2, INPUT);
 pinMode(inpin3, INPUT);
 pinMode(inpin4, INPUT);
 pinMode(inpin5, INPUT);
}

void loop() {
  buttonState1 = digitalRead(inpin1);
  buttonState2 = digitalRead(inpin2);
  buttonState3 = digitalRead(inpin3);
  buttonState4 = digitalRead(inpin4);
  buttonState5 = digitalRead(inpin5);

  if (buttonState1 == HIGHtone(8, 524,4);
  else if (buttonState2 == HIGHtone(8, 588,4);
  else if (buttonState3 == HIGHtone(8, 660,4);
  else if (buttonState4 == HIGHtone(8, 698,4);
  else if (buttonState5 == HIGHtone(8, 784,4);
  else {
  noTone(8);
  }
}


/*
  keyboard

 Plays a pitch that changes based on a changing analog input

 circuit:
 * 3 force-sensing resistors from +5V to analog in 0 through 5
 * 3 10K resistors from analog in 0 through 5 to ground
 * 8-ohm speaker on digital pin 8

 created 21 Jan 2010
 modified 9 Apr 2012
 by Tom Igoe
This example code is in the public domain.

 http://arduino.cc/en/Tutorial/Tone3

 */
#include "pitches.h"
const int threshold = 10;    // minimum reading of the sensors that generates a note
// notes to play, corresponding to the 3 sensors:
int notes[] = {
  NOTE_A4, NOTE_B4,NOTE_C3 };
void setup() {
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
Serial.begin(9600);
}
void loop() {
  for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
    // get a sensor reading:
    int sensorReading = analogRead(thisSensor);
    // if the sensor is pressed hard enough:
    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:
      tone(8, notes[thisSensor], 20);
      Serial.println(DEC);
    }
  }
}

Week12, HW11,

利用arduino 做出一個小鋼琴感覺很棒 :))
//#define  c     3830    // 261 Hz
//#define  d     3400    // 294 Hz
//#define  e     3038    // 329 Hz
//#define  f     2864    // 349 Hz
//#define  g     2550    // 392 Hz
//#define  a     2272    // 440 Hz
//#define  b     2028    // 493 Hz
//#define  C     1912    // 523 Hz
//#define  R     0
// #include "pitches.h"
const int inPin1 = 7;  
const int inPin2 = 6;
const int inPin3 = 5;  
const int inPin4 = 4;
const int inPin5 = 3;
int val1 = 0;    
int val2 = 0;
int val3 = 0;    
int val4 = 0;
int val5 = 0;

void setup() {
//pinMode(ledPin, OUTPUT);
pinMode(inPin1, INPUT);   
pinMode(inPin2, INPUT);
pinMode(inPin3, INPUT);   
pinMode(inPin4, INPUT);
pinMode(inPin5, INPUT);

}
void loop() {
val1 = digitalRead(inPin1);
val2 = digitalRead(inPin2);
val3 = digitalRead(inPin3);
val4 = digitalRead(inPin4);
val5 = digitalRead(inPin5);
// Serial.println(val);
// delay(100);
//tone = 2028;
if(val1==HIGH){
tone(8,524,8);
}
if(val2==HIGH){
tone(8,588,8);
}
if(val3==HIGH){
tone(8,660,8);
}
if(val4==HIGH){
tone(8,698,8);
}
if(val5==HIGH){
tone(8,784,8);
}
}

Week13,HW11

(1)Demo影片


(2)程式碼
int buttonPin = 2;
int buttonPin1 = 3;
int buttonPin2 = 4;
int buttonPin3 = 5;
int buttonPin4 = 6;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

void setup() {
  pinMode(buttonPin, INPUT); 
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
 
  if (buttonState == HIGH) {    
    tone(9,261,100); 
  }
  if (buttonState1 == HIGH) {
    tone(9,294,100);
  }
  if (buttonState2 == HIGH) {
    tone(9,329,100);
  }
  if (buttonState3 == HIGH) {
    tone(9,349,100);
  }
  if (buttonState4 == HIGH) {
    tone(9,392,100);
  }
 
}

Week11,HW09




我覺得兩個結合可以做一些身心健康的遊戲,例如體感遊戲,用簡單的Arduino硬體可以做出來,再配合processing真是簡單又有趣。

Week13, Hw11

1. 瑪莉有隻小綿羊


2.我們的厲害樂譜

3212333 222 355
3212333 22321

3345  5432  1123 322
3345  5432 1123 211

3. 程式碼
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin5 = 5;
int buttonPin6 = 6;
int buttonState2 = 0;
int buttonState3 = 0; 
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0; 
void setup() {
  pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(buttonPin5, INPUT);
 pinMode(buttonPin6, INPUT); 
  Serial.begin(9600);
}
void loop() {
  buttonState2 = digitalRead(buttonPin2);
  buttonState3= digitalRead(buttonPin3);
  buttonState4= digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
  if (buttonState2 == HIGH) {    
    Serial.println("Start Playing");
   tone(8, 262, 10);
  }
  if (buttonState3 == HIGH) {    
    Serial.println("Start Playing");
   tone(8, 294, 10);
  }
  if (buttonState4 == HIGH) {    
    Serial.println("Start Playing");
   tone(8, 330, 10);
  }
  if (buttonState5 == HIGH) {    
    Serial.println("Start Playing");
   tone(8, 349, 10);
  }
  if (buttonState6 == HIGH) {    
    Serial.println("Start Playing");
   tone(8, 392, 10);
  }
}

4.這次的arduino我覺得超酷超有趣的,但是在接線時有點複雜,不過我們還是突破了,所以我們超有成就感,自己做出一座簡易鋼琴的感覺超妙的,讓我回憶起高中時作詞作曲的歲月,所以繼續加油吧 大家!

HW11,Week13

[程式碼]



[影片]

HW11,Week13

int buttonPin1 = 2;     // the number of the pushbutton pin
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int buttonState1 = 0;       
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int speakerPin = 9;
void setup() {
   pinMode(buttonPin1, INPUT);    
   pinMode(buttonPin2, INPUT);
   pinMode(buttonPin3, INPUT);
   pinMode(buttonPin4, INPUT);
   pinMode(buttonPin5, INPUT);
}

void loop(){
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
   buttonState3 = digitalRead(buttonPin3);
   buttonState4 = digitalRead(buttonPin4);
   buttonState5 = digitalRead(buttonPin5);
  
   if (buttonState1 == HIGH) { 
     tone(9, 262,100);  
   }
   else { }
  
   if (buttonState2 == HIGH) { 
     tone(9, 294,100);  
   }
   else { }
   if (buttonState3 == HIGH) { 
     tone(9, 330,100);  
   }
   else { }
   if (buttonState4 == HIGH) { 
     tone(9, 349,100);  
   }
   else { }
   if (buttonState5 == HIGH) { 
     tone(9, 392,100);  
   }
   else { }
}

HW11,Week13

簡易鋼琴:


int val[6];
void setup(){
  for(int i=2;i<7;i++) pinMode(i, INPUT_PULLUP);
}
void loop(){
  for(int i=2;i<7;i++) val[i-2] = digitalRead(i);
  for(int i=0;i<6;i++)
    if (val[i] == LOW) {
      switch(i){
        case 0 : tone(8,262,4); break;
        case 1 : tone(8,294,4); break;
        case 2 : tone(8,330,4); break;
        case 3 : tone(8,349,4); break;
        case 4 : tone(8,392,4); break;
      }
    }
}





可變音調:

int val[6];
int sensorPin = A0;
int sensorValue = 0;
void setup(){
  for(int i=2;i<7;i++) pinMode(i, INPUT_PULLUP);
}
void loop(){
  sensorValue = analogRead(sensorPin); 
  for(int i=2;i<7;i++) val[i-2] = digitalRead(i);
  for(int i=0;i<6;i++)
    if (val[i] == LOW) {
      switch(i){
        case 0 : tone(8,262+sensorValue,4); break;
        case 1 : tone(8,294+sensorValue,4); break;
        case 2 : tone(8,330+sensorValue,4); break;
        case 3 : tone(8,349+sensorValue,4); break;
        case 4 : tone(8,392+sensorValue,4 ); break;
      }
    }
}



心得:
  今天終於學習到如何將arduino當作輸入,與processing 結合,當然,也代表我的期末做也可以開工了。其實心裡比較希望能有多一點的板子(哭),或許可以將很多作品都留著,例如我可愛的音樂遊戲,就可以使用這個當作input端。抑或是先前提到的紅外線感應遊戲。簡單來講,今天所學習到的真是一個很有趣部分,而學習的部分真的不多,但已經感覺能有很大的空間來製作一些不一樣的作品。

2. 我們做了持續閃爍的七彩霓虹燈

3. 程式碼
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 7; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(6, HIGH);
delay(100);
digitalWrite(5, HIGH);
delay(100);
digitalWrite(4, HIGH);
delay(100);
digitalWrite(3, HIGH);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(1, HIGH);
delay(100);

digitalWrite(7, LOW);
delay(100);
digitalWrite(6, LOW);
delay(100);
digitalWrite(5, LOW);
delay(100);
digitalWrite(4, LOW);
delay(100);
digitalWrite(3, LOW);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(1, LOW);
delay(100);

delay(50);
}

Week 13, HW11

1. 自動撥音樂(整首小蜜蜂)

int speakerPin = 9;

int length = 24; // the number of notes
char notes[] = "geefddcdefggggeefddceggc "; // a space represents a rest
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 
2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4};
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2); 
  }
}

2.可用按鍵彈音階

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;

int speakerPin = 9;

int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
  
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);


}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  int duration;
  
  if (buttonState1 == HIGH)  tone(speakerPin, 261,duration); //tone(pin, frequency, duration) 
  else if (buttonState2 == HIGH)  tone(speakerPin, 294,duration);
  else if (buttonState3 == HIGH)  tone(speakerPin, 329,duration);
  else if (buttonState4 == HIGH)  tone(speakerPin, 349,duration);
  else if (buttonState5 == HIGH)  tone(speakerPin, 392,duration);
  else {
  noTone(speakerPin);
  }
} 
 
3.心得
這節課教我們用 Arduino 撥放音樂,還有搭配按鈕彈音階, 讓我想到可以做鋼琴的小遊戲。
而且他的程式碼跟原裡沒有很難,董元李後就可以延伸很多不同的曲子。
可惜這節課當時沒有借到手機或相機,沒有把接出來的執行樣子拍攝下來,
有機會再用得獎的Arduino 再接一次~

Week13,HW10


int inpin1=3;
int inpin2=4;
int inpin3=5;
int inpin4=6;
int inpin5=7;


int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {

pinMode(inpin1, INPUT);
pinMode(inpin2, INPUT);
pinMode(inpin3, INPUT);
pinMode(inpin4, INPUT);
pinMode(inpin5, INPUT);


}

void loop() {
  buttonState1 = digitalRead(inpin1);
  buttonState2 = digitalRead(inpin2);
  buttonState3 = digitalRead(inpin3);
  buttonState4 = digitalRead(inpin4);
  buttonState5 = digitalRead(inpin5);

  if (buttonState1 == HIGH)  tone(9, 524,4);
  else if (buttonState2 == HIGH)  tone(9, 588,4);
  else if (buttonState3 == HIGH)  tone(9, 660,4);
  else if (buttonState4 == HIGH)  tone(9, 698,4);
  else if (buttonState5 == HIGH)  tone(9, 784,4);
  else {
  noTone(9);
  }
}



Week13,HW11

互動技術概論 Arduino 按鍵音樂 

程式碼


int inpin1=3;
int inpin2=4;
int inpin3=5;
int inpin4=6;
int inpin5=7;


int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
  
pinMode(inpin1, INPUT);
pinMode(inpin2, INPUT);
pinMode(inpin3, INPUT);
pinMode(inpin4, INPUT);
pinMode(inpin5, INPUT);


}

void loop() {
  buttonState1 = digitalRead(inpin1);
  buttonState2 = digitalRead(inpin2);
  buttonState3 = digitalRead(inpin3);
  buttonState4 = digitalRead(inpin4);
  buttonState5 = digitalRead(inpin5);

  if (buttonState1 == HIGH)  tone(9, 524,4);
  else if (buttonState2 == HIGH)  tone(9, 588,4);
  else if (buttonState3 == HIGH)  tone(9, 660,4);
  else if (buttonState4 == HIGH)  tone(9, 698,4);
  else if (buttonState5 == HIGH)  tone(9, 784,4);
  else {
  noTone(9);
  }
  
  
  
  
}

week13,hw11







程式碼

int buttonPin = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin5 = 5;
int buttonPin6 = 6;
int buttonState = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; 
int buttonState6 = 0; 
void setup() {
  pinMode(buttonPin, INPUT);    
  pinMode(buttonPin3, INPUT);    
  pinMode(buttonPin4, INPUT);    
  pinMode(buttonPin5, INPUT);    
  pinMode(buttonPin6, INPUT);    
}
void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
 
  if (buttonState == HIGH) {    
   tone(8, 262, 10);
  }


  if (buttonState3== HIGH) {    
   tone(8, 294, 10);
  }


  if (buttonState4 == HIGH) {    
   tone(8, 330, 10);
  
  }


  if (buttonState5 == HIGH) {    
   tone(8, 349, 10);
  }


  if (buttonState6 == HIGH) {    
   tone(8, 392, 10);
  }

}


Week13 , HW11

作業一:
Tutorial Melody
程式碼 ↓

int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2); 
  }
}
影片 ↓


作業二:
Tone Melody
程式碼↓
/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"

const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;

int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;


// 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() {
  
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);  
   
  pinMode(8, OUTPUT);
  // 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);
  }
}

void loop() {
    buttonState3 = digitalRead(buttonPin3); 
    buttonState4 = digitalRead(buttonPin4); 
    buttonState5 = digitalRead(buttonPin5); 
    buttonState6 = digitalRead(buttonPin6); 
    buttonState7 = digitalRead(buttonPin7); 
    
   if(buttonState3 == HIGH){
      tone(8, 261,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState4 == HIGH){
      tone(8,294,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState5 == HIGH){
      tone(8,329,4);0
      digitalWrite(8, HIGH); 
    }
    if(buttonState6 == HIGH){
      tone(8,349,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState7 == HIGH){
      tone(8,400,4);
      digitalWrite(8, HIGH); 
    }

  
  // no need to repeat the melody.
}


影片 ↓