The led and the buzzer are activated by means of a bluetooth command and they have to turn off with the signal from the microphone sensor, I am doing something wrong, because the signal from the microphone sensor is taken only for an imperceptible instant and they turn on the led and the buzzer, I had to put a DELAY of 2000 so that they are off for a perceptible time, but after that time they turn on again without sending any command via bluetooth, what I want is that if the microphone sensor sends a high pulse they turn off and until the command is sent via bluetooth, they do not turn on again, I can't find a way, can someone give me a solution? thanks.
String cadena;
int ledPin = 10; // led
int buzPin = 9; // buzzer
int micPin = 3; // microfono
int padstatus = 0;
int micstatus = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(micPin, INPUT);
digitalWrite(buzPin, HIGH);
delay(500);
digitalWrite(buzPin, LOW);
delay(500);
digitalWrite(buzPin, HIGH);
delay(500);
digitalWrite(buzPin, LOW);
delay(500);
digitalWrite(buzPin, HIGH);
delay(1000);
digitalWrite(buzPin, LOW);
padstatus = digitalRead(ledPin);
micstatus = digitalRead(micPin);
}
void loop(){
if (Serial.available()) {
cadena = String("");
while (Serial.available()) {
cadena = cadena + char(Serial.read());
delay(1);
}
}
if (cadena == "Pad1") {
digitalWrite(ledPin, HIGH);
digitalWrite(buzPin, HIGH);
}
if (digitalRead(micPin) == HIGH) {
digitalWrite(ledPin, LOW);
digitalWrite(buzPin, LOW);
delay(2000);
}
}
first of all you should post as a codesection
here is a link with tips how to speed up getting good answers
I guess your codeline
cadena = String("");
shall clear your variable cadena
it only does if there is something in the serial buffer
This means your program does exactly what you have programmed. (like in 99,5% of all cases)
if (Serial.available())
only becomes true if something is received.
as long as nothing is received variable cadena
holds its content because it gets not cleared and if this content is "Pad1"
your if-condition
if (cadena == "Pad1") {
digitalWrite(ledPin, HIGH);
digitalWrite(buzPin, HIGH);
}
if (digitalRead(micPin) == HIGH) {
digitalWrite(ledPin, LOW);
digitalWrite(buzPin, LOW);
is true and the code inside the if-conditions block gets executed
use this code-version to see it in the serial output
String cadena;
int ledPin = 10; // led
int buzPin = 9; // buzzer
int micPin = 3; // microfono
int padstatus = 0;
int micstatus = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(micPin, INPUT);
digitalWrite(buzPin, HIGH);
delay(500);
digitalWrite(buzPin, LOW);
delay(500);
digitalWrite(buzPin, HIGH);
delay(500);
digitalWrite(buzPin, LOW);
delay(500);
digitalWrite(buzPin, HIGH);
delay(1000);
digitalWrite(buzPin, LOW);
padstatus = digitalRead(ledPin);
micstatus = digitalRead(micPin);
}
int myCount = 0;
void loop(){
myCount++;
if (Serial.available()) {
cadena = String("");
while (Serial.available()) {
cadena = cadena + char(Serial.read());
delay(1);
}
}
if (cadena == "Pad1") {
digitalWrite(ledPin, HIGH);
digitalWrite(buzPin, HIGH);
}
if (digitalRead(micPin) == HIGH) {
digitalWrite(ledPin, LOW);
digitalWrite(buzPin, LOW);
delay(2000);
}
Serial.print("loop no ");
Serial.print(myCount);
Serial.print(" cadenas content is #");
Serial.print(cadena);
Serial.println("#");
delay(1000);
}
best regards Stefan
Thank you very much for your prompt reply StefanL38, I apologize for the error as I posted the code, I am new to the forum, thanks for the advice, I posted it correctly already.
Thanks for showing me that the serial buffer keeps its content if nothing is done to erase it, I calculate that this is why the instruction "digitalWrite LOW" to keep the LED and BUZZER pin off is not maintained and when returning to the beginning of the "void loop "takes the value of the variable stored in the serial buffer again and puts the pins high again, so with the" delay "I keep them off for that time, it is a great help for me, now I begin to realize that I need empty the buffer every time the cycle begins, I tried the code that he sent me and I could see how the value of "pad1" was maintained, it was very graphic, when I have some progress I will publish it here.
It is a great help for me, I have been in electronics since I was 14-15 years old and a pending account was to enter the world of microcontrollers and little by little I am achieving it, thank you very much