CE351 lec16 A music game box Task 2.3 and PWM introduction
Overview
Yiyan Li walks through the CE351 music-copy game in Task 2.3, showing how timer interrupts, song-state flags, and active-low button presses coordinate playback and compare the timing of a player's presses. The lecture then introduces pulse-width modulation (PWM), explaining duty cycle, why digital PWM can replace a DAC for many loads, and the limits of Arduino's analogWrite() when output frequency must be controlled.
Key takeaways
- The music game's playback design separates note generation from song sequencing: timer interrupts run independently, while flags determine whether a note or song should play.
- Task 2.3 measures copied rhythm by saving a first TCNT reading, then subtracting consecutive timer readings and checking each interval against a tolerance.
- The button is active-low, so pressed-state handling waits while the input is low to sustain a note, then waits while it is high before accepting the next press.
- With the demonstrated timer setup, an overflow after roughly 0.5 seconds can cause a failure before the player completes the sequence, making timeout behavior part of the game's difficulty.
- PWM adjusts average power through duty cycle, allowing a digital microcontroller pin to control loads such as motors and LEDs without a separate DAC.
- Arduino analogWrite() provides 8-bit duty-cycle control but limited frequency selection, so it is unsuitable when a device requires a specific PWM carrier frequency.
Chapters
- A note-playing interrupt updates the timer counter and toggles an output pin to generate sound.
- Song playback uses a state sequence for idle, note, and rest, with the song routine setting timer counts for note and rest durations.
- Separate play-song and play-note flags let the interrupts continue running while controlling whether notes are emitted.
- Receiving the serial character R enters the matching case in the provided Task 2.3 template.
- The handler selects the copy-song number and sets the play-song flag so the existing timer interrupt begins playback.
- After the song finishes, the interrupt clears the play-song flag; the main code can wait on that flag before disabling Timer 3's overflow interrupt.
- Timer 3's overflow interrupt enable is controlled by the TOIE3 bit in TIMSK3.
- Setting TOIE3 with a bitwise OR enables the interrupt; clearing it with a complemented bit mask and AND disables it.
- Once the copy song ends, disabling the interrupt prevents unwanted playback while the game waits for button input.
- The player waits for the active-low push button: an unpressed button reads high, while pressing it connects the signal to ground.
- The note index starts at zero, so the first press records the current TCNT timer count as a reference rather than comparing it with a previous press.
- Recording that first count makes the player's later timing measurable even if they wait an arbitrary amount before beginning.
- On a press, the main loop sets the play-note flag true and waits in a loop while the active-low button remains low.
- Releasing the button clears the play-note flag, stopping the sound and letting the player hear each reproduced note.
- After release, the code waits while the button reads high; the next press advances the note index and captures another timer count.
- For each press after the first, the program subtracts the previous TCNT value from the current value to obtain an elapsed-count interval.
- The interval is compared with the target timing using a tolerance, allowing for imperfect human button timing.
- Li notes that a timer overflow can trigger failure if the player waits too long; the demonstrated setup overflows at roughly half a second, which can make the game unforgiving.
- After the required presses, the success flag selects the success song; otherwise, the code selects the failure song.
- Both outcomes use the existing song-playback interrupt structure, changing the song index and setting the play-song flag.
- Li advises following the supplied Task 2.3 template and debugging the code directly; the assignment is due Wednesday.
- PWM changes the fraction of each period spent on, called duty cycle; a larger duty cycle can make a motor spin faster or an LED appear brighter.
- Digital PWM can approximate variable output power without adding a digital-to-analog converter, reducing circuit complexity and component footprint.
- Arduino analogWrite(pin, value) accepts an 8-bit value from 0 to 255: 0 gives an off output and 255 gives a continuously high output.
- The standard analogWrite() interface sets duty cycle but offers limited control over frequency; common Arduino PWM frequencies are about 490 Hz or 980 Hz, so applications needing a specific carrier frequency may require timer-level configuration.
Transcript excerpt
0:00 uh song. Okay. So, playing the song. So, this this code can be found um on the website but partially. Okay. But now I'm giving you guys the solution here. So, playing the note coming coming to the first the first interrupt. Okay. It changes the TCNT count and then flip flip the flip the the pin. Okay. Playing the song is a inter is a independent interrupt routine follows the three states different states. First the first one is idle. If playing song is true start playing it and go to the next state put a plain note play note to be true and then directly give a number
0:49 to TC&T. So it's able to um use correct uh frequency for duration. Okay, for the duration and this one will keep running. These are independent. This is independent from this guy. So this is going to keep running playing the notes. Okay, as long as the play note flag is true. Okay, and when it gets into the rest, you reset, you change the number in the tent counter to the rest duration, right? And then um increment your your uh note index according. So this is a f this is a routine to run playing node and playing song and this will be used and this will be used for task 2.3 which is the very last one in that tutorial. I'm going to
1:37 show you what that is. Okay. Um it's over here. Okay. I'll play this video again to remind you about the final result. with the final result of game. First has a sharp stuff and I can push this
2:33 battery out. did it. Let's pay attention to this one. Okay. >> So, I I'm sending I'm sending a character through the US through the
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Yiyan Li.