Arduino 習作工坊#2 - 動力之夜150114
-
Upload
cavedu-education -
Category
Devices & Hardware
-
view
1.913 -
download
2
Transcript of Arduino 習作工坊#2 - 動力之夜150114
Arduino 可以做些什麼?• 二輪平衡車segway
• 按讚機器人
http://lab.cavedu.com/arduino
PWM
• 數位輸出可以控制訊號的開和關,開和關同時意味著通電與斷電
• 如果我們可以進一步控制通電的時間比例,就能讓輸出的訊號產生變化,例如 LED 燈通電時間為50% ,就可以控制 LED 燈只有 50% 的亮度
PWM 使用方法• IDE code 為 analogWrite()• 格式: analogWrite(pin, value)• 在 uno 板子中, pin 可以是 3/5/9/10/11 腳位• Value: duty cycle ,介於 0-255 , 0 為
0% , 127 為 50% , 255 為 100%
拿程式範例修改File => Example => Basic => Fade
int led = 9; // the pin that the LED is attached toint brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT);}
// the loop routine runs over and over again forever:void loop() { // set the brightness of pin 9: analogWrite(led, 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. 控制器(即控制電路)會將輸入的 PWM 訊號轉換成相對的參考電壓(解碼 ,decoding ),不同的參考電壓會對應到伺服馬達轉軸的不同位置。
2. 控制器藉由量測電位計的分壓得知目前伺服馬達轉軸的位置。
3. 控制器比較此兩電壓的差異(也就是位置的差異),並開始轉動,直到輸入的參考電壓與實際的電位計分壓相同為止。
4. 因此,伺服馬達只要通電就會鎖死,無法由外力轉動,因為其會不斷地比較位置變化(當然會有一個初始值),並嘗試修正到目標位置。
程式 - Sweep#include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
程式 -Knob#include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometerint val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // 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, 179); // 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 }
程式 -Knob#include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometerint val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object Serial.begin(9600);} void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) println(val); myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }
程式• File => Example => Basic => Blink
int input1 = 8;int input2 = 10;void setup() {
pinMode(input1, OUTPUT); pinMode(input2, OUTPUT);
}// the loop routine runs over and over again forever:void loop() {
digitalWrite(input1, HIGH); digitalWrite(input2, LOW); delay(1000);// wait for a second digitalWrite(input1, LOW); digitalWrite(input2, HIGH); delay(1000);// wait for a second
}
http://ming-shian.blogspot.tw/2013/05/blog-post_8.html
雙極就是驅動馬達的電流是雙向的,驅動控制時,需要改變電流方向。 而單極的馬達,其電流就只需提供一個方向就好,改變提供的順序就可以達到驅動控制。
Stepper(int steps, pin1, pin2, pin3, pin4)建立一個步進馬達的物件。其中 step 是指轉一圈所需的步數,假使馬達定義每步的角度,用 360 去除,就會得到步數。例如: Stepper myStepper(100, 8, 9, 10, 11); 表示每一步為 3.6 度,轉一圈總共 100 步。Stepper.setSpeed(long rpms)設定步進馬達每分鐘轉速 (RPMs) ,需為正數。這個函式並不會讓馬達轉動,只是設定好轉速,當呼叫 Step() 函式時才會開始轉動。Stepper.step(int steps)啟動馬達行進 steps 步數。 setSpeed() 定義速度,正的表示一個方向, 負數表示反方向。
http://atceiling.blogspot.tw/
<Stepper.h>
程式Stepmotor_step
#include <Stepper.h>// initialize the stepper library on pins 8 through 11:Stepper myStepper(200, 8,9,10,11);// 表示每一步為 1.8 度,轉一圈總共 200 步。 void setup() {
// nothing to do inside the setup}void loop() { myStepper.setSpeed(50);// 轉速為 50rpm(revolution per minutes 每分鐘可以轉 50 圈 ) myStepper.step(1); // 一次走一步 }
程式Stepmotor2_control
#include <Stepper.h>const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution// for your motor// initialize the stepper library on pins 8 through 11:Stepper myStepper(stepsPerRevolution, 8,9,10,11); int stepCount = 0; // number of steps the motor has takenvoid setup() {
// nothing to do inside the setup}void loop() {
// read the sensor value: int sensorReading = analogRead(A0); // map it
to a range from 0 to 100: int motorSpeed = map(sensorReading, 0, 1023,
0, 100); // set the motor speed: if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed); // step 1/100 of a revolution: myStepper.step(stepsPerRevolution/
100); }
}