2017年1月31日 星期二

可變電阻控制MeArm機械手臂

學校最近買了雷射切割機,於是將想了很久的MeArm使用木板切了一套,不過雷射強度沒有調整好,結果燒焦造成螺絲孔太大,有點零零落落。





PDF 組裝手冊

這是一個高手的DIY : 袖珍版機械手臂 MeArm
 MeArm V0.4 的零件圖下載點
可變電阻控制伺服馬達
https://goo.gl/SX7y9i
利用電位器控制 meArm https://goo.gl/KqhQqp
MeArm組裝說明(中文) https://goo.gl/sNdVjB
MeArm組裝說明 https://goo.gl/ollblX

Arduino 程式

// by Michal Rinott  
// Used Michal's code but x4 to control the MeArm v0.3 - Ben Gray

#include <Servo.h>

Servo myservo0, myservo1, myservo2, myservo3 ;  // 4個馬達

int potpin0 = 0;  // analog pin used to connect the potentiometer
int potpin1 = 1;  // analog pin used to connect the potentiometer
int potpin2 = 2;  // analog pin used to connect the potentiometer
int potpin3 = 3;  // analog pin used to connect the potentiometer
int val0;    // variable to read the value from the analog pin 
int val1;    // variable to read the value from the analog pin 
int val2;    // variable to read the value from the analog pin 
int val3;    // variable to read the value from the analog pin 

void setup() 

  Serial.begin(9600);
  //Serial.println("CLEARDATA");
  //Serial.println("LABEL,Time,Timer,val0,val1,val2,val3");
  myservo0.attach(11);  // attaches the servo on pin 11 to the servo object
  myservo1.attach(10);  // attaches the servo on pin 10 to the servo object
  myservo2.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo3.attach(6);  // attaches the servo on pin 6 to the servo object


void loop() 

  val0 = analogRead(potpin0);// 讀取電位 (value between 0 and 1023) 
  val0 = map(val0, 0, 800,179,0);   // 轉換為servor角度 (value between 0 and 180) 
  myservo0.write(val0);             // 馬達旋轉位置
 // Serial.println(val0);
  delay(40);                           // waits for the servo to get there

  val1 = analogRead(potpin1);            
  val1 = map(val1, 0, 1023, 0, 179);     
  myservo1.write(val1); 
 // Serial.println(val1);  
  delay(40); 
                          
  val2 = analogRead(potpin2);            
  val2 = map(val2, 0, 1023, 0, 179);     
  myservo2.write(val2);                  
  //Serial.println(val2);
  delay(40);                           
  
  val3 = analogRead(potpin3);            
  val3 = map(val3, 0, 1023, 0, 179);     
  myservo3.write(val3);                  
 // Serial.println(val3);
  delay(40);                           
  
 /* Serial.print("DATA,TIME"); 
  Serial.print(",");
  Serial.print("TIMER"); 
  Serial.print(",");
  Serial.print(val0);
  Serial.print(",");
  Serial.print(val1);
  Serial.print(",");
  Serial.print(val2);
  Serial.print(",");
  Serial.println(val3); 
*/

可變電阻控制伺服馬達

Servo library

可變電阻控制伺服馬達(Examples from Libraries ; Servo ; Knob)

  • Arduino uno
  • 伺服馬達 Servo Motor
  • 10k ohm可變電阻 10k ohm potentiometer
  • 單心線 hook-up wires

/*
 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 建立一個 Servo 

int potpin = 0;  // analog pin used to connect the potentiometer  可變電阻接中間訊號接在 Analog pin 0
int 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  Servo 接在 pin 9
}
void loop() {
  val = analogRead(potpin);            //讀取可變電阻(數值介於 0 到 1023)
  val = map(val, 0, 1023, 0, 180);     // 把 0 - 1023 的數值按比例轉換為 0 - 180 的數值
  myservo.write(val);                  // Servo 旋轉角度
  delay(15);                           // 等待 Servo 旋轉位置
}