Servo motor bracket assemble and arduino source code

2018. 5. 30. 01:10Hobby/아두이노

반응형

Servo motor bracket assemble and arduino source code


■ Source Code download

Servo motor bracket assemble and arduino source code.txt


아두이노에서 실행 후 시리얼 창에 case 문에 있는 명령어를 입력하면 움직입니다. 명령을 연속으로 넣고 엔터 치면 몰아서 일괄 실행합니다.


/*  by ZENEZ, 2018-05-29 */ #include <Servo.h> int curPosition1 = 90; //starting position X, range : 20~160 deg int curPosition2 = 90; //starting position Y  range : 40~130 deg int nSwitch = 0; // user serial command input value, default = 0 int servoPin1 = 8;  // X axis, left, right int servoPin2 = 7;  // Y axis, up, down Servo servo_1; // X axis, left, right Servo servo_2; // Y axis, up, down void setup() {  Serial.begin(9600);  servo_1.attach(servoPin1);  servo_2.attach(servoPin2);  servo_1.write(curPosition1);  servo_2.write(curPosition2); } void loop() {  if (Serial.available())  {    nSwitch = Serial.read();  }  switch (nSwitch) {    case '6': //right      for (int pos = curPosition1 ; pos >= 20 ; pos -= 1) {        // tell servo to go to position in variable 'pos'        servo_1.write(pos);        curPosition1 = pos;        // wait 15 ms for servo to reach the position        delay(15); // Wait for 10 millisecond(s)        printXY();      }      break;    case '4' : //left      for (int pos = curPosition1; pos <= 160; pos += 1) {        servo_1.write(pos);        curPosition1 = pos;        delay(15);        printXY();      }      break;    case '8' : //up      for (int pos = curPosition2 ; pos >= 40 ; pos -= 1) {        servo_2.write(pos);        curPosition2 = pos;        delay(10);        printXY();      }      break;    case '5' : //down      for (int pos = curPosition2; pos <= 130; pos += 1) {        servo_2.write(pos);        curPosition2 = pos;        delay(10);        printXY();      }      break;    case '0' : //center, X = Y = 90 deg      while (curPosition1 != 90 || curPosition2 != 90) {        curPosition1 = ( curPosition1 < 90 ? ++ curPosition1 : -- curPosition1);        servo_1.write(curPosition1);        delay(10);        Serial.print("X : " );        Serial.println(curPosition1);        curPosition2 = ( curPosition2 < 90 ? ++ curPosition2 : -- curPosition2);        servo_2.write(curPosition2);        delay(10);        Serial.print("Y : " );        Serial.println(curPosition2);      }  } } void printXY() {  Serial.print("X : " );  Serial.println(curPosition1);  Serial.print("Y : " );  Serial.println(curPosition2); }


■ YouTube


■ SG-90 Servo Motor

■ SG-90 Pan/Tilt Bracket

싸서 샀습니다. 역시나 비지떡이었습니다. 모터, 브라켓 모두 약합니다. 학습용외 실제 적용하기 아주 무리가 많습니다. 혼자 자세잡고 서 있기도 버거운 모델입니다.

반응형