please can anyone help me to combine 2 sensor HC-SR04 in 1 arduino, i have a problem with it. I just can make a sketch with 1 sensor, and when i make it with two, it is not working as what i want. i use motor DC as output, this is my sketc:
// written at: luckylarry.co.uk
//edited by: Hanif and Akmal
// variables to take x number of readings and then average them
// to remove the jitter/noise from the HC-SR04 sonar readings
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
unsigned int averageDistance = 0; // stores the average value
const int numOfReadings2 = 10; // number of readings to take/ items in the array
int readings2[numOfReadings2]; // stores the distance readings in an array
int arrayIndex2 = 0; // arrayIndex of the current item in the array
int total2 = 0; // stores the cumlative total
unsigned int averageDistance2 = 0;
// setup pins and variables for SRF05 sonar device
const int echoPin = 2; // SR04 echo pin (digital 2)
const int triggerPin = 3; // SR04 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
const int echPin = 7; // SR04 echo pin ke 2 (digital 4)
const int triggPin = 6; // SR04 trigger pin ke 2 (digital 5)
unsigned long pulseTime2 = 0; // stores the pulse in Micro Seconds
unsigned long distance2 = 0; // variable for storing the distance (cm)
// setup pins/values for motor
int motor2Pin = 10;
int motor2Value = 0;
int motorPin = 9; // motor pin, tersambung ke digital PWM pin 9
int motorValue = 0;
//setup
void setup() {
pinMode(triggerPin, OUTPUT); // set pin trigger sebagai output'
pinMode(triggPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motorPin, OUTPUT); // set motor pin sebagai output
pinMode(echPin, INPUT);
pinMode(echoPin, INPUT); // set echo pin sebagai input
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
// initialize the serial port, lets you view the
// distances being pinged if connected to computer
Serial.begin(9600);
}
// menjalankan perintah
void loop() {
digitalWrite(triggerPin, HIGH); // trigger pin dinyalakan
if (averageDistance > 150) // jika jarak rata-rata lebih dari 150 cm
analogWrite(motorPin, 0); // motor pin tidak bergetar
delayMicroseconds(10);
if (averageDistance <= 150)
analogWrite(motorPin, 250-averageDistance); //jika jarak rata-ratanya kurang dari sama dengan 150 cm, motor akan bergerak dengan kecepatan sesuai dengan jarak sensor terhadap objek
delayMicroseconds(10);
digitalWrite(triggerPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
// if the distance is less than 255cm then change the brightness of the LED
if (averageDistance <= 150) {
motorValue = 250 - averageDistance; // semakin dekat jaraknya semakin besar getarannya
}
analogWrite(motorPin, motorValue); // Write current value to LED pins
//Serial.println(averageDistance, DEC); // print out the average distance to the debugger
delay(10); // wait 10 milli seconds before looping again
//sensor kedua
digitalWrite(triggPin, HIGH); // trigger pin dinyalakan
if (averageDistance2 > 100) // jika jarak rata-rata lebih dari 150 cm
analogWrite(motor2Pin, 0); // motor pin tidak bergetar
delayMicroseconds(10);
if (averageDistance2 <= 100)
analogWrite(motor2Pin, 150-averageDistance2); //jika jarak rata-ratanya kurang dari sama dengan 150 cm, motor akan bergerak dengan kecepatan sesuai dengan jarak sensor terhadap objek
delayMicroseconds(10);
digitalWrite(triggPin, LOW); // stop sending the pulse
pulseTime2 = pulseIn(echPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance2 = pulseTime2/58; // Distance = pulse time / 58 to convert to cm.
total2= total2 - readings2[arrayIndex2]; // subtract the last distance
readings2[arrayIndex2] = distance2; // add distance reading to array
total2= total2 + readings2[arrayIndex2]; // add the reading to the total
arrayIndex2 = arrayIndex2 + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex2 >= numOfReadings2) {
arrayIndex2 = 0;
}
averageDistance2 = total2 / numOfReadings2; // calculate the average distance
// if the distance is less than 255cm then change the brightness of the LED
if (averageDistance2 <= 100) {
motor2Value = 150 - averageDistance2; // semakin dekat jaraknya semakin besar getarannya
}
analogWrite(motor2Pin, motor2Value); // Write current value to LED pins
Serial.print("sensor1: ");
Serial.print(averageDistance, DEC);
Serial.print(" sensor2: ");
Serial.println(averageDistance2, DEC); // print out the average distance to the debugger
delay(10); // wait 10 milli seconds before looping again
}
PLEASE HELP ME!!