Trying to use Sound Sensor To Read if Pin is High

Hi,

I'm using a sound sensor with an Arduino Uno. I'm trying to have once the sound sensor reads past a level 9 to turn the pin high. Also as a way to check it I'm trying to get it to print "The pin is high."

It's showing me the numeric value of the Analog read, and that I'm going past level 9. But it does not show "The line is high."

Any help is appreciated.

void setup() {
  pinMode(4, OUTPUT); //setup pin 4 as output
  pinMode(A0, INPUT); // setup input
  Serial.begin(9600); // setup serial
}

void loop() {
  if(analogRead(A0) > 9) { //if the analog value of A0 is greater than 9 then
    digitalWrite(4, HIGH); //write digital pin 4 high
  }
  else {
    digitalWrite(4, LOW); //if it isn't higher than 9, make pin 4 low
  }

Serial.println(analogRead(A0));
delay(100);

if (4 == HIGH)

Serial.println("The pin went high");
delay(100);

}
if (4 == HIGH)

Serial.println("The pin went high");
delay(100);

Two problems right there. Read closely about true and false and the if() construct.

if (4 == HIGH)

So, if the number 4 is ever exactly equal to the value defined by HIGH (which it won't be), execute the Serial.println because there are no curly brackets so the if statement continues to the semicolon. Probably NOT what you intended.

Hi Vaj,

Thank you for the help. I think I fixed the first part of what you are saying by defining int - please see below. Thanks again.

int LedOutput = 6;
int SoundSensor = A0;

void setup() {

  pinMode(LedOutput, OUTPUT); //make sure you have set your pins as inputs or outputs
  pinMode(SoundSensor, INPUT); // setup input
  Serial.begin(9600); // setup serial
}

void loop() {
  if(analogRead(SoundSensor) > 9) { //if the analog value of SoundSensor/Input A0 is greater than 9 then
    digitalWrite(LedOutput, HIGH); //write digital pin LedOutput high
  }
  else {
    digitalWrite(LedOutput, LOW); //if it isn't higher than 9, make pin LedOutput low
  }

Serial.println(analogRead(A0));
delay(100);


  if (digitalRead(LedOutput) == HIGH){

  Serial.println("The line went high");
  delay(1000);
  }

Hi,
Instead of reading the analog input each time you need it, read it at the top of your void loop(), save it to a variable and then use that variable for comparisons and printouts through the rest of your loop.

Tom... :slight_smile:

TOM GEORGE!!

YOU DID IT!! All I had to do was move the Serial Println to the top of the void loop and it worked!!!!! I think it was also incorrectly placed in if bracket and interfering. THANK YOU!

Hi,
Good to hear.
Can you post your edited/working code please?

Thanks.. Tom.. :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.