제가 지금 아두이노 초음파 센서로 만드는 자율주행 RC카를 만들고 있는데요
시리얼 모니터에 보면 거리가 출력 되잖아요
그런데 그 시리얼 모니터에서 거리가 뜨는 속도를 빠르게 할 수 없나요?
코드는 이것 입니다.
const int E1Pin = 5;
const int M1Pin = 4;
const int E2Pin = 6;
const int M2Pin = 7;
/**inner definition**/
typedef struct
{
byte enPin;
byte directionPin;
}MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] ={ {E1Pin,M1Pin}, {E2Pin,M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;
/**program**/
void setup()
{
pinMode (2, OUTPUT);
pinMode (3, INPUT);
Serial.begin(9600);
initMotor();
}
void loop()
{
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);
long duration = pulseIn(3, HIGH);
if (duration == 0) {
return;
}
long distance = duration / 58.2;
Serial.println(distance);
int value;
/**test M1 **/
if(distance>50){
setMotorDirection(M1,Forward);
setMotorSpeed(M1,100);
delay(1000);
setMotorDirection(M2,Forward);
setMotorSpeed(M2,100);
delay(1000);
}
else if(distance<100){
setMotorDirection(M1,Forward);
setMotorSpeed(M1,100);
delay(1000);
setMotorDirection(M2,Forward);
setMotorSpeed(M1,0);
delay(1000);
}
}
/**functions**/
void initMotor( )
{
int i;
for ( i = 0; i < MotorNum; i++ )
{
digitalWrite(MotorPin[i].enPin, LOW);
pinMode(MotorPin[i].enPin, OUTPUT);
pinMode(MotorPin[i].directionPin, OUTPUT);
}
}
/** motorNumber: M1, M2
direction: Forward, Backward **/
void setMotorDirection( int motorNumber, int direction )
{
digitalWrite( MotorPin[motorNumber].directionPin, direction);
}
/** speed: 0-100 * */
inline void setMotorSpeed( int motorNumber, int speed )
{
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed/100.0) ); //PWM
}
|