#include<Stepper.h>
#defineSTEPS 180 //步進馬達每圈的步進數
//steps:代表馬達轉完一圈需要多少步數。如果馬達上有標示每步的度數,
//將360除以這個角度,就可以得到所需要的步數(例如:360/3.6=100)。(int)
Stepperstepper(STEPS, 12, 11, 10, 9);
const intTrigPinIn = 7; //超音波
const intEchoPinIn = 6; //超音波
voidsetup() {
//initialize serial communication:
//digitalWrite(bz_Pin,LOW);
stepper.setSpeed(140);//將馬達的速度設定成140RPM 最大 150~160
Serial.begin(9600);
}
voidloop()
{
//digitalWrite(bz_Pin,LOW);
//establish variables for duration of the ping,
// and thedistance result in inches and centimeters:
longdurationIn, inchesIn, cmIn;
longdurationOut, inchesOut, cmOut;
// ThePING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give ashort LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(TrigPinIn,OUTPUT);
pinMode(EchoPinIn,INPUT);
//歡迎光臨
digitalWrite(TrigPinIn,LOW);
delayMicroseconds(2);
digitalWrite(TrigPinIn,HIGH);
delayMicroseconds(5);
digitalWrite(TrigPinIn,LOW);
durationIn= pulseIn(EchoPinIn, HIGH);
inchesIn =microsecondsToInches(durationIn);
cmIn =microsecondsToCentimeters(durationIn);
Serial.print(inchesIn);
Serial.print("in,");
Serial.print(cmIn);
Serial.print("cmIIIIN");
Serial.println();
delay(30); //100
if(cmIn<=30.0 )
{
stepper.step(180);//正半圈
Serial.println("--------------------------");
delay(1000); //要修正
}
else
{
stepper.step(0);// 停止運轉
delay(1000);
}
}
longmicrosecondsToInches(long microseconds)
{
//According to Parallax's datasheet for the PING))), there are
// 73.746microseconds per inch (i.e. sound travels at 1130 feet per
//second). This gives the distance travelled by the ping, outbound
// andreturn, so we divide by 2 to get the distance of the obstacle.
// See:http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
returnmicroseconds / 74 / 2;
}
longmicrosecondsToCentimeters(long microseconds)
{
// Thespeed of sound is 340 m/s or 29 microseconds per centimeter.
// Theping travels out and back, so to find the distance of the
// objectwe take half of the distance travelled.
returnmicroseconds / 29 / 2;
}
有甚麼地方修改,還請大大們看看一下
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) | Powered by Discuz! X3.2 |