부품 특성 :
코드 :
#include <Servo.h>
#define PIN_PWM 9
#define MIN 0
#define MAX 180
#define MODE 0
void UpAndDown();
void OrderBySerial();
void setMode(int mode);
Servo moter;
String readString;
String wBuf;
int theta=0;
int step = 5;
void (*func)() = UpAndDown;
void setup() {
Serial.begin(9600);
moter.attach(PIN_PWM, MIN, MAX);
moter.writeMicroseconds(20);
setMode(0);
}
void setMode(int mode) {
switch(mode) {
case 1: case -1: case 'a': case 'A':
func = UpAndDown;
break;
case 2: case -2: case 'b': case 'B':
func = OrderBySerial;
break;
}
}
void loop() {
func();
delay(20);
}
void UpAndDown() {
if (Serial.available() > 0) {
char c = Serial.read();
setMode(c);
}
theta += step;
if(theta > MAX || theta < MIN){
step = -step;
theta += step;
}
Serial.println(theta);
moter.write(theta);
}
void OrderBySerial(){
if (Serial.available() > 0) {
delay(3);
char c = Serial.read();
if((c == '\n')&&(readString.length() > 0)) {
theta = readString.toInt();
readString = "";
if(theta < 0) setMode(theta);
Serial.println(theta);
moter.write(theta);
}else{
readString += c;
}
}
}
부품 출처 :
https://www.devicemart.co.kr/goods/view?no=1128421
TowerPro 호환 9g 미니 서보모터 SG-90
대표 미니 서보모터, 토크 : 1.8kg/cm(4.8V), Operating speed : 0.1sec/60degree(4.8v)
www.devicemart.co.kr
'아두이노' 카테고리의 다른 글
LED (0) | 2022.01.17 |
---|---|
건반 만들기 (0) | 2022.01.17 |
조이스틱 AxisJoystic (0) | 2022.01.17 |
온도, 습도 센서 (KY-015) (0) | 2022.01.17 |
아날로그 입력 (0) | 2022.01.17 |
댓글