Hello, I need help with my code, I have a speaker and i want it to activate with 2 tones of 800 and 500 Hz during 5 seconds. Then stop another 5 sec and start again, 5 times until final stop. I dont know how to do that, so i hope you can help me. This is what im talking about:
void activateMosfet(){
tone(10, 800);
delay(20);
tone(10, 500);
delay(20);
delay(MOSFET_TIME); // wait for 5 second
}
The problem is that the code would stop in delay(MOSFET_TIME);
I'm not clear what the problem is. The code you showed only does that output once, so it would make sense to call it five times in a FOR loop to achieve the overall behaviour. I noticed that you mention 400 Hz but have 500 in the code. Apart from that I don't see the problem.
Yes, 500 Hz sorry. This code is part of a bigger code (an airsoft bomb :P) so when the game ends and the bomb explodes, and i want to make the speaker sound with 2 tones at same time during 5 seconds, wait 5 sec, and do it again 5 times, and then stop (ON, OFF, ON, OFF, ON, OFF, ON, OFF, ON, OFF)
The problem with my code is that it sounds a little moment, wait 5 sec, and im not sure if it repeats.
Anyway, Im a beginner in programation, i dont know how to make a loop with FOR.
Well, it won't repeat unless you call that function again.
It's time for you to learn how to use a FOR loop.
Do you really want those two tones to be put out simultaneously? You can't use the tone() function to output two tones simultaneously. The current code will put out the two tones in sequence. If you want the tone to stop, you will need to call noTone() after the delay ends.
void activateMosfet(){
int x = 1;
for(int i=0;i<=MOSFET_TIME;i=i+x){ //MOSFET_TIME is 5000, declared in another part of the code
tone(mosfet, 800);
delay(20);
tone(mosfet, 500);
delay(20);
if(i>MOSFET_TIME){
noTone(10);
delay(MOSFET_TIME);
i=0;
}
}
if(keypad.waitForKey() == key){
cls();
menuPrincipal();
}
}
But... it doesnt do what i want. I want it to stop by itself after repeating two or three times or after some time, or if i press any key in the keyboard, and go back to the main menu.
Alferciu:
I want it to stop by itself after repeating two or three times or after some time, or if i press any key in the keyboard, and go back to the main menu.
I'm not sure what "it" is but I will guess you are talking about the tone output.
If you want a loop to continue until something has been done a number of times, or the duration exceeds a threshold, or an input event is detected, then it would make sense to implement that using a for loop, but including all the other termination conditions.
unsigned long startTime = millis();
for(int count = 0; (count < 3) and (millis() - startTime < timeout) and !keypad.keyPressed(); count++)
{
tone(mosfet, 800);
delay(20);
tone(mosfet, 500);
delay(20);
}
I'm not familiar with the keypad library you're using so I don't know what non-blocking method you'd need to call to test whether a key had been pressed - replace keyPressed() with whatever the right method is.
int x = 1;
for(int i=0;i<=MOSFET_TIME;i=i+x){ //MOSFET_TIME is 5000, declared in another part of the code
tone(mosfet, 800);
delay(20);
tone(mosfet, 500);
delay(20);
if(i>MOSFET_TIME){
noTone(10);
delay(MOSFET_TIME);
i=0;
}
}