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);
}
-
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.sempaiscuba– sempaiscuba2022年09月24日 08:34:54 +00:00Commented Sep 24, 2022 at 8:34
-
Is there something on the sound sensor board to adjust the threshold? Like potentiometer ?chrisl– chrisl2022年09月24日 08:50:37 +00:00Commented Sep 24, 2022 at 8:50
-
1With 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.st2000– st20002022年09月24日 13:19:35 +00:00Commented 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?Austin– Austin2022年09月24日 17:27:00 +00:00Commented Sep 24, 2022 at 17:27
-
Have you seen this answer? arduino.stackexchange.com/a/54495/37523VE7JRO– VE7JRO2022年09月24日 19:18:37 +00:00Commented Sep 24, 2022 at 19:18
1 Answer 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);
}