2015年11月28日 星期六

Arduino的教學09-使用I2C 2X16 lcd


一、學習目標:學習使用I2C 2X16 lcd
i2c為一種通訊界面,2x16代表lcd為二行,每行可以顯示16個字母。

二、接線圖


SDA – 接 Arduino 的 Analog Pin 4 (Arduino Mega 為 Pin 20)
SCL – 接 Arduino 的 Analog Pin 5 (Arduino Mega 為 Pin 21)
GND – 接 GND
VCC – 接 +5V

三、參考資料:
http://coopermaa2nd.blogspot.tw/2012/09/i2c-16x2-lcd.html

四、程式碼
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);//第2列,空2格開始
  lcd.print("second line");
}

void loop()
{
}
練習一:如何每秒測量一次,利出
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);  
 
int no=0;//計數器
void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);//第2列,空2格開始
  lcd.print("sceond line");
  delay(3000);//等3秒
}
 
void loop(){
 no++;
 
   //資料顯示在LCD
  lcd.setCursor(0, 0);
  lcd.print("No.");
  lcd.print(no);
  delay(500);            //每500ms更新一次  
  lcd.clear();
 
}
五、作業:
如何將 HC-SR04 超聲波模組測量結果,以lcd顯示。

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x27,16,2);


int number=0;
#define TrigPIN 13
#define EchoPIN 12

void setup() {
  pinMode(TrigPIN, OUTPUT);
  pinMode(EchoPIN, INPUT);
    lcd.init();  // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1, 0);//第1列,空1格開始
  lcd.print("Hello,HC_SR04!");

  //序列埠除錯用
  Serial.begin(9600);
  Serial.println("Hello, HC_SR04!");
  delay(1000);
}
void loop() {
  float duration, distance;//時間、距離
  digitalWrite(TrigPIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TrigPIN, LOW);
  duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
  distance = (duration/2)/29;

  number = number +1;

  lcd.setCursor (0,0);
  lcd.print("No.");
  lcd.print (number);
  lcd.setCursor(0, 1);  
  lcd.print ("Dis.");   
  lcd.print(distance);
  lcd.print ("cm");
  
  
  Serial.print("No.");
  Serial.print (number);
  Serial.print (" Dis. ");   
  Serial.print(distance);
  Serial.println (" cm");//換行
  
  delay(1000);
  lcd.clear();
}
 

畫愛心的方法
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};



void setup() {

  Serial.begin(9600);
  lcd.init();                      // initialize the lcd

  // Print a message to the LCD.
  lcd.backlight();


  lcd.createChar(0, heart);
}

void loop() {

lcd.printByte(0);

delay(1000);
  lcd.clear();

}

2015年10月3日 星期六

Arduino的教學08-利用Tera Term 來收集序列埠資料

一、教學目標:利用Tera Term 來收集序列埠(com port )資料

二、Tera Term 可以在底下的網址取得
http://ttssh2.sourceforge.jp/

三、參考資料:
http://coopermaa2nd.blogspot.tw/2012/01/tera-term.html

四、學生作業:
1.請利用tera term來收集
的資料

Arduino的教學07-利用HC-SR04 超音波模組測量距離

一、教學目標:HC-SR04 超音波模組測量距離-led閃爍


Working Voltage: DC 5 V 
Working Current: 15mA 
Working Frequency: 40Hz 
Max Range: 4m 
Min Range: 2cm 
MeasuringAngle: 15 degree 
Trigger Input Signal: 10uS TTL pulse 
Echo Output Signal: Input TTL lever signal and the range in proportion 
Dimension: 45*20*15mm

二、佈線圖



三、程式碼

#define TrigPIN 13
#define EchoPIN 12
#define LED1 8
#define LED2 9

void setup() {
  pinMode(TrigPIN, OUTPUT);
  pinMode(EchoPIN, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
    float duration, distance;
  digitalWrite(TrigPIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TrigPIN, LOW);
  duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
  distance = (duration/2)/29;

  
  if (distance <= 100) {
    digitalWrite(LED1, HIGH);
    delay(distance*1.5 + 10);
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    delay(distance*1.5 + 10);
    digitalWrite(LED2, LOW);
  }
  
  delay(100);
}


四、作業
1.請將此系統改裝成車雷達(小於一段距離時)產生聲音

五、參考資料
https://eportal.stust.edu.tw/eshare/EshareFile/2014_5/2014_5_c185d20a.pdf
http://blog.lyhdev.com/2012/10/arduino-1hc-sr04.html

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燈,否則亮綠燈。

2015年7月31日 星期五

Arduino的教學02-控制 LED 燈光亮度與PWM

一、教學目標:控制LE燈亮度(呼吸燈)


三、程式碼

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
} 

void loop()  { 
  // set the brightness of pin 9:
  analogWrite(9, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}
四、作業練習 1.如何讓呼吸燈的變化頻率快一點?

2015年7月29日 星期三

Arduino的教學01-arduino的介紹

一、Arduino簡介
二、實驗器材清單
三、麫包板
         http://www.letry.com.tw/letryhandbookc/handbook.htm
三、第一支程式-Blinking LED
四、作業

  1. 請用麫包板接出二個串聯的LED燈(LED不可以直接接電源會燒毀,須串聯 220歐姆的電阻)
  2. 並聯的LED燈?
  3. 如何讓2個 LED燈交替亮?


程式碼

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int led2 = 12;
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
  pinMode(led2, OUTPUT);   
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(led2, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
   digitalWrite(led2, HIGH);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}


三、網路資源

4. Arduino教學

http://1know.net/knowledge/97636f101c46?v=list

5.電阻色碼表


  • Arduino創始人在TED大會的演說 (Massimo Banzi: How Arduino is open-sourcing

練習01:暴力解-霹靂燈

//(1)設定檔
int ledPin1 =  8;      // the number of the LED pin
 int ledPin2 =  9;      // the number of the LED pin
 int ledPin3 =  10;      // the number of the LED pin
//(2)setup 函式
void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}
 
//(3)loop 函式
void loop() {
    digitalWrite(ledPin1, HIGH);
     delay(100);
    digitalWrite(ledPin1, LOW);
     delay(100);
     
      digitalWrite(ledPin2, HIGH);
       delay(100);
    digitalWrite(ledPin2, LOW);
     delay(100);


      digitalWrite(ledPin3, HIGH);
       delay(100);
    digitalWrite(ledPin3, LOW);
     delay(100);
     
       digitalWrite(ledPin2, HIGH);
       delay(100);
    digitalWrite(ledPin2, LOW);
     delay(100);
}


Arduino教學
https://www.youtube.com/playlist?list=PLXbFMuyNWWqBQxgALwjrDSEC97f4Krq3P

2015年5月13日 星期三

建立分子模型

https://phet.colorado.edu/zh_TW/simulation/build-a-molecule

建立分子模型 螢幕截圖下載
Version 1.03嵌入
從原子開始,看看您可以建立多少分子。收集您的分子,並檢視其 3D 結構!

翻譯者:
Amber Chang (2011)