Code review: Sub from SensorValue, have triggerpoint react to moded reading

I have taken a new approach - see post

Hello, I have a microhone connected to an Arduino 1 that controls a relay based on the value from the Microphone. For some reason the microphone wont give me any readings above 665 (don't know if this has to do with the micrphone being hooked to the 3.3V pin).
This problem limits the upper limit of loudness before the relay switches, since i cannot just set a higher triggerpoint, because I won't get a higher reading.

I know i can put in another unit, that controls the voltage coming to the board and that would allow me to operate in lower values at a higher volume. I wish not to install another unit as this is not a single instalp, so my hope is, that i can subtract a given value from the AnalogRead and make that the new Sensorvalue that is compared to the triggerpoint.

If poosible, i would like to still have the original readings side by side with the modified reading in the serial monitor, but that's secondary.
If it's not too much trouble, it would be a huge help to both get the code bit and how to impolement it in the code.

Thank you all in advance.
(BTW, i have had help with a good chunk of the code you see, so it does not represent my skille level and understanding, so please explain thorougly :slight_smile: )

//Program variables setup:
 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=5000;
 //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
 }

void loop(){
  //You can adjust the triggerlevel in steps from 0-1023 (0-5V), depending on the wished for soundlimit
  int triggerpoint=665;
  
//Turn the relayPin on to activate speakers:
  digitalWrite(relayPin,HIGH);

  //read the value from the microphone:
  sensorValue=analogRead(sensorPin);

  //converts to volts
  double volts=(sensorValue*3.3)/1024;

  //Send the reading to serial
  Serial.print(sensorValue);
   //Serial.print("   "); //Gives a little space - somebody recommended it ahead of TAB-function. Does not seem like it is needed.
   Serial.print("\t"); //TAB function
   Serial.print(volts);//.prinln as opposed to just .print creates a new line afterwards
   Serial.println(" V");
   

  
  //Now check if sound is too high:
  if(sensorValue>triggerpoint){digitalWrite(relayPin,LOW);
  }

  //Set the timeout in millisecond for the music:
  delay(myDelay);

  //The program will now repeat itself, thus turning the music on after the delay.
}

I have tried around myself a bit now, but i do not have my unit at hand - would you expect this to solve my problem?

Again thank you :slight_smile:

/*
 Lydaktiveret relæ.
 Rælæet er aktiveret indtil lydstyrken fra mikrofonen når den ønskede grænseværdi,
 hvorefter rælæet afrbyder forbindelsen.
 Rælæet skal helst sidde på højtalerledningen for at undgå tænd/sluk "pop" fra forstærkeren.
 */

 //Program variables setup:
 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=5000;
 //Forsinkelsen før programemt genstarter

 int triggerPoint=0;
 //Triggerpoint for the relaymodule

 int subT=35;
//Value subtracted from sensorValue before compared to triggerPoint

 void setup() {
  pinMode(relayPin,OUTPUT);
  //Declares the relaypin as an output

  Serial.begin(9600); 
  //Sets up serial monitor in Arduino IDE
 }

void loop(){
  //You can adjust the triggerlevel in steps from 0-1023 (0-5V), depending on the wished for soundlimit
  int triggerpoint=665;
  
//Turn the relayPin on to activate speakers:
  digitalWrite(relayPin,HIGH);

  //read the value from the microphone:
  sensorValue=analogRead(sensorPin)-subT;

  //converts to volts
  double volts=(sensorValue*3.3)/1024;

  //Send the reading to serial
  Serial.print(sensorValue);
  Serial.print(sensorValue+subT);
   //Serial.print("   "); //Gives a little space - somebody recommended it ahead of TAB-function. Does not seem like it is needed.
   Serial.print("\t"); //TAB function
   Serial.print(volts);//.prinln as opposed to just .print creates a new line afterwards
   Serial.println(" V");
   

  
  //Now check if sound is too high:
  if(sensorValue>triggerpoint){digitalWrite(relayPin,LOW);
  }

  //Set the timeout in millisecond for the music:
  delay(myDelay);

  //The program will now repeat itself, thus turning the music on after the delay.
}

If this is part of another project you have posted it would be better to merge it but let people know you have changed the code or another aspect.

You can do that with the "report to moderator" but please INCLUDE a link to the other post so we will know where to add it. I did not fully understand your report so left it open in case another moderator made more sense of it.

As far as your readings go you could also use the MAP function to spread the readings further.

Or us a small OP-AMP to give you actual greater readings (confined to the range 0 - 1023 or 0 to 5 volts)

Either would be a suitable work around.

ballscrewbob:
As far as your readings go you could also use the MAP function to spread the readings further.

Or us a small OP-AMP to give you actual greater readings (confined to the range 0 - 1023 or 0 to 5 volts)

Either would be a suitable work around.

Thank you - i have now linked the other post and put in an edit - I hope it is okay?

I regards to your response:

Would the following work as hoped? I would have to play around with a new triggerpoint, but other than that? Have i understood the mapping function correctly and is it correct to then compare the mNumb value to the triggerpoint instead og the sensorValue?
I do not have my unit here, so i cant test it till tomorrow, where i will have to get it working :confused:

//Program variables setup:
 int sensorPin=A1;                                                                   
 int relayPin=12;                                                                     
 int sensorValue=0;                                                                   
 int myDelay=5000;                                                                   
 int triggerPoint=0;                                                                  
 unsigned int = mNumb;                                                                

 void setup() {
  pinMode(relayPin,OUTPUT);                                                           

  Serial.begin(9600);                                                                 
 }

void loop(){
  
  int triggerpoint=665;    
  mNumb = map(analogRead(sensorPin), 0, 665, 0, 1023);    
  digitalWrite(relayPin,HIGH); 
  sensorValue=analogRead(sensorPin);   
  double volts=(sensorValue*3.3)/1024;                                              
  Serial.print(mNumb);                                                                 
  Serial.print("\t")
  Serial.print(sensorValue);                                                          
   Serial.print("\t");                                    
   Serial.print(volts);                                                             
   Serial.println(" V");
   
  if(mNumb>triggerpoint){digitalWrite(relayPin,LOW);                            
  }

  delay(myDelay);                                                                     }

Your declaration of mNumb is not correct. You can also move all those variables inside loop() since you never use them anywhere else

//Program variables setup:
const int sensorPin = A1;
const int relayPin = 12;
const int myDelay = 5000;

void setup() {
  pinMode(relayPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  int triggerpoint = 665;
  int sensorValue = analogRead(sensorPin);
  int mNumb = map(sensorValue, 0, 665, 0, 1023);
  digitalWrite(relayPin, HIGH);
  double volts = (sensorValue * 3.3) / 1024;
  Serial.print(mNumb);
  Serial.print("\t")
  Serial.print(sensorValue);
  Serial.print("\t");
  Serial.print(volts);
  Serial.println(" V");

  if (mNumb > triggerpoint) {
    digitalWrite(relayPin, LOW);
  }
  delay(myDelay);
}

blh64:
Your declaration of mNumb is not correct. You can also move all those variables inside loop() since you never use them anywhere else

Thank you very much - i will try this tomorrow when I have units at hand again and report back if it solves my problem.