#include <IpoeA1.h>
IpoeMotor myMotor; // 實體化馬達控制物件
int maxs=90;
int _ABVAR_1_dis = 0 ;
int ardublockUltrasonicSensorCodeAutoGeneratedReturnCM(int trigPin, int echoPin)
{
long duration;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
duration = duration / 59;
if ((duration < 2) || (duration > 300)) return false;
return duration;
}
void setup()
{
myMotor.motor_init(); // 進行馬達及編碼器腳位的初始化動作
digitalWrite( 36 , LOW );
Serial.begin(9600);
}
void loop()
{
_ABVAR_1_dis = ardublockUltrasonicSensorCodeAutoGeneratedReturnCM( 36 , 37 ) ;
Serial.print("Dis=");
Serial.print(_ABVAR_1_dis);
Serial.println();
if (_ABVAR_1_dis> 30)
{
myMotor.setSpeeds(maxs, maxs);
delay(100);
}
else
{
myMotor.setSpeeds(-maxs, -maxs);
delay(300);
myMotor.setSpeeds(maxs, -maxs);
delay(300);
}
delay( 100 );
}
沒有留言:
張貼留言