Sign in to follow this  
Followers 0
Styreman

How would do this using Ladder Logic?

6 posts in this topic

Hey everyone, I'm currently working on what seems like a basic project, but keep going in circles when it comes to implementation using Ladder Logic. I'm using RSLogix 5000 and the project is to create the "Simon Says" game with 4 Push Button inputs and 4 LEDs. For those not familiar, the system is supposed to display an initial random pattern of 4 lights, then the user is supposed to use input the same pattern on the push buttons. If the user inputs the correct pattern, the system does the same pattern again plus an additional light. This continues until the user enters the wrong pattern with the inputs, which the game would then be reset. Essentially it is an alarm system which shows the alarm code. I've been lurking around these forums for awhile getting help on various other projects and thought I'd see how the MrPlc community might handle this.

Share this post


Link to post
Share on other sites
Many people on here can help you AFTER you post what you've tried and what problems you are having.

Share this post


Link to post
Share on other sites
I'm not exactly looking for advice to fix my code, but more of direction to head towards. I know my code isn't going in the right direction, but I seem to be stuck on which logic commands to use instead of what I have. What I've currently created is a code which has two arrays, each size 4. Initially the arrays are cleared and then 4 lights flash 1 at a time, 1 second apart, by use of a timer (specifically timer.TT, timer. DN, and res timer). Each time a light flashes, a 1 is moved into the corresponding array location of array1 (so array1.0, array1.1 etc...) by use of a mov command. Then it enters a different state waiting for inputs and each time a PB is activated, a 1 is moved into the corresponding location of array2. Then I compare each value of the 2 arrays and if each value is equal it energizes all the lights. There is no tracking of which order the buttons are pressed, which i know defeats the purpose of the game. Also, I know there is an easier, more efficient way to complete this using loops instead of this manual method I currently have. I posted here so I could hear what the community would try to do then research how to do the code myself. Mainly I didn't post much info because I'm not sure of the correct question to ask and don't want to limit the responses. Edited by Styreman

Share this post


Link to post
Share on other sites
The array approach might work better if you store the ORDER of lamps lighting in Array1, and the ORDER of button presses in Array2. Let's say the lamps light 2-3-1-4. Logic detects lamp 2 and moves a 2 into Array1, 0. Then logic detects lamp 3 and moves a 3 to Array1, 2. Etcetera. You obviously will need logic to increment the position to fill in the array. Array1 eventually holds the values 2-3-1-4. Then, the same thing happens with button presses. When the arrays match, start over. This is grossly simplified, but do you get the idea?

Share this post


Link to post
Share on other sites
Ah I can't believe I overlooked doing that. I see exactly what you mean. I can keep storing the array values i.e. 2-3-4-2-4-1-etc.... What would be the easiest way to use the logic to step through an array? the code I described took me over 25 lines to do and I know I missed some simplification. A big issue I had was comparing arrays. I found I couldn't compare them as a whole, only by specific location (so array1.0 equal to array2.0). Is there a simpler way to go about that?

Share this post


Link to post
Share on other sites
I would do this with 2 arrays, not 2-dimensional arrays. I would make the arrays large so I could use a "random" staring point. I would use SQO to do the "display" phase, using one of the arrays, and use SQI to do the contestant's "Input" phase, using the other array. I would modify . POS for each stage, so that the light pattern is pseudo-random. The "random" start-point loads into the .POS of the SQO, but if .LEN causes the end-point to go past the end of the array, I would re-choose another "random" start-point. I would use the SQO instruction to drive the lamps in sequence, starting at the "random" start point, until .DN After the "display" phase, I would use an SQI to read in the button presses to a second array, at the same start-point, and for the same .LEN as the SQO At any time during the "read" phase, if the current values at .POS (of the SQI) of the two arrays are not equal, then the player has faulted. You don't need to keep re-testing ones that they got right in the sequence. so you don't need to compare arrays.... that one solitary NEQ at the current position means a fail, and a duck-doh sound. These are just thoughts, and I haven't actually tested it, but I believe SQO and SQI will save you a lot of work. Another advantage of using SQO and SQI is that you can output and input bit patterns directly to the I/O modules... just choose the mask correctly so you don't interfere with other I/O. For a pseudo random number generator, look at the value in the uSeconds register of the wall-clock. You can use MOD to reduce it to the size of your arrays. Edited by daba

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0