Saturday, March 01, 2014

Multiple HC-SR04 Sensors on One Arduino Board



HC -SR - 04 trig pins connect to Arduino pins 2, 4, 6, 8, 10, 12.

HC -SR - 04 echo pins connect to Arduino pins 3, 5, 7, 9, 11, 13.




Adjust this line:


  for(int i = 0; i < 6; i ++) {

to change the number of sensors to less than six.




Data is sent as MIDI formatted high resolution pitch bend data. Every new ping sensor has a dedicated channel in this way. The resolution is up to 14 bit.




Arduino Code:
 
 
#define led 11
#define led2 10
unsigned int j; 
byte flag = 0; 

void setup() {
  Serial.begin (57600);
  for(int i = 0; i < 6; i ++) {
    pinMode((i * 2) + 2, OUTPUT); 
    pinMode((i * 2) + 3, INPUT); 
  }

  // pinMode(led, OUTPUT);
  // pinMode(led2, OUTPUT);
}

void loop() {

  for(int i = 0; i < 6; i ++) {
    readHCSR04((i * 2) + 2, (i * 2) + 3, i + 1);
  }

  delay(100); 

}

void readHCSR04(int trigPin, int echoPin, int sensorNumber) {
  long duration, distance;
  byte working; 
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  // delayMicroseconds(1000); // - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH, 100000);
    

    Serial.write(0xE0 + sensorNumber); 
    working = duration & 0x7F; 
    Serial.write(working); 
    working = (duration >> 7) & 0x7F;
    Serial.write(working);
    
    
 //   delay(10); 
  
}



0 comments: