HC-SRO4 controlled sound installation

I am attempting to make a sound installation using HC-SRO4 sensors to map the noise too, I was originally planning to adapt the AUDUINO synthesiser and have built it using potentiometers and wanted switch them out for the sensors, due to my basic knowledge of Arduino it was too difficult for me, but if anyone could help me work it out it would be greatly appreciated!!!

I am using an Arduino UNO R3

But because of this I decided to try my own, I have been able to map the distances provided by the sensors to some scales, but I don’t know how to get the Arduino to make a noise aha whether it be a tone or a play function im unsure how to make it work, ill leave the code here too.



// Include NewPing Library
#include "NewPing.h"
#include <avr/io.h>
#include <avr/interrupt.h>
// Define Constants

#define TRIGGER_PIN_1  10
#define ECHO_PIN_1     10
#define TRIGGER_PIN_2  5
#define ECHO_PIN_2     5
#define MAX_DISTANCE 4000
#define PWM_PIN 3

NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);

// Define Variables


float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor
float tone1;
float tone2;

int output;

uint16_t minorpentatonicTable[55] = {
  0,17,20,23,26,31,34,41,46,51,61,69,82,92,103,122,137,163,183,206,244,274,326,366,
  411,489,549,652,732,822,978,1097,1305,1465,1644,1955,2195,2610,2930,3288,
  3910,4389,5220,5859,6577,7821,8779,10440,11718,13153,15642,17557,20879,23436,26306
};

uint16_t mapminorPentatonic(uint16_t input) {
  uint8_t value = (1023-input) / (1024/53);
  return (minorpentatonicTable[value]);
}

uint16_t minordiatonicTable[76] = {
  0,17,19,20,23,26,27,31,34,38,41,46,51,54,61,69,77,82,92,103,109,122,137,154,163,183,206,218,244,274,308,326,366,
  411,435,489,549,616,652,732,822,871,978,1097,1232,1305,1465,1644,1742,1955,2195,2463,2610,2930,3288,
  3484,3910,4389,4927,5220,5859,6577,6968,7821,8779,9854,10440,11718,13153,13935,15642,17557,19708,20879,23436,26306
};

uint16_t mapminorDiatonic(uint16_t input) {
  uint8_t value = (1023-input) / (1024/53);
  return (minordiatonicTable[value]);
}




void setup() {
  Serial.begin (9600);
 pinMode(PWM_PIN, OUTPUT);
}

 void play ( int output ) {               
    digitalWrite(PWM_PIN, HIGH);            
    delayMicroseconds(output);            
    digitalWrite(PWM_PIN, LOW);              
    delayMicroseconds(output); 
}





void loop()
{
    
    
 distance1 = sonar1.ping_cm();
  
  // Add a delay between sensor readings
  
  delay(10);
  
  // Measure duration for first sensor
  
  distance2 = sonar2.ping_cm();
  

  
  // Send results to Serial Monitor
  
    Serial.print("Distance 1: ");

    if (distance1 >= 4000 || distance1 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance1);
    Serial.print(" cm ");
    }
    
    Serial.print("Distance 2: ");

    if (distance2 >= 4000 || distance2 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance2);
    Serial.print(" cm");
    }
  
  Serial.println(" ");

 tone1 = mapminorDiatonic( distance1 );

  tone2 = mapminorPentatonic( distance2);





}

Thanks Guys, Much appreciated!!!

Hi there! What sort of sounder(s) do you have? With a simple piezo buzzer, you can use the tone() function.

You may already know this, but a way to define notes from a chromatic scale is to take a starting tone, e.g. 440 Hz (which is a concert pitch A), and multiply it by 2^(1/12) to get one semitone up. I can expand on this if it would help :smile:

Richard

Hiya thanks for the response!

im using an 2.5mm AUX output

Cheers

Ethan

Anyone able to help?

4000 cm is 40 meters. You’ll be lucky to pick up anything much further than 4 meters. I would set MAX_DISTANCE to 500 and then use MAX_DISTANCE in your ‘if’ statements rather than 4000.

The 10 millisecond delay may be too short. Start at 30 and reduce that until the second sensor starts reading echoes from the first sensor’s ping (low readings).

The lack of a delay between the second sensor and the repeat of loop() may cause similar late echo problems. Put a similar delay after the second sensor.

Awesome I’ll have a scan through and switch it up!

Thank you