Hi,
I'm working on a Industrial design project, designing a Blind spot detection system for motorcycles. Part of this is to get an Arduino model working, and this is in fact the first Arduino project I have ever done.
I am using two HC-SR04 Ultrasonic sensors connected to the board, controlling two LED's
My problem is that the system works in close proximity, attached to my bike in a garage. But as soon as I increase the range and take the motorcycle outside, the sensors cut out and stop pinging.
Here is the code I have been using, any ideas on what could be wrong is much appreciated:
const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 = 12;
const int echoPin2 = 7;
int LED1 = 13;
int LED2 = 8;
unsigned long duration;
unsigned long duration2;
int maximumRange = 12000; // Maximum range needed
int minimumRange = 0; // Minimum range needed
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
/// -------------- Sensor 1 ----------------
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH, 1000000);
/// -------------- Sensor 2 ----------------
pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Read the signal from the sensor: 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(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH, 1000000);
//convert the time into a distance
//inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
//Serial.print(inches);
//Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.println("duration");
Serial.println(duration);
Serial.println("duration2");
Serial.println(duration2);
delay(100);
if(duration < 800){
Serial.println("LED ON");
digitalWrite(LED1, HIGH);
}
if(duration > 800){
Serial.println("LED OFF");
digitalWrite(LED1, LOW);
}
if(duration2 < 800){
Serial.println("LED2 ON");
digitalWrite(LED2, HIGH);
}
if(duration2 > 800 ){
Serial.println("LED2 OFF");
digitalWrite(LED2, LOW);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}