Mini Project Arduino Code help

Hi,

I have used the code below which is for a single sound sensor. I need help in getting a code for connecting 2 sound sensors. Also is it possible to get two graphs at the same time? And is there a way to record the obtained graph?

Thank you.

The site I found the code: Using a Microphone with Arduino • AranaCorp

The code used:

//Parameters
const int micPin = A0;

//Variables
int micVal = 0;

void setup() {
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init Microphone
pinMode(micPin, INPUT);
}

void loop() {
readMicrophone();
}

void readMicrophone( ) { /* function readMicrophone */
////Test routine for Microphone
micVal = analogRead(micPin);
Serial.print(F("mic val ")); Serial.println(micVal);
if (micVal > 600) {
Serial.println("mic detected");
}
}

1 Like

Define a micPin2 and double the code.

1 Like

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

1 Like

Yes. If you are new to programming or coding whatever you are calling it, you will soon see that recognizing patterns and changing them to do more or new stuff is an essential skill.

So take a look at how micPin is used, and fake your way to having all the same things but talking about a newly introduced micPin2.

a7

1 Like

Will this be satisfactory?

//Parameters
const int micPin = A0;
const int micPin2 = A1;

//Variables
int micVal = 0;
int micVal2 = 0;

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init Microphone
  pinMode(micPin, INPUT);
  pinMode(micPin2, INPUT);
}

void loop() {
  readMicrophone();
  readMicrophone2();
}

void readMicrophone( ) { /* function readMicrophone */
  ////Test routine for Microphone
  micVal = analogRead(micPin);
  Serial.print(F("mic val ")); Serial.println(micVal);
  if (micVal > 600) {
    Serial.println("mic detected");
  }
}
void readMicrophone2( ) { /* function readMicrophone2 */
  ////Test routine for Microphone2
  micVal2 = analogRead(micPin2);
  Serial.print(F("mic val2 ")); Serial.println(micVal2);
  if (micVal2 > 600) {
    Serial.println("mic2 detected");
  }
}


2 Likes

I appreciate your response. Thank you :slight_smile: .. Yes I am new to coding.

Sure will do this next time.

Thank you so much. Much appreciated.. The code worked.
Is there a way to get two graphs? So that it's easier to detect the sound obtained in each sensor?
The image uploaded below is what I get in the serial plotter.

You can print multiple values to the serial potter and it will draw them in different colors. If the data is well behaviour, the auto scaling isn’t a problem. I sometimes just write constants at the extremes to keep it from juggling the scale, like lower than the minimum and higher than the max you expect to see.

You can screen grab, obvsly, or do a run and capture the data and use it i a program that produces graphs. I use Open Office spreadsheet, which isn’t Excel and is cheaper and isn’t Microsoft, that’s just me. Cheap and bitter.

a7

1 Like

For several variables with labels.
https://dreamonward.com/2020/07/25/arduino-serial-plotter-labels/

1 Like

Nice.

I’ve only ever used plotting comma separated values, the simplest syntax from your chart.

a7

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.