-1

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);
} 
jsotola
1,5342 gold badges12 silver badges20 bronze badges
asked Sep 24, 2022 at 8:21
5
  • Welcome to Arduino:SE. If you want us to be able to help you, we'll need to see how you wired the hardware together as well as your code. Commented Sep 24, 2022 at 8:34
  • Is there something on the sound sensor board to adjust the threshold? Like potentiometer ? Commented Sep 24, 2022 at 8:50
  • 1
    With no schematic it is difficult to understand what to expect at soundInPin. That said, inspecting the code, if "val" changes to 1, "heard" changes to 1, "relayON" changes to 1, the LED is turned on then the LED is never turned off because "relayON" is 1 which does not allow "heard" to change ever again. So despite what the hardware does, the code appears to need some more work. Commented Sep 24, 2022 at 13:19
  • Hello, Mr. Myth! Let's start from the basics: 1. What Arduino board? 2. What is it you are trying to do (explain, please), and 3. (As others have said) what does your schematic look like? Commented Sep 24, 2022 at 17:27
  • Have you seen this answer? arduino.stackexchange.com/a/54495/37523 Commented Sep 24, 2022 at 19:18

1 Answer 1

1

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);
}
answered Sep 24, 2022 at 17:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.