I am trying to read data info from two ultrasonic sensors and transmitt them bak to back.
Here is my code, it doesnt transmit for some reason, it is going through the loop and it is emabling the sensors to read but it doesnt transmit. TX LED doesnt flash. ANY idea will be extremely appreciated.
const int pingPin1 = 7; // CONNECT SENSOR 1 to pin 7
const int pingPin2 = 8; // CONNECT SENSOR 1 to pin 7
long DataAvg[8];
void setup() {
// initialize serial communication:
Serial.begin(115200);
// initialize the data averaging filter
int i;
for(i = 0; i < 8; i++)
{
DataAvg[i] = 0;
}
}
int ping(int pingPin)
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, mm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
mm = (duration / 3) / 2;
// Filter the data
int i;
long FiltData = 0;
for(i = 7; i >= 1; i--)
{
DataAvg[i] = DataAvg[i-1];
FiltData += DataAvg[i];
}
DataAvg[0] = mm;
FiltData = (FiltData + DataAvg[0]) >> 3;
// Truncate the data if it's too big so the value is contained within a single byte
int pingData = 0;
if(FiltData >= 255)
pingData = 255;
else if(FiltData <= 0)
pingData = 0;
else
pingData = FiltData & 0xff;
return pingData;
}
void loop()
{
// Send the data
Serial.write(ping(pingPin1) >> 1);
delay(100);
Serial.write((ping(pingPin2) >> 1) + 0x80);
delay(100);
}