Arduino 習作工坊 - Lesson 5 機械之夜
-
Upload
cavedu-education -
Category
Education
-
view
552 -
download
2
Transcript of Arduino 習作工坊 - Lesson 5 機械之夜
Arduino PlusMakerBar Taipei Workshop
BLUE TOOTH CAR藍牙遙控車
原理說明
TA7279PIC Arduino1 不⽤用接
2 63 94 motor1 +5 5V6 motor1 -7 GND8 GND9 motor2 -10 vcc11 motor2 +12 313 514 5V
阻抗值
煞⾞車
逐漸停⽌止
電路ARDUINO IC6 29 33 125 13
MOTOR ICM1+ 4M1- 6M2+ 11M2- 9
ARDUINO BT5V VCCGND GND10 Tx11 Rx
13
motoduino⽤用來控制2個直流⾺馬達的腳位分別為D5+D8、D6+D7兩組,⽤用來做⽅方向及轉速的控制
PWM正反轉
#include <SoftwareSerial.h> int Data = 0; SoftwareSerial I2CBT(10,11); void setup() { //開啟通訊 Serial.begin(9600); I2CBT.begin(9600); }
void loop() { if(I2CBT.available()>0) { //讀取鍵盤訊息 Data = I2CBT.read(); I2CBT.println(Data); }
switch(Data) { case 'f': //前進 MotorControl(0,200); MotorControl(1,200); break; case 'l': //左轉 MotorControl(0,100); MotorControl(1,200); break; case 'r': //右轉 MotorControl(0,200); MotorControl(1,100); break; case 'b': //後退 MotorControl(0,-200); MotorControl(1,-200); break; case 's': //停⽌止 MotorControl(0,0); MotorControl(1,0); break; } }
void MotorControl(int motor,int velocity) { if(motor==0) { if(velocity > 0) { analogWrite(6,velocity); analogWrite(9,0); } else { velocity = velocity*(-1); analogWrite(6,0); analogWrite(9,velocity); } }
if(motor==1) { if(velocity > 0) { analogWrite(3,velocity); analogWrite(5,0); } else { velocity = velocity*(-1); analogWrite(3,0); analogWrite(5,velocity); } } }
副程式
ME ARM機械手臂
Pulse Width Modulation (PWM)脈衝寬度調變
• 數位輸出可以控制訊號的開和關,開和關同時
意味著通電與斷電
• 如果我們可以進一步控制通電的時間比例,就
能讓輸出的訊號產生變化,例如LED燈通電時
間為50%,就可以控制LED燈只有50%的亮度
PWM
• 以數位訊號模擬類比訊號的技術
PWM 使用方法
• IDE code 為 analogWrite() • 格式:analogWrite(pin, value) • 在uno板子中只有pin 3,5,6,9,10,11腳位為
PWM腳位 • Value: duty cycle,介於0-255,0為0%,
127為50%,255為100%
mearm範例(mearmtest)
#include <Wire.h>#include <Servo.h>
Servo servo_3;
int i=135;
void setup(){ Serial.begin(9600); delay(100); servo_0.attach(9); servo_0.write(i); delay(100);}
void loop(){ int readnum = Serial.read(); if(readnum==49) //“1” { if(i<180) i++; } else if(readnum==50) //“2” { if(i>0) i=i-1; } Serial.print("i= "); Serial.println(i); servo_3.write(i); delay(50);}
mearm範例(mearm)
24
0
13
2
• servo_0.attach(3); • servo_0.write(i0);
• servo_1.attach(5); • servo_1.write(i1);
• servo_2.attach(6); • servo_2.write(i2);
• servo_3.attach(9); • servo_3.write(i3);
25
26
搖桿:類比+數位輸入
按鈕: int btn_l = digitalRead(4); int btn_r = digitalRead(7); 遙桿: int vrx = analogRead(A0); int vry = analogRead(A1); int vlx = analogRead(A2); int vly = analogRead(A3);
27
int i0 = 90; int i1 = 150; int i2 = 110; int i3 = 130; int i0max=180; int i0min=0; int i1max=175; int i1min=120; int i2max=140; int i2min=90; int i3max=140; int i3min=120;
初始值
最⼤大最⼩小值
29
0
13
2
0
12
3
1
03
2
3