The Game of Simon
Learning all about bit manipulation and sequencers
Simon is a memory game introduced in 1978. It has four coloured buttons, each producing a particular tone when it is pressed or activated by the device. A round in the game consists of the device lighting up one or more buttons in a random order, after which the player must reproduce that order by pressing the buttons. As the game progresses, the number of buttons to be pressed increases. If the wrong button is hit the current game is over. Our game will have a high-level score and a current level score.
We will be using AdvancedHMI to communicate Modbus TCP to the Automation Direct Do-More Designer Software Simulator.
Watch on YouTube: The Game of Simon Play (PLC / HMI)
Here is the end result of our program.
Note: The programs are provided free of charge and are an excellent way to learn PLC / HMI programming.
Here is a quick review of the programming series so far. If you are new to the site, we recommend reviewing the other parts in the series first. In part 1 we looked at writing PLC programs to control a traffic light using discrete bits and then using timed sequencing using indirect addressing. Part 2 used indirect addressing for inputs as well as output to control the sequence of pneumatic (air) cylinders in the program. Part 3 and 4 we returned to the traffic light application and expand our program significantly. We looked at the sequence of operation using Input, output and mask tables.
The first thing that we will do is look at the HMI programming. Please refer to the following post for information on setting up and using AdvancedHMI software.
Create a PLC with HMI Training and Learning Environment Free
The following table is the Modbus TCP memory map to the Do-More PLC:
|Coil/Register Numbers||Data Addresses||Type||Do-More PLC||Table Name|
|00001-09999||0000 to 270E||Read-Write||MC1 to MC1023||Discrete Output Coils|
|10001-19999||0000 to 270E||Read-Only||MI1 to MI1023||Discrete Input Contacts|
|30001-39999||0000 to 270E||Read-Only||MIR1 to MIR2047||Analog Input Registers|
|40001-49999||0000 to 270E||Read-Write||MHR1 to MHR2047||Analog Output Holding Registers|
The following map will apply to our game:
(Communication between the HMI and PLC)
40002 – MHR2 – Register – Game Sounds
40001 – MHR1 – Register – Current Game Level
40003 – MHR3 – Register – Highest Game Level
00005 – MC5 – Bit – Start/Reset Game
00001 – MC1 – Bit – Green Button Input
10001 – MI1 – Bit – Green Button Set
00002 – MC1 – Bit – Red Button Input
10002 – MI1 – Bit – Red Button Set
00003 – MC1 – Bit – Yellow Button Input
10003 – MI1 – Bit – Yellow Button Set
00004 – MC1 – Bit – Blue Button Input
10004 – MI1 – Bit – Blue Button Set
The following is the code for the button when hit to play the sound and the DataSubscriber1 to play the sound when the playback from the PLC is required. (Console. Beep (Frequency (Hz), Duration (msec)))
Private Sub PilotLight1_Click(sender As Object, e As EventArgs) Handles PilotLight1.Click ‘ Green Light
Private Sub PilotLight2_Click(sender As Object, e As EventArgs) Handles PilotLight2.Click ‘ Red Light
Private Sub PilotLight3_Click(sender As Object, e As EventArgs) Handles PilotLight3.Click ‘ Yellow Light
Private Sub PilotLight4_Click(sender As Object, e As EventArgs) Handles PilotLight4.Click ‘ Blue Light
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If DataSubscriber1.Value = “1” Then
Console.Beep(415, 420) ‘ Green
ElseIf DataSubscriber1.Value = “2” Then
Console.Beep(310, 420) ‘ Red
ElseIf DataSubscriber1.Value = “4” Then
Console.Beep(252, 420) ‘ Yellow
ElseIf DataSubscriber1.Value = “8” Then
Console.Beep(209, 420) ‘ Blue
ElseIf DataSubscriber1.Value = “10” Then
Console.Beep(120, 1500) ‘ Losing Sound
ElseIf DataSubscriber1.Value = “20” Then
For x = 1 To 8
Console.Beep(600, 90) ‘ Winning Sound
We now have our HMI interface complete and can move onto the PLC programming.
Random sequence generator – MHR4
The first four bits of MHR4 will be used to generate the random sequence for each step of the pattern.
The first scan is to set (1) a bit in MHR0. This will also ensure that the rest of the bits in the word are reset (0).
The second rung will shift the bits left in the output word MHR0. This will happen once per scan of the PLC. When bit 04 turns on then bit 00 will then be turned on again. This way we will always have one of the first four bits turned on in the output word. (00, 01, 02 or 03) MHR0 is logically AND with Hex value 000F and the result is placed in MHR4.
Start the Game
The game will be started n the leading edge of the reset button. (MC5)
All of the registers and pointers are reset to start the game.
1 is moved into the current level – MHR1
100 is moved into the Play Sequence Pointer – V1
100 is moved into the Input Sequence Pointer – V2
100 is moved into the Current Level Pointer – V0
The random sequence MHR5 is moved indirectly into the Current Level Pointer V0 and this pointer is then incremented by 1.
An initialization bit is then set.
The initialization bit starts a timer for 600msec in order to give time for the HMI to respond. Once the time expires the Initialization bit is reset and the game start bit is set. (Y0)
Y0 – Game Started is used to determine if the play is to continue.
Play the Sequence
Set the outputs to play the sequence.
This will also set the sound to play for each of the colours selected. (MHR2)
Reset the sound during the playing of the sequence.
Read the inputs from the HMI. (MC1 – MC4)
Set the bits in V501 so we can compare the word.
If the wrong button is hit, then play a sound and stop the game.
If the correct color is selected, increment to the next random colour in the sequence.
If the correct colour sequence has been entered, then the level has been completed.
Start a delay to ensure that the HMI has finished playing all of the sounds.
When the level is entered correctly and the time delay has expired, we will reset the play and input pointers. The level increases by 1 and another random number are added to the sequence and the current level pointer is incremented by 1.
MHR1 – Level – Increases
V1 – 100
V2 – 100
V0 – New random number gets indirectly addressed and the pointer is incremented by 1
Setting the Highest Level achieved
If the current level is greater than the highest level, the current level is moved to the highest level.
If no key is hit for 45 seconds after the sequence is played a sound will be played and the game start bit will be reset.
If the game start bit is off for more than 500ms, the sound will be reset.
Watch on YouTube: Building a PLC Program that You can be Proud Of – Part 5 – Game of Simon
Part 6 will look at a sequencer controlling seven cylinders that can be taught. The cylinders can be operator programmed from the AdvancedHMI screen.
If you have any questions or need further information please contact me.
If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.
To get this free article, subscribe to my free email newsletter.
Use the information to inform other people how numbering systems work. Sign up now.
The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.