Hello, I am trying to make a unit that reacts to sound. It is supposed to break power to the sound once the limit is reached. Problem is, that spikes in sound appear, and this shuts down the music in a way i dont want.
I have tried to work around this by measuring the amount of times the threshold has been crossed within a period of time. If the threshold has been crossed more than x times within the period of time, then the power should break.
The time should then be adjusted for another run.
My question is:
Have I set up the code correctly, so that the "count++" part runs and hits perhaps 5 times as the limit has been crossed 5 times, before i check if the new value of "count>2"?
Or have I made it so, that it adds 1 to count, then checks if "count>2" and then the rest of the code is run in a way, so that i will never have "count>2" no matter what?
I hope it makes sense - english is not my first language, and that it is possible, thank you for your help.
/*
Soundactivated relay.
Relay allows for currentflow until microphone reaches defines upper limit a certain amount of times within a period of time.
If the limit has been passed a certain amount of times within the timelimit, the relay will shut of currentflow.
The relay should be placed directly on the wire to the speaker to avoid "popping" from the amplifier.
*/
//Program variables setup:
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000; //period during which button input is valid
int count = 0;
boolean counting; //EDIT - corrected mistake
int sensorPin=A1; //selects the input pin for the microphone
int relayPin=12; //selects the pin for the relaymodule
int sensorValue=0; //Variable to store the value coming from the sensor
int myDelay=10000; //Forsinkelsen før programemt genstarter
int triggerPoint=0; //Triggerpoint for the relaymodule
void setup() {
pinMode(relayPin,OUTPUT); //Declares the relaypin as an output
Serial.begin(9600); //Sets up serial monitor in Arduino IDE
counting = true; //turns on the counting indicator
startMillis = millis(); //Saves the starttime
}
void loop(){
//You can adjust the triggerlevel in steps from 0-1023 (0-5V), depending on the wished for soundlimit
int triggerpoint=665;
currentMillis = millis(); //gets current time
digitalWrite(relayPin,HIGH); //Turn the relayPin on to activate speakers:
if (currentMillis-startMillis <= period) //true until period has passed, so it is true for "period" seconds
{
sensorValue=analogRead(sensorPin); //read the value from the microphone:
double volts=(sensorValue*3.3)/1024; //converts to volts
Serial.print(sensorValue); //Prints sensorValue
Serial.print("\t"); //TAB function
Serial.print(volts); //.prinln as opposed to just .print creates a new line afterwards
Serial.println(" V");
if (sensorValue>triggerpoint)
{
count++; //add one to the count
Serial.println(count);
}
if (count>2)
{
digitalWrite(relayPin,LOW);
}
else //period has ended
{
if (counting == true) //if we were counting
{
Serial.print("Time is up");
counting = false; //prevents the message from being displayed again
}
}
}
startMillis = currentMillis; // Gives startMillis new value, så when the loop restarts, the period has not elapsed in "if (currentMillis-startMillis <= period)"
delay(myDelay);
}