總瀏覽量

2012年6月23日 星期六

Arduino / motoduino controls 7 Servos + 1 Stepper Motor (步進馬達)

做了一個簡單實驗, 利用Arduino/Motoduino控制七顆伺服馬達(手上只有七顆)和一顆步進馬達(2-Phase)運轉,運轉還算順暢! 主要利用 I/O Expansion Shield疊在motoduino上,除了馬達電線容易接線外,還可讓七顆伺服馬達吃外部電源.DIY material (材料):
1. Motoduino (Arduino + L293D motor driver)
2. I/O Expansion shield
3. Servo motor (伺服馬達) x 7 
4. Stepper Motor(步進馬達 二相四線)  x 1
5. 9V Battery x 2


接線圖

接線圖

                                                              I/O Expansion Shield V5




Arduino Sketch: (有興趣網友可以把重複程式建立function來呼叫)



#include <Stepper.h>
#include <Servo.h>

Servo servo12;          // Define servo12
Servo servo11;          // Define servo11
Servo servo10;          // Define servo10
Servo servo9;          // Define servo9
Servo servo4;          // Define servo4
Servo servo3;          // Define servo3
Servo servo2;          // Define servo2

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution                                 // for your motor
const int Motor_E1 = 5; // digital pin 5 of Arduino (PWM)  
const int Motor_E2 = 6;  // digital pin 6 of Arduino (PWM)
const int Motor_M1 = 7;     // digital pin 7 of Arduino
const int Motor_M2 = 8;    // digital pin 8 of Arduino
   
Stepper myStepper(stepsPerRevolution, Motor_M1, Motor_M2);          
int pos = 0;

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(Motor_M1, OUTPUT);
  pinMode(Motor_M2, OUTPUT);

  digitalWrite( Motor_E1, HIGH);  // speed control
  digitalWrite( Motor_E2, HIGH);  // speed control

  servo12.attach(12);
  servo11.attach(11);
  servo10.attach(10);
  servo9.attach(9);
  servo4.attach(4);
  servo3.attach(3);
  servo2.attach(2);

}

void loop() {
  // step one revolution  in one direction:
//   Serial.println("clockwise");
  myStepper.step(200);
  delay(500);

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                              
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                              
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 10ms for the servo to reach the position
  } `
  for(pos = 0; pos < 180; pos += 1){  // goes from 0 degrees to 180 degrees
    servo12.write(pos);    // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1){  // goes from 0 degrees to 180 degrees
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1){  // goes from 0 degrees to 180 degrees
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1){  // goes from 0 degrees to 180 degrees
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1) { // goes from 0 degrees to 180 degrees
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1) { // goes from 0 degrees to 180 degrees
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 0; pos < 180; pos += 1) { // goes from 0 degrees to 180 degrees
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }

  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo9.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo10.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }
  for(pos = 180; pos>=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);
  }

  for(pos = 0; pos < 180; pos+=1) {    // goes from 0 degrees to 180 degrees
    servo12.write(pos);              // tell servo to go to position in variable 'pos'
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);
  }

  for(pos = 180; pos >=1; pos-=1) {    // goes from 180 degrees to 0 degrees
    servo12.write(pos);
    servo11.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);
  }
   myStepper.step(-200);
  delay(500);
}






Video:  http://www.youtube.com/watch?v=rB-noWhgbXM&list=UU6aKpj71jLKYABYLr3Wbpuw&index=1&feature=plcp



更多影片請看: http://www.youtube.com/user/sinocgtchen
部落格 : http://sinocgtchen.blogspot.com
Motoduino資訊: http://motoduino.com