本帖最後由 fsn103659 於 2015-1-22 11:07 編輯
各位好 這幾天參考了
步進馬達用ULN2003APG驅動電路來轉動馬達
[size=12.7272720336914px]http://atceiling.blogspot.tw/2013/04/arduino.html#.VL_PUkeL86W
[size=12.7272720336914px]參考這版大的接法
[size=12.7272720336914px]我們現在多加一個超音波跟馬達來驅動
[size=12.7272720336914px]
這是自己接在麵包板上
超音波顯示出來 0cm 可能讀到值沒有寫入有人建議我寫個副程式 小弟我不太會弄
[size=12.7272720336914px]以下朋友修改程式碼
[size=12.7272720336914px]我們成果是說讓超音波感應到而馬達開始轉動
[size=12.7272720336914px]沒有感應到 馬達不會轉動
#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; }
有甚麼地方修改,還請大大們看看一下 |