Rich's Wordpress

又一个WordPress站点

Buzzer Follow-up

I thought about it for a few days and decided that although I could control the sound of the buzzer, it wasn’t good enough. I remembered that when the alarm is set off in the general movie, there is not only the alarm sounds but also the red light flashes at the same time, so as to tell people to run away. So I went ahead and did some research and made the idea a reality.

Alarm with sound and light

Aim: Let the Buzzer make sounds while letting the red LED flash

Step:

  1. Import needed modules
  2. Initialize all components that need to be used
  3. Define a function to toggle the state of the buzzer, so that when the key is pressed it can be turned on and off.
  4. Set the interrupt for the button
  5. Determine if the buzzer is on, if it is make the LED flash, or else turn off the LED.

Sleep method

from gpiozero import Buzzer
from gpiozero import LED
from gpiozero import Button
from time import sleep

red = LED(4)
bz = Buzzer(16, active_high = False)
key = Button(12)

def make_sound():
	bz.toggle()
	
key.when_pressed = make_sound

while True:
	while bz.is_active:
		red.toggle()
		sleep(0.5)
	red.off()

How does the program work?

When the program is executed, after the three components (Red LED, Buzzer & Key) are initialized, it goes into the while loop to check whether the buzzer is on. When I press the key, it causes an interrupt, and it alters the sequence in which the processor executes instructions. The computer will first treat the interrupt so in line 12, the function “make_sound” is called by it, in this function, the state of the buzzer is toggled from off to on. Now, the computer finishes treating the interrupt, it will come back to the main program, and keep checking if the buzzer is on. Since the buzzer is turned on by the interrupt, the program in the while loop starts to be executed, it makes the red LED turn on and off every 0.5 seconds.


But that’s not good enough, the program needs to wait every time it executes the sleep() function. This wastes a considerable amount of time, and during this waiting time, the computer could actually go and take care of something else, and wait until the buzzer and LED need to be taken care of before coming back.

Timer method (Threading)

The concept of thread needs to be used here

Definition of thread: a thread is a sequence of instructions that can be executed independently of the remaining program

If there is a large program, there are many small tasks that the program performs, each of these small tasks can be a thread as long as the execution is independent of the execution of the other task. But why is thread important and needed in the program? Nowadays, almost every device uses multi-core processes, which means that there are multiple cores in the system, the point of threading is to make the maximum utilization of these cores. Let’s say one program will only run on a single thread, which means that it would only utilize that one CPU or one core, if the program is divided into different threads, and each of the threads uses a different CPU or core, the program would execute much faster and it would be way more efficient.

The threads are different units of the process that do jobs independently when scheduled. If they need to wait for a slow external operation to finish, they can enable the scheduler to spend time executing another thread, so that the time is not wasted.

Timer(): This function is used to create a new thread and specify the time after which it should start. Once it starts, it should call the specified function.

from gpiozero import Buzzer
from gpiozero import LED
from gpiozero import Button
import threading

red = LED(4)
bz = Buzzer(16, active_high = False)
key = Button(12)

def shine():
	if bz.is_active:
		red.toggle()
		threading.Timer(0.3,shine).start()
		
def make_sound():
	bz.toggle()
	shine()
		
key.when_pressed = make_sound

while True:
	pass

How does this code work?

Similarly, modules needed are imported and the components needed are initialized first. Then, two functions are defined, “shine()” and “make_sound”, the first function is to make the red LED switch its status continuously to achieve the blinking effect, while the second function serves to invert the status of the buzzer and to call the first function “shine()”. The part that is different from the previous code is the way I control the flashing of the light, this time I use the concept of threading in Python to toggle the status of the LED, which is achieved by scheduling the task every 0.3 seconds. Although between each task, the time interval is only 0.3 seconds, that 0.3 seconds is enough for the extremely strong CPU to do many other things such as executing lines of code in the main program (the main program in this code is just “pass”, so it basically is still not doing anything, but if there is something in the main code, it can be executed during the interval of time, so no time will be wasted).

By the way, this program does the same thing as the previous program, just using a better and more efficient approach.

Buzzer Follow-up
Scroll to top