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