Q&A
질문 | 서보모터 제어 관련 질문있습니다.
페이지 정보
작성자 smc5 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 작성일18-06-08 00:01 조회251회 댓글1건본문
서보모터와 스위치를 이용해 로봇팔을 만들고 싶은데요, 스위치를 이용해 스위치가 눌러진다면 팔을 그랩해서 장애물을 잡고 올라가는 동작을 구현하고 싶습니다. (Youtube에 monkeybot 3 servo을 검색하면 나오는데 그 모형에서 손에 스위치를 추가한 모형입니다.) motor5라는 오른팔을 이용해 180도까지 움직였다가 다시 반대 방향으로 움직이면서 팔에 달려있는 스위치가 눌러진다면 특정 동작을 수행하고 안눌러지면 motor4를 다시 구동시키면서 더 높게 팔을 들어 장애물이 있는지를 감지하고 싶습니다. 그런데 서보모터가 움직이다가 스위치 입력을 받을 때 동작을 정지하게 만들 수가 있나요...? if, for, whie, detach(?) 중 하나를 써서 만들 수 있을 것 같긴한데 아무리 고민해도 답이 나오지 않는 것 같습니다. 단순 motor5.write(180);delay(500);motor5.write(0);if(inputPin1){};으로 해봤는데 이 경우에는 if로 빠지지가 않는 것 같습니다
#include <Servo.h>
Servo motor3;
Servo motor4;
Servo motor5;
const int inputPin1 = 6;
const int inputPin2 = 7;
int swCountTimer = 0;
const int swCountTime = 10;
const int ledPin1 = 13;
const int ledPin2 = 12;
void setup() {
Serial.begin(9600);
motor3.attach(3);
motor4.attach(4);
motor5.attach(5);
pinMode(inputPin1, INPUT_PULLUP);
pinMode(inputPin2, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
motor3.write(90);
motor4.write(90);
motor5.write(90);
}
void loop() {
int ledOutput2 = digitalRead(ledPin2);
int ledOutput1 = digitalRead(ledPin1);
if (swCheck(inputPin2)){
if (ledOutput2) digitalWrite(ledPin2, LOW);// LED가 점등되어 있으면 소등
else digitalWrite(ledPin2, HIGH);
exit(0);
}
motor5.write(180);
delay(500);
motor1.write(int i);
while()
}
boolean swCheck(int pin){
boolean swInput = digitalRead(pin);
boolean state;
if(swInput == LOW){
if(swCountTimer >= swCountTime){
if(swCountTimer == swCountTime) state = HIGH;
else state = LOW;
swCountTimer = swCountTime + 1;
}
else{
++swCountTimer;
}
}
else{
state = LOW;
swCountTimer = 0;
}
return state;
}
#include <Servo.h>
Servo motor3;
Servo motor4;
Servo motor5;
const int inputPin1 = 6;
const int inputPin2 = 7;
int swCountTimer = 0;
const int swCountTime = 10;
const int ledPin1 = 13;
const int ledPin2 = 12;
void setup() {
Serial.begin(9600);
motor3.attach(3);
motor4.attach(4);
motor5.attach(5);
pinMode(inputPin1, INPUT_PULLUP);
pinMode(inputPin2, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
motor3.write(90);
motor4.write(90);
motor5.write(90);
}
void loop() {
int ledOutput2 = digitalRead(ledPin2);
int ledOutput1 = digitalRead(ledPin1);
if (swCheck(inputPin2)){
if (ledOutput2) digitalWrite(ledPin2, LOW);// LED가 점등되어 있으면 소등
else digitalWrite(ledPin2, HIGH);
exit(0);
}
motor5.write(180);
delay(500);
motor1.write(int i);
while()
}
boolean swCheck(int pin){
boolean swInput = digitalRead(pin);
boolean state;
if(swInput == LOW){
if(swCountTimer >= swCountTime){
if(swCountTimer == swCountTime) state = HIGH;
else state = LOW;
swCountTimer = swCountTime + 1;
}
else{
++swCountTimer;
}
}
else{
state = LOW;
swCountTimer = 0;
}
return state;
}
댓글목록
최고관리자님의 댓글

위 코드에 이미 서보 모터 제어하는 코드는 다 있네요. 아래 링크에 있는 함수들 참고해서 구현하세요.
https://www.arduino.cc/en/Reference/servo
https://www.arduino.cc/en/Tutorial/Knob
특정 위치에서 멈추기 위해서는 변수에 현재 서보모터의 각도를 기록해두고 사용하면 됩니다.