So I just recently made an Arduino device that is fairly simple. It is hooked up to a microphone that takes a reading of how loud a sound is, finds the magnitude of the sound analog value every 50 ms or so, stores a minimum and maximum value, uses that to find the amplitude (volume of the sound), and depending on the loudness of the sound, powers on a certain color LED.
In my code chronologically , I declare all the pin holes, get a loop going to keep taking sound measurements and calculating the amplitude with that, covert that measurement to voltage, and have various if else statements to light up different lights corresponding to the amplitude.
This is where the confusing part comes in. If I try to view my serial monitor which I have the voltage printed out to, it actually CHANGES the value that the sensor is reading. How do i know this? I could be in an absolutely quiet room with the lowest level sound light lit up, but as soon as I open the monitor, the numbers jump up and other lights light up right until I close the serial monitor.
I will also sometimes, with EXTREMELY slight variations in the code (I'm talking just changing the voltage threshold for a certain LED to light up), my serial monitor will show readings jumping up and down vigorously, like from 0 to 1.5 volts repeatedly, even in a very quiet room.
Does anybody know what could be causing this? I don't think it's a microphone issue because I've tried like three different mics, and I don't think it's a circuit interference, because I've put together the cirucuit like three times.
Here is my code:
// declare pin hole numbers by color
int ledG = 2;
int ledB = 3;
int ledPU = 4;
int ledPI = 5;
int ledW = 6;
int ledY = 7;
int ledO = 8;
int ledR = 9;
int input = A1;
// declare values pertinent to sound collection
const int windowOfTime = 5 ;
const int windowOfTimeLight = 20;
void setup() {
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(ledPU, OUTPUT);
pinMode(ledPI, OUTPUT);
pinMode(ledW, OUTPUT);
pinMode(ledY, OUTPUT);
pinMode(ledO, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(input, INPUT);
digitalWrite(ledG, LOW);
digitalWrite(ledB, LOW);
digitalWrite(ledPU, LOW);
digitalWrite(ledPI, LOW);
digitalWrite(ledW, LOW);
digitalWrite(ledY, LOW);
digitalWrite(ledO, LOW);
digitalWrite(ledR, LOW);
Serial.begin(9600);
}
void loop() {
// Aquires time at a given moment
unsigned long startMillis = millis();
unsigned int peakDifference = 0;
unsigned int maxSound = 0;
unsigned int minSound = 1024;
// if the difference in time between when you intially got the time, and the time that appears in the while statement
// (which I'm assuming starts a new timer after the first millis) is greater less than the window of time that I want in milliseconds, begin aquisition
while (millis() - startMillis < windowOfTime)
{
int input = analogRead(A1);
// Remove mistakes from machines
if (input < 1024)
{
if (input > maxSound)
{
maxSound = input;
}
else if (minSound > input)
{
minSound = input;
}
}
}
peakDifference = maxSound - minSound;
//conversion from analog to voltage
double volts = (peakDifference * 3.3) / 1024;
delay(50);
Serial.println(volts, DEC);
//g
if(volts < .05)
{
for(int i=2; i<10; i++)
{
if (i < 3)
{
digitalWrite(i, HIGH);
}
else if (i > 2)
{
digitalWrite(i,LOW);
}
}
}
//b
else if(volts < .1)
{
for(int i=2; i<10; i++)
{
if (i < 4)
{
digitalWrite(i, HIGH);
}
else if (i > 3)
{
digitalWrite(i,LOW);
}
}
}
//pu
else if(volts < .15)
{
for(int i=2; i<10; i++)
{
if (i < 5)
{
digitalWrite(i, HIGH);
}
else if (i > 4)
{
digitalWrite(i,LOW);
}
}
}
//pi
else if(volts < .2)
{
for(int i=2; i<10; i++)
{
if (i < 6)
{
digitalWrite(i, HIGH);
}
else if (i > 5)
{
digitalWrite(i,LOW);
}
}
}
//w
else if(volts <.25 )
{
for(int i=2; i<10; i++)
{
if (i < 7)
{
digitalWrite(i, HIGH);
}
else if (i > 6)
{
digitalWrite(i,LOW);
}
}
}
//y
else if(volts < .3)
{
for(int i=2; i<10; i++)
{
if (i < 8)
{
digitalWrite(i, HIGH);
}
else if (i > 7)
{
digitalWrite(i,LOW);
}
}
}
//o
else if(volts < .35)
{
for(int i=2; i<10; i++)
{
if (i < 9)
{
digitalWrite(i, HIGH);
}
else if (i > 8)
{
digitalWrite(i,LOW);
}
}
}
//r
else if(volts <.4 )
{
for(int i=2; i<10; i++)
{
if (i < 10)
{
digitalWrite(i, HIGH);
}
}
}
}
Please let me know if y'all know what's going on.