서보모터 다중제어에 대하여 질문드립니다.

메카 2016-10-18 (화) 17:32 7년전 12802  
아두이노 메가를 사용하고 있고 버전은 1.0.3을 사용하고 있습니다.

키보드를 통해서 서보모터 5개를 제어하는 하는 방법을 알고 싶습니다. 

아래와 같은 소스로 한개제어는 가능한데 직렬포트창에 1>, 2> 하면 1모터와 2모터의 펄스를 100us만큼씩 변형이 되도록 하고 싶습니다. 
 
어떠한 방법이 있는지 조언좀 부탁드리겠습니다. 소스는 아래와 같습니다.
 
int servoPin = 2;
int minPulse = 600;
int maxPulse = 2400;
int turnRate = 100;
int refreshTime = 20;
int centerServo;
int pulseWidth;
int moveServo;
long lastPulse = 0;
void setup()
{
  pinMode(servoPin, OUTPUT);
  centerServo = maxPulse - ((maxPulse - minPulse)/2);
  pulseWidth = centerServo;
  Serial.begin(9600);
  Serial.println("Arduino Serial Servo Control");
  Serial.println("Press < or > to move. spacebar to center");
  Serial.println();
}
void loop()
{
  if(Serial.available()>0)
  {
    moveServo = Serial.read();
    
    if(moveServo == 44){pulseWidth = pulseWidth - turnRate;}
    if(moveServo == 46){pulseWidth = pulseWidth + turnRate;}
    if(moveServo == 32){pulseWidth = centerServo;}
    
    if(pulseWidth > maxPulse){pulseWidth = maxPulse;}
    if(pulseWidth < minPulse){pulseWidth = minPulse;}
    
    Serial.print("Pulse Width;");
    Serial.print(pulseWidth);
    Serial.println("us");
  }
  
  if(millis() - lastPulse >= refreshTime)
  {
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(servoPin, LOW);
    lastPulse = millis();
  }
}
 
 


==================================



코드를 보려다가 주석이 달려있지 않아서 곽윤건님께서 요청하신 키보드를 이용한 5개의 서보제어를 해보았습니다. 아두이노에서 서보를 제어하기 위해서 Servo.h라는 라이브러리를 사용했고, 간단히 각도를 이용한 제어가 가능합니다. 
또한, A, B, C를 아두이노 시리얼모니터에서 입력받아 각각에 대해 회전값을 주었고요.
혹시나 싶어서 동영상으로 찍었는데, 도움이 되실런지 모르겠네요. 

아, 그리고 회원가입을 해주시면, 답변에 대해서 문자를 보내드리니 가입해주시면 감사하겠습니다. 그럼, 저희 메카솔루션 많이 애용해주세요.^^


#include <Servo.h> 
 
Servo myservo1; 
Servo myservo2; 
Servo myservo3; 
Servo myservo4; 
Servo myservo5; 

byte incomingKey;
 
void setup() 
  Serial.begin(9600);
  myservo1.attach(2);  // attaches the servo on pin 2 to the servo object 
  myservo2.attach(3);  // attaches the servo on pin 3 to the servo object 
  myservo3.attach(4);  // attaches the servo on pin 4 to the servo object 
  myservo4.attach(5);  // attaches the servo on pin 5 to the servo object 
  myservo5.attach(6);  // attaches the servo on pin 6 to the servo object 

  myservo1.write(10);                  // sets the servo position according to the scaled value 
  myservo2.write(10);                  // sets the servo position according to the scaled value 
  myservo3.write(10);                  // sets the servo position according to the scaled value    
  myservo4.write(10);                  // sets the servo position according to the scaled value 
  myservo5.write(10);                  // sets the servo position according to the scaled value 
  delay(100);


 
void loop() 
   incomingKey = Serial.read(); 

    if(incomingKey == 'A')
  {
  myservo1.write(10);                  // sets the servo position according to the scaled value 
  myservo2.write(10);                  // sets the servo position according to the scaled value 
  myservo3.write(10);                  // sets the servo position according to the scaled value 
  myservo4.write(10);                  // sets the servo position according to the scaled value
  myservo5.write(10);                  // sets the servo position according to the scaled value 
  delay(100);
  }
  else if(incomingKey == 'B')
  {
  myservo1.write(150);                  // sets the servo position according to the scaled value 
  myservo2.write(150);                  // sets the servo position according to the scaled value 
  myservo3.write(150);                  // sets the servo position according to the scaled value 
  myservo4.write(150);                  // sets the servo position according to the scaled value 
  myservo5.write(150);                  // sets the servo position according to the scaled value 
  delay(100);
  }
  else if(incomingKey == 'C')
  {
  myservo1.write(90);                  // sets the servo position according to the scaled value 
  myservo2.write(90);                  // sets the servo position according to the scaled value 
  myservo3.write(90);                  // sets the servo position according to the scaled value 
  myservo4.write(90);                  // sets the servo position according to the scaled value 
  myservo5.write(90);                  // sets the servo position according to the scaled value 
  delay(100);
  }












==================================



친절한 답변 너무 고마습니다 (_ _) 

제가 처음 구현하다 보니 어려움이 많습니다...ㅜㅜ

코드를 넣을
메카리워즈 Image Map


모바일 버전으로 보기