답변 : 급합니다 도와주세요
페이지 정보
작성자 master 작성일18-12-07 19:13 조회3,552회 댓글0건본문
// MCU BASIC: https://www.basic4mcu.com
// DateTime : 2018-12-07 오후 7:18:32
// by Ok-Hyun Park
//
#include <SoftwareSerial.h>
const byte pinRX=7;
const byte pinTX=8;
SoftwareSerial btSerial(pinRX,pinTX);
//
#include <Servo.h>
const byte pinServo=9;
const byte pinMotor=10;
Servo servoSteering;
Servo servoThrust;
//
union{
int intValue;
byte byteValue[sizeof(int)];
}intUnion; //union 사용.
//
//
int steering=90; // Angle=60~120(60: left,90: center,120: right
int thrust=1500; // Duty=1200~1800(1200: backward,1500: stop,1800: forward)
//
void receive(){
int x_value,y_value;
while(btSerial.available()>=5){
if(btSerial.read()=='@'){
for(int16_t i=0; i<2; i++){ intUnion.byteValue[i]=btSerial.read(); }
y_value=intUnion.intValue;
steering=map(x_value,-500,500,60,120); // Left(60)-Center(90)-Right(120)in Angle
//
for(int16_t i=0; i<2; i++){ intUnion.byteValue[i]=btSerial.read(); }
x_value=intUnion.intValue;
thrust=map(y_value,-500,500,1200,1800); // rew(1200)-stop(1500)-forw(1800)in Duty
return;
}
}
}
//
void setup(){
servoSteering.attach(pinServo);
servoThrust.attach(pinMotor);
steering=90; servoSteering.write(steering); delay(10);
thrust=1500; servoThrust.writeMicroseconds(thrust); delay(10);
btSerial.begin(9600);
}
//
void loop(){
receive();
servoSteering.write(steering); delay(10); // 60~90~120 in Angle
servoThrust.writeMicroseconds(thrust); delay(10); // 1200~1500~1800 in Duty
}
혹시 다음 답변을 하게 될런지 몰라서 소스코드 정리해둔 것 올려둡니다.
댓글 : 0
조회수 : 3,552
등록된 댓글이 없습니다.