|
本帖最後由 apk4553123 於 2015-3-25 22:11 編輯
如提我想把紅外線當成觸發的工具
觸發完畢後不管獲得的值怎麼變動都不會影響行為
(後面寫失敗是我測試失敗的位子 提醒自己用
#include <Servo.h>
Servo servoleft; //輪子
Servo servoright;
const int pingPin = 11; //超音波
const int pingPin2 = 2;
long duration, duration2, cm, cm2; //英尺換公分之類的東西
void setup()
{
Serial.begin(9600); //回傳
servoleft.attach(13); //輪子
servoright.attach(12);
pinMode(7,OUTPUT); //紅外線
pinMode(10,INPUT);
}
void loop()
{
int ir = irdetect(7,10,47000);
Serial.println(ir);
delay(100);
pinglift();
duration = pulseIn(pingPin, HIGH);
delay(5);
pingright();
duration2 = pulseIn(pingPin2, HIGH);
cm = microsecondsToCentimeters(duration); //換算出來的直公分
cm2 = microsecondsToCentimeters(duration2);
Serial.print(cm2);//顯示回傳超音波直
Serial.print("cm2, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(10);
if(ir==1)
{
stp(100);
//break;失敗
}//break;失敗
else
{
if(cm>=4&&cm<=8&&cm2>=4&&cm2<=8)//種血衣下 波音波好了
{
forward(80);
}
else if(cm<=3)
{
turnrights(60);
forward(30);
}
else if(cm<=11&&cm>=9)
{
turnlefts(60);
forward(30);
}
else if(cm>=50&&cm<=175)
{
forward(280);
turnleft(300);
forward(1);
}
else if(cm2>=90&&cm2<=115)
{
forward(285);
turnright(685);
forward(495);
}
else if(cm2>=200)
{
forward(95);
turnright(570);
forward(460);
}
else if(cm2>=46&&cm2<=54)
{
forward(80);
}
else if(cm2>=55&&cm2<=60)
{
turnlefts(30);
forward(1);
}
else if(cm2>=40&&cm2<=45)
{
turnlefts(30);
forward(1);
}
}
}
int irdetect(int irledpin, int ireceiverpin, long frequency)//紅外線含式
{
tone(irledpin, frequency, 8);
delay(1);
int ir = digitalRead(ireceiverpin);
delay(1);
return ir;
}
void forward(int time)//輪軸含式
{
servoright.writeMicroseconds(1420);//1420
servoleft.writeMicroseconds(1620);//1620
delay(time);
}
void turnleft(int time)
{
servoleft.writeMicroseconds(1300);//1700
servoright.writeMicroseconds(1300);//1700
delay(time);
}
void turnlefts(int time)
{
servoleft.writeMicroseconds(1440);//1540
servoright.writeMicroseconds(1440);//1540
delay(time);
}
void turnright(int time)
{
servoleft.writeMicroseconds(1700);//1300
servoright.writeMicroseconds(1700);//1300
delay(time);
}
void turnrights(int time)
{
servoleft.writeMicroseconds(1540);//1440
servoright.writeMicroseconds(1540);//1440
delay(time);
}
long microsecondsToCentimeters(long microseconds)//
{
return microseconds / 29 / 2;
}
//void pingPin()//失敗
void pinglift () //超音波含式
//const int pingPin = 11;//失敗
{
//const int pingPin = 11;//失敗
//pinglift = pingPin;//失敗
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
}
//void pingPin2()//失敗
void pingright ()
{
//const int pingPin = 2;//失敗
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
pinMode(pingPin2, INPUT);
}
//請無視下面的存在
/*
void backward(int time)
{
servoleft.writeMicroseconds(1435);//
servoright.writeMicroseconds(1595);//
delay(time);
}
*/
void stp(int time)
{
servoleft.writeMicroseconds(1500);//
servoright.writeMicroseconds(1500);//
delay(time);
}
我該如何修改才能把紅外線當成只是出發的工具 請求指導 |
|