STATE MACHINE: In order to have a more simplistic and less repetitive program I used a stat machine. To use this method you must first design your program as a "state diagram". This is simply like a flow chart that moves from event to event...or state to state. Each "state"is numbered in the circles. Blocks of code "to do" are in the boxes. State machines try to return to certain states instead of having everything in one long line of code. As an example my state diagram for this robot is below. You can see that I have a loop which returns to state 2 and one which relates to state 10.
The code for a state machine simply has two variables: present state and next state. As events happen, you load "present state" from "next state"....and then come around the loop again.
In each state you have a "switch" command tab, and in that tab you run the code for that state. Then all that is left is writing code for all the states you have (in the LEGO programming language you can write "my blocks" instead. "My blocks" contain a lot of code in one block so that you can copy one block instead of 20 if you need to repeat a section of code). You can avoid loops inside loops all over the place and all the confusion that brings. State machine EV3 code:
When you use a state machine making changes to your code flow is super-easy and it avoids your code becoming a bowl-of-spaghetti.
PIXYCAM COLOR CODES: Since the Briscola cards are quite colourful with lots of difficult-to-interpret shapes, I had to make the cards black & white and then add "colour codes"to each card so that the PIXYCAM could read them. Every card had its own unique 3 colour code. Each colour represents a different number to the program: Yellow, Blue, Green would be "123" and Yellow, Purple, Yellow is "141." I had to use 5 colours in total to be able to have 40 unique colour codes. You can see these in the video on the "Robotics" page.
Since the PIXY-LEGO block outputs a 3 digit number for the 3 colours on each card, an interesting code block is the one that converts the 3 digit code into card information. It uses a 40 tab switch command...one tab for each card.
I also gave the cards another number to indicate their worth to the robot e.g. an ace is 5, a face card is 3 and other cards are 1. I used this number in a greater-than/less-than block to let the robot decide whether it should play a higher card (a five) to win the hand, or forfeit by playing its lowest value card (a one). I added some complexity by coding in an additional block that added to the number of any card in the "briscola" or "trump" suit, which changes every game, since they are more valuable.
Sometimes the PIXYCAM does not read the card correctly, so I re-read the card until I get a 3 digit number eg: >99. I used a loop and read the card up to 10 times... using the "3 digit" test to decide when to exit. Below I show the code with True and False tabs both shown:
Using colour codes with PIXYCAM is very handy and opens up lots of possibilities for applying the camera to real projects. One difficulty I had was avoiding glare on the codes which confused the camera. Managing the lighting is important ...so plan for that in your design, whether it be through shields or installing lighting on your robot.
I hope you enjoy these ideas. All the best, Alanna Manfredini