2016年3月21日 星期一

Arduino的教學13-伺服馬達的使用(servo motor)

一、課程目標:學習利用可變電阻來控制伺服馬達
二、接線圖:

一般伺服馬達有三條線,電源(紅色)、接地(黑或棕色)、訊號線(白、黃、橘、藍....)
dataPin=9;

伺服馬達是控制轉幾度!最多180度。 三、程式碼:
Examples -> Servo -> Knob
每次轉1度,由1-180度 再轉回來
#include <Servo.h> // 匯入 Servo 這個 class
Servo myservo;          // 建立 myservo 這個 Servo 物件 
int pos = 0;                // 儲存伺服馬達旋轉角度

void setup()
{
  myservo.attach(9);  // 指定伺服馬達的 control line 為 Arduino Uno 的 pin 9
 } 

void loop()
{
  for(pos = 0; pos < 180; pos += 1)   // 一度一度由 0 度旋轉到 180 度
  {                                 
    myservo.write(pos);             
    delay(15);                      
  }
  for(pos = 180; pos>=1; pos-=1)     // 一度一度由 180 度旋轉到 0 度
  {                               
    myservo.write(pos);             
    delay(15);                      
  }
}


/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott 

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // 可變電阻的pin analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int servoPin=9;//伺服馬達pin
void setup() {
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}
四、作業練習:
如何將伺服馬達來回轉90度,三次?

2016年3月15日 星期二

Arduino的教學12-常用參考語法

1.數位輸入:File > Examples >01.Basics > Blink
//(1)設定檔
int ledPin =  13;      // the number of the LED pin

//(2)setup 函式
void setup() {
  pinMode(ledPin, OUTPUT);
}

//(3)loop 函式
void loop() {
    digitalWrite(ledPin, HIGH);
 delay(100);
    digitalWrite(ledPin, LOW);
 delay(100);
}
2.類比輸入與Serail Moniter:File > Examples >03.Analog > AnalogInput
//(1)設定檔
int sensorPin = A0;    // select the input pin 
int sensorValue = 0;  // the value coming from the sensor

//(2)setup 函式
void setup() {
Serial.begin(9600);
}

//(3)loop 函式
void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
 Serial.println(sensorValue);
delay(10);

}
3.使用2x16液晶
//(1)設定檔
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//(2)setup 函式
void setup() {
 lcd.init();  //初使化
  lcd.backlight();//打開背景燈
  lcd.setCursor(1, 0);//設定 第1列,空1格開始
  lcd.print("Hello, world!");
  lcd.setCursor(2, 1);//設定 第2列,空2格開始
  lcd.print("sceond line");}

//(3)loop 函式
void loop() {
delay(100);
lcd.clear();
}

2016年3月6日 星期日

arduino-文章列表


Arduino的教學11- 數位輸入 button 的的使用

一、學習目標: 學習使用button來控制 led燈

二、重點說明:
按鈕一腳串接一個10k的電阻。

三、接線圖:
buttonPin = 2;
ledPin = 13;



四、程式碼
/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave 
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

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

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);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}


練習01-設定按鈕後不同值(按鈕後依序出現 0-4值)
// set pin numbers:
const int buttonPin = 12;     // 按鈕輸入值pin

int ledCount=5;//除數
int no=0;  //流水號
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int thisNo=0;  //餘數值
void setup() {
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);   
}

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) {     
   
    no++;
    thisNo = no % ledCount;//取餘數
delay(100);
Serial.println(thisNo);
} 

    
  delay(100);
}
練習02-設定按鈕後 依序點亮不同的led燈


// set pin numbers:
const int buttonPin = 2;     // 按鈕輸入值pin
int ledPins[] = { 
  8,9,10};   // an array of pin numbers to which LEDs are attached

int ledCount=3;//除數
int no=0;  //流水號
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int thisNo=0;  //餘數值
void setup() {
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);   

  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT); 
  }

}

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) {     

    no++;
    thisNo = no % ledCount;//取餘數
    //    digitalWrite(ledPins[thisNo], HIGH );


    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      if(thisLed == thisNo){

        digitalWrite(ledPins[thisLed], HIGH);
      }
      else{
        digitalWrite(ledPins[thisLed], LOW     );
      }

    }

    delay(100);
    Serial.println(thisNo);
  } 

  delay(100);
}



2016年1月24日 星期日

Arduino的教學10- 利用DHT11抓取溫度和濕度,並顯示於2x16 I2C LCD上

一、學習目標:
學習使用DHT11溫溼度感應器來測量溫度,並顯示於 I2C 2X16

二、重點說明
DHT11功能說明:
1、濕度測量範圍:20~90%RH;
2、濕度測量精度:±5%RH;
3、溫度測量範圍:0~50℃
4、溫度測量精度:±2℃
5、電源供應範圍: 3~5V
6、頻率不可超過:0.5Hz (每2秒一次)

三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在2

四、程式碼
DHT11的程式碼1

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
 
dht DHT; 
  
void setup(){   
Serial.begin(9600);   
delay(300);             //Let system settle   
Serial.println("Humidity and temperature\n\n");   
delay(700);             //Wait rest of 1000ms recommended delay before   
                        //accessing sensor   
}
  
void loop(){   
DHT.read11(dht_dpin);   //去library裡面找DHT.read11  
Serial.print("Humidity = ");   
Serial.print(DHT.humidity);   
Serial.print("% ");   
Serial.print("temperature = ");   
Serial.print(DHT.temperature);   
Serial.println("C ");   
delay(1000);  //每1000ms更新一次  
}

DHT11 + I2C LCD 程式碼
#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

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
dht DHT; 

int no=0;//計數器
void setup()
{
  lcd.init();                      // initialize the lcd 
Serial.begin(9600);
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("I2C+DHT11");
  Serial.println("Humidity and temperature\n\n");   
  delay(1000);  
}

void loop()
{
  no++;
  DHT.read11(dht_dpin);   //去library裡面找DHT.read11 
  //序列埠輸出
  Serial.print("Humidity = ");   
  Serial.print(DHT.humidity);   
  Serial.print("% ");   
  Serial.print("temperature = ");   
  Serial.print(DHT.temperature);   
  Serial.println("C "); 

  //資料顯示在LCD
  lcd.setCursor(0, 0);
  lcd.print("No.");
  lcd.print(no);
  lcd.setCursor(8, 1);
  lcd.print("H=");
  lcd.print(DHT.humidity);   
  lcd.setCursor(0, 1);
  lcd.print("T=");
  lcd.print(DHT.temperature);   
  delay(500);            //每500ms更新一次  
  lcd.clear();
}
五、參考資料: http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html

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來收集
的資料