📖 Scenario: You are building a simple drum sequencer for a music machine. The sequencer will play drum hits in a repeating pattern over 8 steps. Each step can have one or more drum hits: kick, snare, or hi-hat.The drum hits are represented as boolean values (True or False) for each step.
🎯 Goal: Create a drum pattern using a list of dictionaries. Each dictionary represents a step with keys 'kick', 'snare', and 'hihat'. Then, set a threshold to decide which drum hits to play. Finally, generate a list of steps where at least one drum hit is active and print the active steps.
📋 What You'll Learn
Create a list called pattern with 8 dictionaries for each step
Each dictionary must have keys 'kick', 'snare', and 'hihat' with boolean values
Create a variable called active_threshold set to True
Use a list comprehension to create a list called active_steps containing only steps where at least one drum hit is True
Print the active_steps list
💡 Why This Matters
🌍 Real World
Drum sequencers are used in music production to create repeating drum patterns that form the rhythm of a song.
💼 Career
Understanding how to represent and filter data structures like drum patterns is useful in automation and control systems programming, including PLCs for music or industrial automation.
Progress0 / 4 steps
1
Create the drum pattern data
Create a list called pattern with 8 dictionaries. Each dictionary must have keys 'kick', 'snare', and 'hihat' with these exact boolean values: {'kick': True, 'snare': False, 'hihat': True}, {'kick': False, 'snare': True, 'hihat': False}, {'kick': True, 'snare': True, 'hihat': False}, {'kick': False, 'snare': False, 'hihat': True}, {'kick': True, 'snare': False, 'hihat': False}, {'kick': False, 'snare': True, 'hihat': True}, {'kick': True, 'snare': True, 'hihat': True}, {'kick': False, 'snare': False, 'hihat': False}
PLC Programming
Hint
Use a list with 8 dictionaries. Each dictionary has keys 'kick', 'snare', and 'hihat' with True or False values.
2
Set the active threshold
Create a variable called active_threshold and set it to True. This will be used to check if a drum hit is active.
PLC Programming
Hint
Just write active_threshold = True.
3
Filter active steps using list comprehension
Use a list comprehension to create a list called active_steps. It should include only the steps from pattern where at least one drum hit ('kick', 'snare', or 'hihat') is equal to active_threshold.
PLC Programming
Hint
Use a list comprehension with step for step in pattern and check if any drum hit equals active_threshold.
4
Print the active steps
Print the active_steps list to show which steps have active drum hits.
PLC Programming
Hint
Use print(active_steps) to show the filtered list.
Practice
(1/5)
1. What is the main role of a drum sequencer in PLC programming?
easy
A. To control the speed of a conveyor belt
B. To create repeating timed drum patterns using timers and outputs
C. To measure temperature changes over time
D. To display text messages on a screen
Solution
Step 1: Understand the drum sequencer purpose
A drum sequencer automates drum beats by controlling timing and outputs.
Step 2: Match the purpose to options
Only To create repeating timed drum patterns using timers and outputs describes creating repeating timed drum patterns using timers and outputs.
Final Answer:
To create repeating timed drum patterns using timers and outputs -> Option B
Quick Check:
Drum sequencer = timed drum patterns [OK]
Hint: Drum sequencer = timed drum beats using timers [OK]
Common Mistakes:
Confusing drum sequencer with conveyor control
Thinking it measures temperature
Assuming it displays messages
2. Which of the following is the correct way to start a timer in a PLC drum sequencer program?
easy
A. Timer1(IN, 500ms);
B. Timer1 = Start(500);
C. TON Timer1 (IN:=TRUE, PT:=T#500ms);
D. StartTimer Timer1 500ms
Solution
Step 1: Identify standard PLC timer syntax
In PLC programming, TON timers use the format TON TimerName (IN:=condition, PT:=preset time).
Step 2: Compare options to standard syntax
Only TON Timer1 (IN:=TRUE, PT:=T#500ms); matches the correct TON timer syntax with input and preset time.
Final Answer:
TON Timer1 (IN:=TRUE, PT:=T#500ms); -> Option C
Quick Check:
TON timer syntax = TON Timer1 (IN:=TRUE, PT:=T#500ms); [OK]
Hint: Look for TON with IN:= and PT:= parameters [OK]
Common Mistakes:
Using non-PLC syntax like Start(500)
Missing IN:= or PT:= keywords
Incorrect function call style
3. Given this PLC code snippet for a drum sequencer:
TON Timer1 (IN:=Start, PT:=T#250ms);
IF Timer1.Q THEN Output1 := TRUE; ELSE Output1 := FALSE; END_IF;
What will be the state of Output1 after 300ms if Start is TRUE?
medium
A. Output1 will be TRUE
B. Output1 will toggle ON and OFF rapidly
C. Output1 will be FALSE
D. Output1 will remain unchanged
Solution
Step 1: Understand timer behavior with Start TRUE
Timer1 counts 250ms when Start is TRUE, then Q becomes TRUE after 250ms.
Step 2: Check Output1 assignment after 300ms
Since 300ms > 250ms, Timer1.Q is TRUE, so Output1 is set to TRUE.
Final Answer:
Output1 will be TRUE -> Option A
Quick Check:
Timer done (Q=TRUE) after 250ms means Output1=TRUE [OK]
Hint: Timer Q is TRUE after preset time passes [OK]
Common Mistakes:
Assuming Output1 toggles without reset
Thinking Output1 stays FALSE after timer done
Ignoring timer preset time
4. Identify the error in this drum sequencer PLC code snippet:
TON Timer1 (IN:=Start, PT:=T#500ms);
IF Timer1.Q THEN Output1 := TRUE;
ELSE Output1 := TRUE; END_IF;
medium
A. Start input is not declared
B. Timer1 PT value is invalid
C. Missing semicolon after TON statement
D. Output1 is always TRUE regardless of timer
Solution
Step 1: Analyze IF-ELSE logic for Output1
Both IF and ELSE branches set Output1 to TRUE, so Output1 never becomes FALSE.
Step 2: Check timer and syntax correctness
Timer PT value and semicolons are correct; Start input declaration is not shown but not an error here.
Final Answer:
Output1 is always TRUE regardless of timer -> Option D
Quick Check:
Output1 logic error = always TRUE [OK]
Hint: Check IF and ELSE assignments carefully [OK]
Common Mistakes:
Ignoring ELSE branch value
Assuming timer PT is wrong
Confusing missing declarations as error
5. You want to create a 4-step drum sequencer using PLC timers that activates Output1, Output2, Output3, and Output4 in sequence every 500ms. Which approach best achieves this?
hard
A. Use one timer with 500ms preset and a counter from 1 to 4 to activate outputs in order
B. Use four timers all set to 500ms running simultaneously to activate outputs
C. Use one timer with 2000ms preset and activate all outputs at once
D. Use a single output and toggle it every 500ms without sequencing
Solution
Step 1: Understand sequencing with timers and counters
One timer triggers every 500ms; a counter tracks steps 1 to 4 to activate outputs in sequence.
Step 2: Evaluate options for correct sequencing
Use one timer with 500ms preset and a counter from 1 to 4 to activate outputs in order uses timer plus counter for stepwise output activation; others either run timers simultaneously or activate all outputs together, not sequencing.
Final Answer:
Use one timer with 500ms preset and a counter from 1 to 4 to activate outputs in order -> Option A
Quick Check:
Timer + counter = stepwise output sequence [OK]
Hint: Combine timer with counter for step sequencing [OK]
Common Mistakes:
Using multiple timers without sequencing logic
Activating all outputs simultaneously
Not using a counter to track steps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.