Friday, February 28, 2014

Multiple Ping Sensors on One Arduino Board




Ping data pins connected to Arduino 2, 4, 6, 8 etc for each sensor



Adjust this line:

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

to increase the number of sensors beyond one sensor.




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);

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

void loop() {

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

  delay(100); 

}

void readHCSR04(int trigPin, int echoPin, int sensorNumber) {
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  byte working; 

  // 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(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, 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(trigPin, INPUT);
  duration = pulseIn(trigPin, HIGH);

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

}




0 comments: