Hello all,
I am working on achieving communication using HCSR04. In my project, a temperature (DS18B20) sensor and a HCSR04 is interfaced to Arduino. The measured values bty DS18B20 is converted to binary and then transmitted through HCSR04. For ex; 80C is given by 1000. Now this is transmitted through HCSR04, which is received by another HCSR04 kept at some distance. When I have to transmit 1, a high pulse is written on trigger pin and when 0 is to be sent, low is written on trigger pin. The HCSR04 acting as receiver must analyze these received pulses and converted them back to ones and zeroes.
How can I change the distance measuring (given below) to achieve the above?
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate “out of range” */
Serial.println(“-1”);
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}