Hello everyone,
i'm facing a problem which i onestly don't understand. Maybe it is just my fault because i've been struggling with this project last days and there has always been another issue coming on, but any advice will be useful!
I am trying to make a simple audiometer with an audio sensor (KY-038) and some leds. Leds should light according to the values measured by the sensor and compared to a threshold (that's because sensor values come in a narrow range and i need to determine where my "silence point" is). I can modify the threshold with two simple buttons (one up, one down).
The problem is: when the threshold (modified using those buttons) gets to "touch" values coming from the sensor, sensor values shift, like they were "avoiding" the threshold. Please refer to this image for a better understanding:
As you can see, when the threshold, which is the red line (and is manually modified using buttons) "touches" the sensor values (blue line), sensor values shift in the other direction.
I really can not understand how a variable coming from a digital input interferes with analog values coming from a sensor.
Here's the code i'm using (it's just for testing, so don't expect any complete or optimized code):
int thresholdUp = 13;
int ledsOn;
float threshold = 0;
void setup()
{
pinMode (sound, INPUT);
pinMode(thresholdDown, INPUT_PULLUP);
pinMode(thresholdUp, INPUT_PULLUP);
for(int i=0;i<8;i++){
pinMode(ledPins[i], OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
if(digitalRead(thresholdDown) == LOW) threshold -= 0.2; //threshold is modified long-pressing buttons
if(digitalRead(thresholdUp) == LOW) threshold += 0.2;
(void)analogRead(sound);
int sensor = analogRead(sound);
Serial.print(sensor);
Serial.print(",");
Serial.println(threshold);
if(sensor < threshold){
ledsOn = 0;
}
if(sensor >= threshold && sensor < threshold+2){
ledsOn = 1;
}
if(sensor >= threshold+2 && sensor < threshold +4){
ledsOn = 2;
}
[...]
if(ledsOn == 0){
for(int i=0;i<8;i++)digitalWrite(ledPins[i],LOW);
}
for(int i=0;i<ledsOn;i++){
digitalWrite(ledPins[i],HIGH);
}
}
Thank you in advance for any advice or solution ![]()
