Robofun 機器人論壇
標題:
ARDUINO高手能幫解嗎@@
[打印本頁]
作者:
iverson23760
時間:
2012-9-3 13:33
標題:
ARDUINO高手能幫解嗎@@
這是能讓遙控車能控制上下左右 還有顯示溫濕度 距離的程式碼我希望他能在距離障礙物20公分時讓遙控車停止不讓他繼續往前
請問要如何修改@@
#define DHT11_PIN 0 // 溫濕度預設腳位A0
const int Motor_E1 = 5;
const int Motor_E2 = 6;
const int Motor_M1 = 7;
const int Motor_M2 = 8;
char val; //變量來接收數據的串行端口(藍芽)
const int pingPin = 11;
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(!(PINC & _BV(DHT11_PIN))); // 等待50微秒
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |=(1<<(7-i));
while((PINC & _BV(DHT11_PIN))); //等待'1'完成
}
return result;
}
void setup()
{
Serial.begin(57600);
pinMode(Motor_M1, OUTPUT);
pinMode(Motor_M2, OUTPUT);
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
Serial.println("Ready");
}
void loop()
{
if(Serial.available())
{
val = Serial.read();
switch(val)
{
case 'f': //汽車前進
forward(0, 255);
break;
case 'b': //汽車退後
backward(0, 255);
break;
case 'l': //車左轉
left(0, 255);
break;
case 'r': //車右轉
right(0, 255);
break;
case 's': //車停止
motorstop(0, 0);
break;
}
}
byte dht11_dat[5];
byte dht11_in;
byte i;
//啟動條件
// 1。下拉I / O引腳從18毫秒
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in){
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if(!dht11_in){
Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);
//現在已經準備好進行數據接收
for (i=0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");
delay(1000);
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void motorstop(byte flag, byte motorspeed)
{
Serial.println("Stop!");
digitalWrite( Motor_E1, motorspeed);
digitalWrite( Motor_E2, motorspeed);
}
void forward(byte flag, byte motorspeed)
{
Serial.println("Forward!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, motorspeed);
}
void backward(byte flag, byte motorspeed)
{
Serial.println("Backward!");
digitalWrite( Motor_M1, LOW);
digitalWrite( Motor_M2, LOW);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, motorspeed);
}
void right(byte flag, byte motorspeed)
{
Serial.println("Turn Right! ");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, motorspeed);
analogWrite( Motor_E2, 0);
}
void left(byte flag, byte motorspeed)
{
Serial.println("Turn Left!");
digitalWrite( Motor_M1, HIGH);
digitalWrite( Motor_M2, HIGH);
analogWrite( Motor_E1, 0);
analogWrite( Motor_E2, motorspeed);
}
作者:
mzw2008
時間:
2012-9-3 21:58
把偵測距離的程式包裝起來
然後在移動前呼叫他來判斷是否20以下要停了
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2