Author Topic: Tic Tac Toe game for PLC  (Read 6884 times)

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Tic Tac Toe game for PLC
« on: December 08, 2017, 11:35:22 AM »
I got bored.  So I wrote PLC program to play Tic Tac Toe (Noughts and Crosses).  

The game will run on any of the current TRI PLCs.  The program does not use any physical INPUTs or OUTPUTs. You could run it with a Nano-10.  The game can be played using the on-line monitor or using a touch screen HMI.  I'll put up the code for the Weintek HMI that I am using in a future post.

The game is written in 99.9% ladder logic.  It is a good demo of the use of PLC sequencers to implement finite element state machines.

I'll put up some documentation that helps explain my use of state machine design and the PLC sequencer mechanism.

I've attached the PC6 program to this posting.

Gary D*ckinson

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:HMI code for Tic Tac Toe
« Reply #1 on: December 08, 2017, 11:42:34 AM »
I've attached a zip file for the Wientek HMI code for the Tic Tac Toe Game.

This code is written for one of the 7" Wientek HMI's.  You can rework it to fit on the smaller 4.3" HMI.

The HMI code communicates with the PLC via an RS-232 connection and uses Modbus RTU protocol.  If you want to use this code, be certain that the PLC's RS-232 port is set up for 38.4K BAUD, 8 bit data, no parity and 1 stop bit.  This is the default (as shipped) data rate on the TRI PLCs.

If you want to run this HMI code and connect it to the NANO-10, you will need to modify the HMI program to use the RS-485 connection to communicate to the PLC.  Modbus RTU and 38.4K, 8N1 setup for both serial ends of the cable.

Please note that the PC6 program does not initialize the PLC's serial port.  You may want to add a custom function that runs on the first scan to initialize the serial port.  I was just being a bit lazy.

Gary D*ckinson
« Last Edit: December 08, 2017, 09:45:23 PM by garysdickinson »

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe Documentation
« Reply #2 on: December 11, 2017, 03:16:56 PM »
I broke up the documentation over the next 5 posts to attempt to stay under the 200K file size limitiation

Best regards

Gary D*ckinson

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #3 on: December 11, 2017, 03:17:18 PM »
I broke up the documentation over the next several posts to attempt to stay under the 200K file size limitiation

Best regards

Gary D*ckinson

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #4 on: December 11, 2017, 03:17:31 PM »
I broke up the documentation over the next 5 posts to attempt to stay under the 200K file size limitiation

Best regards

Gary D*ckinson

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #5 on: December 11, 2017, 03:17:44 PM »
I broke up the documentation over the next 5 posts to attempt to stay under the 200K file size limitiation

Best regards

Gary D*ckinson

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #6 on: December 11, 2017, 03:18:32 PM »
I broke up the documentation over the next 5 posts to attempt to stay under the 200K file size limitiation

Best regards

Gary D*ckinson

Lorne Van Dusen

  • Jr. Member
  • Posts: 87
  • I'm a old guy
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #7 on: December 14, 2017, 08:24:21 AM »
Gary that is the most impressive program I have ever seen in all my many years of programming PLC.s
I have viewed some of you documentation and I must admit it really shows how experienced and professional you are keep up your excellent work as I am a firm believer in always keep the mind occupied with new things so your mind does not start to deteriorate to the point you no longer have interest in anything.  
I never imagined anyone ever making a actual game using a PLC.
Many years ago when computers were fairly new to most of the public I played around and made some simple games using basic and one of them was a simple game that was designed for kids. It was similar to a hangman game except that for each question the kids got wrong a little stick man would walk towards a cliff and if you got too many questions wrong the little stick man would fall off the cliff. I was amazed though that I had given t the school that my kids went to at that time the teachers all loved it and they used it for many years after.
Now you have given me something to do over the Christmas holidays as like you if I have too much time off I get board

Lorne Van Dusen

garysdickinson

  • Expert Member
  • Posts: 482
  • Old PLC Coder
    • View Profile
Re:Tic Tac Toe game for PLC
« Reply #8 on: December 14, 2017, 10:43:37 AM »
Lorne,

I am glad that you enjoyed my post. My PLC programming is “recreational”.

I try to share ideas on how to get the most out of the TRI PLCs. Three of the most powerful features of these PLCs, in my opion, is the specialized counters (Sequencers), the TBASIC programming language and the raw horse power of the 32-bit CPU core that is buried in the PLC.  

I had started to write a tutorial on using the Sequencers as the basis of finite element state machines over 10 years ago, but never got very far.

The Tic Tac Toe gave was, initially, written using only a single state machine.  I wrote all of the game strategy (checking for win, block, special moves...) using custom functions.  The use of CFs is much simpler to write and debug.  Once I got the game strategy working I replaced the CFs with ladder logic.

I thought that Tic Tac Toe was a better vehicle to give people a hint of the power of using state machines in PLC code.  All of my PLC code is based on this sort of state machine approach.  My code has logged hundreds of thousands of hours for my clients without problems.  And, better yet, years later I can add fetatures to old PLC programs with ease.

As, always, if you have questions about how to get the PLC to do your bidding, please feel free to ask.

Best regards,

Gary D*ckinson
« Last Edit: December 14, 2017, 10:47:54 AM by garysdickinson »