brunch

You can make anything
by writing

C.S.Lewis

by 메이커k Apr 26. 2018

5/10 모터




모터 

모든 움직임의 기본은 모터이다. 로봇을 비롯한 움직이는 것들은 모터들의 연결과 조합이라고 할 수 있다. 모터의 제어가 곧 로봇 움직임의 관건이라고 할 수 있다. 그동안 정적인 변화였다면 이번에는 servo moter의 제어해보도록 한다. 기본 방법은 앞에 예제와 같다. android에서 버튼 입력을 받으면 arduino에서 신호를 받아 moter를 제어하는 흐름이다. 


한쪽 버튼 버트를 좌측으로 움직이고, 다른 버튼은 우측으로 모터를 움직이다. 잠근 장치로 연결한다면 스마트폰으로 문을 여 닫을 수 있는 기능이 될 수 도 있을 것이다. 비록 모터 하나를 제어하였지만 앞으로 나올 더 복잡한 예제들도 큰 틀에서 이번 예제와 같다.


아두이노 코드는 아래와 같다. 

arduino Sweep example 을 약간 수정하였다. 


/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.

 modified 8 Nov 2013

 by Scott Fitzgerald

 http://www.arduino.cc/en/Tutorial/Sweep

*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

int ByteReceived;

void setup() {

  Serial.begin(115200);

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

void loop() {

   if (Serial.available() > 0){

    ByteReceived = Serial.read();

    if(ByteReceived == '1'){

      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

      }

    }

    if(ByteReceived == '0'){

      for (pos = 180; pos >= 0; 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

      }

    }

  }

}

매거진의 이전글 4/10 LED
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari