두개 초음파센서를 연결해서 사용할려고 합니다. 한 개 초음파센서만 출력이 되고 하나는 작동이 안되는데요
3핀짜리 초음파센서를 사용합니다.... 봐주시고 수정좀 부탁드려요....
#include
// TX_PIN is not used by the sensor, since that the it only transmits!
#define PING_RX_PIN 7
#define PING_TX_PIN 7
#define PING_RX_PIN2 8
#define PING_TX_PIN2 8
SoftwareSerial mySerial(PING_RX_PIN, PING_TX_PIN);
long inches = 0, mili = 0, cm = 0;
byte mybuffer[4] = {0};
byte bitpos = 0;
SoftwareSerial mySerial1(PING_RX_PIN2, PING_TX_PIN2);
long inches1 = 0, mili1 = 0, cm1 = 0;
byte mybuffer1[4] = {0};
byte bitpos1 = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial1.begin(9600);
}
void loop() {
bitpos = 0;
while (mySerial.available())
{
// the first byte is ALWAYS 0xFF and I'm not using the checksum (last byte)
// if your print the mySerial.read() data as HEX until it is not available, you will get several measures for the distance (FF-XX-XX-XX-FF-YY-YY-YY-FF-...). I think that is some kind of internal buffer, so I'm only considering the first 4 bytes in the sequence (which I hope that are the most recent! :D )
if (bitpos < 4)
{
mybuffer[bitpos++] = mySerial.read();
}
else break;
}
mySerial.flush(); // discard older values in the next read
mili = mybuffer[1]<<8 | mybuffer[2]; // 0x-- : 0xb3b2 : 0xb1b0 : 0x--
inches = 0.0393700787 * mili;
cm = 2.54 * inches;
Serial.print("Serial =");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(300);
bitpos1 = 0;
while (mySerial1.available()) {
// the first byte is ALWAYS 0xFF and I'm not using the checksum (last byte)
// if your print the mySerial.read() data as HEX until it is not available, you will get several measures for the distance (FF-XX-XX-XX-FF-YY-YY-YY-FF-...). I think that is some kind of internal buffer, so I'm only considering the first 4 bytes in the sequence (which I hope that are the most recent! :D )
if (bitpos1 < 4) {
mybuffer1[bitpos1++] = mySerial1.read();
} else break;
}
mySerial1.flush(); // discard older values in the next read
mili1 = mybuffer1[1]<<8 | mybuffer1[2]; // 0x-- : 0xb3b2 : 0xb1b0 : 0x--
inches1 = 0.0393700787 * mili1;
cm1 = 2.54 * inches1;
Serial.print("mySerial1 =");
Serial.print(cm1);
Serial.print("cm");
Serial.println();
delay(300);
}
|