2015年8月12日 星期三

Arduino的教學06-利用蜂鳴器來發音

一、教學目標:蜂鳴器來演奏簡單音樂
https://www.arduino.cc/en/Tutorial/Tone
二、佈線圖

以tone(腳位,頻率,時間)函式播放
第一個參數是腳位
第二個參數是音符的頻率
第三個參數代表播放時間長度(ms)。

tone(6,224,500);



練習01-發出簡單三個音
#define tone_pin 8 //發聲的pin
#define NOTE_    0//

#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494


int melody[] = {
  NOTE_C4  , NOTE_D4  , NOTE_E4   
};

int noteDurations[] = {
  6, 16, 8
};


//函式,傳入歌曲的資料,進行播放。
void play(int *melody, int *noteDurations, int num){
  for(int note = 0; note < num; note++){
    int noteDuration = 3000 / noteDurations[note];
    tone(tone_pin, melody[note], noteDuration);

    delay(noteDuration * 1.30);
  }
}
void setup () {
  }

void loop(){
  play(melody, noteDurations, sizeof(melody) / sizeof(int));
  
  delay(2000);
}



三、程式碼
#define tone_pin 8 //發聲的pin
#define NOTE_    0//
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978


int melody[] = {
  NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, 
  NOTE_E4, NOTE_D4, NOTE_C4, NOTE_A3, NOTE_G3, NOTE_A3, NOTE_G3, 
  NOTE_A3, NOTE_A3, NOTE_C4, NOTE_A3, NOTE_C4, NOTE_D4, NOTE_E4, 
  NOTE_D4, NOTE_D4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, 
};

int noteDurations[] = {
  6, 16, 8, 8, 8, 8, 4, 
  6, 16, 8, 8, 8, 8, 4, 
  6, 16, 8, 8, 8, 8, 4,
  6, 16, 8, 8, 8, 8, 4,
};


//函式,傳入歌曲的資料,進行播放。
void play(int *melody, int *noteDurations, int num){
  for(int note = 0; note < num; note++){
    int noteDuration = 3000 / noteDurations[note];
    tone(tone_pin, melody[note], noteDuration);

    delay(noteDuration * 1.30);
  }
}
void setup () {
  }

void loop(){
  play(melody, noteDurations, sizeof(melody) / sizeof(int));
  
  delay(2000);
}

四、作業
1.請加入一個按鈕,按下時發出Do、Re、Mi的音,放開時發出Mi、Re、Do
2.請調整出一首兒歌或校歌

五、參考資料
http://yehnan.blogspot.tw/2012/02/arduinoloudspeaker.html

2015年8月6日 星期四

Arduino的教學05-可變電阻類比輸入與LED燈調速

一、教學目標:將LED燈依可變電阻調速
https://www.arduino.cc/en/Tutorial/AnalogInput

二、接線圖


/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);                  
}

練習01-如何將可變電阻的測量值-顯示除錯(Serial Monitor Debug)
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
 
void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}
 
void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);   
   Serial.println(sensorValue);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);
}

2015年8月5日 星期三

Arduino的教學04-霹靂燈與陣列宣告練習

一、教學目標:將LED燈依序點亮
http://ming-shian.blogspot.tw/2013/05/ardunioled_5.html

二、接線圖

三、程式碼

const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 5;    // the number of LEDs
int ledPins[] = { 
7, 8,9,10,11};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

void loop() {
// read the potentiometer:
 // int sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  //int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(100);
    digitalWrite(ledPins[thisLed], LOW);
  } 

  for (int thisLed = ledCount-1; thisLed >= 0; thisLed--) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(100);
    digitalWrite(ledPins[thisLed], LOW);

    if (thisLed ==0){
      digitalWrite(ledPins[thisLed], HIGH );
      delay(100);
    }
  }
}


四、作業練習:
如何利用可變電阻來調整霹靂燈速?

2015年8月4日 星期二

Arduino的教學03-讀取光敏電阻與序列埠輸出

一、教學目標:利用光敏電阻控制 LED燈:

http://yehnan.blogspot.tw/2012/02/arduino_23.html

二、接線圖


三、程式碼

int photocellPin = A0; // 光敏電阻 (photocell) 接在 anallog pin A0
int photocellVal = 0; // photocell variable
int minLight = 600;   // 最小光線門檻值
int ledPin = 13;
int ledState = 0; 
 
void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600);
}
 
void loop() {
  // 讀取光敏電阻並輸出到 Serial Port 
  photocellVal = analogRead(photocellPin);
  Serial.println(photocellVal);   
  
  // 光線不足時打開 LED
  if (photocellVal < minLight && ledState == 0) {
    digitalWrite(ledPin, HIGH); // turn on LED
    ledState = 1;
  }
  
  // 光線充足時關掉 LED
  if (photocellVal > minLight && ledState == 1) {
    digitalWrite(ledPin, LOW); // turn off LED
    ledState = 0;
  }  
  
  delay(100);       
}
練習-01-加入序列埠的除錯(serial moniter)
int photocellPin = A0; // 光敏電阻 (photocell) 接在 anallog pin A0
int photocellVal = 0; // photocell variable
int minLight = 800;   // 最小光線門檻值
int ledPin = 13;
int ledState = 0; 
int no=0; 
void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600);
}
 
void loop() {
  // 讀取光敏電阻並輸出到 Serial Port 
  photocellVal = analogRead(photocellPin);
   no++;
   Serial.print("test:");   
     Serial.print(no);
     Serial.print(":"); 
  Serial.println(photocellVal);   
 
  
  // 光線不足時打開 LED
  if (photocellVal < minLight && ledState == 0) {
    digitalWrite(ledPin, HIGH); // turn on LED
    ledState = 1;
  }
  
  // 光線充足時關掉 LED
  if (photocellVal > minLight && ledState == 1) {
    digitalWrite(ledPin, LOW); // turn off LED
    ledState = 0;
  }  
  
  delay(100);       
}
四、作業練習
1.請設定當光敏電阻的測量值小於設定值時亮紅色LED燈,否則亮綠燈。