Hello everyone,
I have a work to do,
I have to use Piezo and i have to program that the led if we beat one time shine and when you hit two times shutdown
but i don't know how to program that.
Please help
Thanks you very much
jazy
Hello everyone,
I have a work to do,
I have to use Piezo and i have to program that the led if we beat one time shine and when you hit two times shutdown
but i don't know how to program that.
Please help
Thanks you very much
jazy
96,000 hits on Google for 'Arduino knock sensor', including some on this site which include code, explanations...
It should probably be an interrupt routine which starts at one knock.
If you write it as an interrupt, the routine must probably first disable itself being an interrupt routine to avoid the second knock from starting a new interrupt. And before ending, it must enable the interrupt again.
The downside is that there's a 400 ms waiting when nothing happens. Next step would be to get rid of that. If your program is supposed to do other things, too, you can't wait 400 ms just to check for a double knock.
You NEVER wait in an interrupt routine. If there's 400ms to spare you exit and let the second knock be a second interrupt. The interrupt shouldn't be allowed to take even a tenth of long enough for the possibility of a double knock for even the fastest knocker. ISR's should be in the microseconds.
Really, knocks are slow enough you can most likely just poll for them in loop if you keep things tight and non-blocking.
You will also have to define "beat two times".
Does this mean it has to go off on the second beat regardless of when it is done, or that it has to go off when you make two beats in quick succession?
The first is easy.
void loop() {
if (beatDetected()) {
LEDstate = !LEDstate;
digitalWrite(LEDpin, LEDstate);
}
}
The beatDetected() function you can write when you know how what signal your sensor gives. It's piezo so no need to debounce, right?
The second requires you to keep track of time - look for millis() timing & the "blink without delay" example code.
By the way, did you ask your teacher for guidance on this? That should be your first step.