Go Down

Topic: (ask) using multiple ultrasonic sensors as midi controller (Read 804 times) previous topic - next topic

aplatisza

hello, i'm trying to make a midi controller using multiple hcsr04, it seems running smoothly when iam using 2 sensors. but when i'm trying add 2 more it's doesnt working properly

here's the code for 2 sensors :

Code: [Select]
#define trigPin1 2
#define echoPin1 3
#define trigPin2 4
#define echoPin2 5


void setup() {
 Serial.begin(38400);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT); 
}

void loop() {   

  long duration1, distance1;
  digitalWrite(trigPin1, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;   

  long duration2, distance2;
  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;   


 
 
 if( distance1<50)
 {
  noteOn(0x90, 0x51, 0x45);
  }
  else if( distance1>101){
    noteOn(0x90, 0x53, 0x45);
  }

  delay(30);
 
 if( distance2<50)
 {
  noteOn(0x91, 0x61, 0x46);
  }
  else if( distance2>101){
  noteOn(0x91, 0x63, 0x46);
  }

 
  delay(30);
 
 
  }


 void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
                                              }


and here's code for 4 sensor, which is doesnt work properly :
Code: [Select]
#define trigPin1 2
#define echoPin1 3
#define trigPin2 4
#define echoPin2 5
#define trigPin3 6
#define echoPin3 7
#define trigPin4 8
#define echoPin4 9


void setup() {
 Serial.begin(38400);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);   
}

void loop() {   

  long duration1, distance1;
  digitalWrite(trigPin1, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;   

  long duration2, distance2;
  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;

  long duration3, distance3;
  digitalWrite(trigPin3, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3/2) / 29.1;   

  long duration4, distance4;
  digitalWrite(trigPin4, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);

  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4/2) / 29.1;   


 
 
 if( distance1<50)
 {
  noteOn(0x90, 0x51, 0x45);
  }
  else if( distance1>101){
    noteOn(0x90, 0x53, 0x45);
  }

  delay(30);
 
 if( distance2<50)
 {
  noteOn(0x91, 0x61, 0x46);
  }
  else if( distance2>101){
  noteOn(0x91, 0x63, 0x46);
  }

 
  delay(30);
 
  if( distance3<50)
 {
  noteOn(0x92, 0x71, 0x47);
  }
  else if( distance3>101){
    noteOn(0x92, 0x73, 0x47);
  }

  delay(30);
 
 if( distance4<50)
 {
  noteOn(0x93, 0x81, 0x48);
  }
  else if( distance4>101){
  noteOn(0x93, 0x83, 0x48);
  }

 
  delay(30);
 
 
  }


 void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
                                              }


i dont know where's the exact problem, on the 4 sensors code im just add another variable for additional sensors. and i'm planning to using 12 sensors 4 this project.is it power problem or something else?

thanks! :) 

AWOL

Quote
doesnt work properly
Grrr.

I'd start by factoring and shortening your code.

slipstick

If it doesn't work properly you should change it so that it does work properly.

.....or
If you want a more useful answer than that you need to tell us exactly what it does and and exactly what it was you wanted it to do. Just "doesn't work properly" is no help.

Steve

Go Up