In order to test an innovation of mine, I've taken it upon myself to learn a bit of Arduino to help facilitate my ventures. That being said, it's been a bit more difficult than I anticipated, given that one of the code scripts I encountered to test my microphone sensor, seems to have a lot of mistakes. Unlike my more traditional coding experiences (with Python and MATLAB), the Arduino editor has a type of initialization script along with a loop or action script hardcoded into it's program. So this change in format alone has been rather heady, and is causing what is likely the majority of my confusion.
int sensor_value = 0;
int threshold = 452; //threshold value
int abs_value = 0;
int ledCount = 10; //number of Bargraph LEDs
int bargraph[] = {2, 3, 4, 5, 6, 7, 8, 9,10,11}; // Bargraph pins
void setup() {
Serial.begin(9600); // setup serial
for (int i = 0; i <= ledCount; i++) // Define bargraph pins OUTPUT
{
pinMode(bargraph[i], OUTPUT);
}
for (int i = 0; i < ledCount; i++)
{
digitalWrite(i, LOW);
}
}
void loop() {
sensor_value = analogRead(A0);
abs_value = abs(sensor_value - threshold);
int ledLevel = map(abs_value, 0, (700- threshold), 0, ledCount);
for (int i = 0; i < ledCount; i++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (i <= ledLevel) {
digitalWrite(bargraph[i], HIGH);
Serial.println(i);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(bargraph[i], LOW);
}
}
}
In the video above, you'll see my rather sad attempt at getting my board to both recognize and properly index the sound level over to a digital bar graph. I can't tell if it's my threshold value, the potentiometer settings of my microphone, or if it's the code itself that's causing the issue. The code originally set the first two bar graph pins to the 1 and 0 Rx and Tx digital channels respectively, which was preventing lighting. So, I re-indexed the pins to go from digital pins 2-11 (see code above). This was supposed to work, via a one by one light up test, where each bar on the bar graph lit up, but in practice, only the first bar seems to react to sound. If you were to blow on the microphone, it would light up the higher bar levels, but seems to cap at the ninth bar, which was a previous issue in earlier testing phases.
Regardless of that long winded explanation of what went wrong, I do hope that you, my dear reader, find some amusement out of what little success I've managed to make. Currently only one bar works, but rather well I might say. The video above is from about 6-8 inches away from the computer speaker, however there was decent sensitivity from about 2 feet away.
song: "Bohemian Rhapsody" by Queen
project link: https://create.arduino.cc/projecthub/electropeak/how-to-use-ky-037-sound-detection-sensor-with-arduino-a757a7
Comments