user86808: I got this code from robojax.com
i have follow all the step but when i try to clap its not turn off or turn on
but when i blowing on the sound sensor it reacts and makes the light turn on
int soundInPin = 2;// connect output to Sound module DO
int relayPin = 8;// Connected to relay or buzzer (or LED)
int extra5V = 12;// define a pin for extra extra 5V

// do not change values bellow
int val = 0; // sound value from pin 2
int relayON = 0;//light status
int heard = 0;//sound heard status


void setup() {

//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338   
  Serial.begin(9600);
  pinMode(soundInPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
  pinMode(extra5V,OUTPUT);// set extra5V as output
  digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V

}

void loop() {
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  val = digitalRead(soundInPin);// read the sound pin

  if(val == HIGH && relayON == LOW){

    heard = 1-heard;// toggle the value of "heard" from HIGH to LOW or from LOW to HIGH
    delay(100);
  }    
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  relayON = val;// save the value of pin 2

      if(heard == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); // turn relay ON
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);// turn relay OFF
   
      }     


//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
} 


user40318: This is not an answer, so I will delete it after you read it.
Since you are a beginner, here is a comment on programming style.
Your code feels very cluttered and overwhelming because the comments are made a part of the program lines.
There is a difference in readability between these two lines.
digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V
digitalWrite(extra5V,HIGH);                // turn the extra5V pin HIGH to get 5V
The comments are there for future, like next year, to remind you what the program does.
Also the following two lines are on the same indentation level
  relayON = val;     // save the value of pin 2

      if(heard == HIGH){

and should be
  relayON = val;     // save the value of pin 2

  if(heard == HIGH){

With some formatting changes, your code could look something like this.
int soundInPin =  2;                       // connect output to Sound module DO
int relayPin   =  8;                       // Connected to relay or buzzer (or LED)
int extra5V    = 12;                       // define a pin for extra extra 5V

// do not change values below

int val        =  0;                       // sound value from pin 2
int relayON    =  0;                       // light status
int heard      =  0;                       // sound heard status


void setup() {                             // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  Serial.begin(9600);

  pinMode(soundInPin, INPUT_PULLUP);
  pinMode(relayPin,   OUTPUT);
  pinMode(extra5V,    OUTPUT);             // set extra5V as output

  digitalWrite(extra5V,HIGH);              // turn the extra5V pin HIGH to get 5V

}

void loop() {                              // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  val = digitalRead(soundInPin);           // read the sound pin

  if (val == HIGH && relayON == LOW) {

    heard = 1-heard;                       // toggle the value of "heard" from HIGH to LOW or from LOW to HIGH
    delay(100);

  }                                        // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  relayON = val;                           // save the value of pin 2

  if (heard == HIGH) {

    Serial.println("Light ON");
    digitalWrite(relayPin, LOW);           // turn relay ON

  } else {

    Serial.println("Light OFF");
    digitalWrite(relayPin, HIGH);          // turn relay OFF

  }
                                           // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
}


86808: <p>I got this code from robojax.com
i have follow all the step but when i try to clap its not turn off or turn on
but when i blowing on the sound sensor it reacts and makes the light turn on</p>
<pre class="lang-cpp prettyprint-override"><code>int soundInPin = 2;// connect output to Sound module DO
int relayPin = 8;// Connected to relay or buzzer (or LED)
int extra5V = 12;// define a pin for extra extra 5V

// do not change values bellow
int val = 0; // sound value from pin 2
int relayON = 0;//light status
int heard = 0;//sound heard status


void setup() {

//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338   
  Serial.begin(9600);
  pinMode(soundInPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
  pinMode(extra5V,OUTPUT);// set extra5V as output
  digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V

}

void loop() {
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  val = digitalRead(soundInPin);// read the sound pin

  if(val == HIGH &amp;&amp; relayON == LOW){

    heard = 1-heard;// toggle the value of &quot;heard&quot; from HIGH to LOW or from LOW to HIGH
    delay(100);
  }    
//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  relayON = val;// save the value of pin 2

      if(heard == HIGH){
        Serial.println(&quot;Light ON&quot;);
        digitalWrite(relayPin, LOW); // turn relay ON
       
      }else{
        Serial.println(&quot;Light OFF&quot;);
        digitalWrite(relayPin, HIGH);// turn relay OFF
   
      }     


//Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
} 
</code></pre>
 40318: <p>This is not an answer, so I will delete it after you read it.</p>
<p>Since you are a beginner, here is a comment on programming style.</p>
<p>Your code feels very cluttered and overwhelming because the comments are made a part of the program lines.</p>
<p>There is a difference in readability between these two lines.</p>
<p><code>digitalWrite(extra5V,HIGH);// turn the extra5V pin HIGH to get 5V</code></p>
<p><code>digitalWrite(extra5V,HIGH);                // turn the extra5V pin HIGH to get 5V</code></p>
<p>The comments are there for future, like next year, to remind you what the program does.</p>
<p>Also the following two lines are on the same indentation level</p>
<pre><code>  relayON = val;     // save the value of pin 2

      if(heard == HIGH){
</code></pre>
<p>and should be</p>
<pre><code>  relayON = val;     // save the value of pin 2

  if(heard == HIGH){
</code></pre>
<p>With some formatting changes, your code could look something like this.</p>
<pre><code>int soundInPin =  2;                       // connect output to Sound module DO
int relayPin   =  8;                       // Connected to relay or buzzer (or LED)
int extra5V    = 12;                       // define a pin for extra extra 5V

// do not change values below

int val        =  0;                       // sound value from pin 2
int relayON    =  0;                       // light status
int heard      =  0;                       // sound heard status


void setup() {                             // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  Serial.begin(9600);

  pinMode(soundInPin, INPUT_PULLUP);
  pinMode(relayPin,   OUTPUT);
  pinMode(extra5V,    OUTPUT);             // set extra5V as output

  digitalWrite(extra5V,HIGH);              // turn the extra5V pin HIGH to get 5V

}

void loop() {                              // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  val = digitalRead(soundInPin);           // read the sound pin

  if (val == HIGH &amp;&amp; relayON == LOW) {

    heard = 1-heard;                       // toggle the value of &quot;heard&quot; from HIGH to LOW or from LOW to HIGH
    delay(100);

  }                                        // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338

  relayON = val;                           // save the value of pin 2

  if (heard == HIGH) {

    Serial.println(&quot;Light ON&quot;);
    digitalWrite(relayPin, LOW);           // turn relay ON

  } else {

    Serial.println(&quot;Light OFF&quot;);
    digitalWrite(relayPin, HIGH);          // turn relay OFF

  }
                                           // Robojax Arduino Step By Step Course http://robojax.com/L/?id=338
  delay(100);
}
</code></pre>
