잘동작하는 최종 예제 소스 (무선 통신)
https://github.com/happythingsmaker/openBuskingBot/blob/master/arduino/openBuskingBot_Serial_BT_Servo.ino
#include <Servo.h>
#include <SoftwareSerial.h>
#define PIN_NUMBER_SW_SERIAL_TX (2)
#define PIN_NUMBER_SW_SERIAL_RX (3)
#define PIN_NUMBER_LEFT_MOTOR (9)
#define PIN_NUMBER_RIGHT_MOTOR (10)
#define LEFT_HIT_POSITON 140
#define RIGHT_HIT_POSITON 40
#define CENTER 90
Servo myservo1;
Servo myservo2;
SoftwareSerial BTSerial(PIN_NUMBER_SW_SERIAL_RX, PIN_NUMBER_SW_SERIAL_TX);
long lastHitTime = 0;
void LeftHit() {
myservo1.attach(PIN_NUMBER_LEFT_MOTOR);
myservo1.write(LEFT_HIT_POSITON);
delay(200);
myservo1.write(CENTER);
}
void RightHit() {
myservo2.attach(PIN_NUMBER_RIGHT_MOTOR);
myservo2.write(RIGHT_HIT_POSITON);
delay(200);
myservo2.write(CENTER);
}
void hitDrum(char data) {
if (data == 'b') {
LeftHit();
} else if (data == 's') {
RightHit();
} else if (data == 'f') {
myservo1.attach(PIN_NUMBER_LEFT_MOTOR);
myservo2.attach(PIN_NUMBER_RIGHT_MOTOR);
myservo1.write(LEFT_HIT_POSITON);
myservo2.write(RIGHT_HIT_POSITON);
delay(60000);
}
}
void setup() {
myservo1.attach(PIN_NUMBER_LEFT_MOTOR);
myservo2.attach(PIN_NUMBER_RIGHT_MOTOR);
long baudrate = 38400;
Serial.begin(baudrate);
BTSerial.begin(baudrate);
LeftHit();
RightHit();
}
char tempData = 0;
void loop() {
if (Serial.available()) {
char tempData = Serial.read();
hitDrum(tempData);
lastHitTime = millis();
}
if (BTSerial.available()) {
char tempData = BTSerial.read();
hitDrum(tempData);
lastHitTime = millis();
}
if (millis() > lastHitTime + 1000) {
myservo1.detach();
myservo2.detach();
}
}