Get in contact

posts/page

Using Arrays with EV3-G – Damien Kee

Jan 17, 2014

At a recent conference I ran an advanced EV3 workshop where one activity concentrated specifically on the use of arrays. Arrays are a new edition the MINDSTORMS software that the previous NXT-G software didn’t support (at least not cleanly). Arrays allow us to store multiple bits of data all in the one ‘thing’, which makes it easier to access. If you think of Variables as being a suitcase where you can read and write some information, then Arrays can be thought of as a suitcase that has lots of smaller folders inside. Each folder can store an individual piece of information and you can access that information but selecting suitcase, and then the folder inside.

While you can do the same thing with lots and lots of Variables, Arrays make it far cleaner.

The Project:

Create a game of Memory. The EV3 will call out 4 random colours. You then have to show the Colour Sensor the colours in the right order. Get it right and you score a point, get it wrong and you get a disappointing buzz.

The whole project can be broken down into 2 distinct stages 1. Create and say the sequence of random colours 2. Check if the colours shown to the colour sensor match the sequence.

(click for full size)

Create the Sequence array

To start, we initialise an array and call it ‘colours’. As we are going to use the ‘Append’ function, we don’t need to say how big the array will be to start with.

We then generate a random number between 1-4 (I’m only using 4 colours at the moment). This random number is appended to the Array. As there is nothing yet in our array, this means it will be put in at array index 1. (NB. Arrays in EV3-G are numbered from 1, which is different other programming languages with often begin numbering at 0. There were long and intense debates around the reasoning for this which I won’t get into!).

If the number is a 1, the EV3 will say ‘Black’. A 2 will give ‘Blue’, 3 will give ‘Green’ and 4 gives ‘Yellow’.

Rinse and repeat 4 times (don’t want to make the sequence too long). Each time it repeats, the ‘append’ function adds a new random number to the end of the array.

Checking the colours

Once the EV3 has said its sequence of colours, it moves into checking mode.

Firstly it waits for a colour to be detected. Any colour will do, just so long as it is not ‘no colour’. It then waits for half a second to make sure the colour paddle has settled into place. This is needed to make sure the colour sensor doesn’t pick up a bunch of random colours as the paddle comes down over the top.

We then read the actual colour so we can check it against our sequence.  After that we need to check against our array. We use the loop counter plug which keeps track of how many times we’ve been through the loop. The first time through the loop, the loop counter is ‘1’, which means when we use the ‘Read at Index’ block, it will tell us what number is in the 1st position of our array.

We send this number from the array along with the colour measured from the Colour Sensor block (which will be in the form of a number) through to a ‘Compare Block’ which will give us either a TRUE (colours match) or FALSE (colours don’t match).

A Logic Switch uses this information to play a High note if it is TRUE and a low note if it is FALSE. Repeat 4 times to match the length of the sequence.

Added bonus

This is the basics behind the game.  In the video I added a score using a variable that incremented.  I also used the Medium motor to turn a simple dial to indicate the score going up.  I’ll leave that up to you to figure out 🙂

If you make some cool modification, please let me know!

Get in Contact

  • Brenda Gahan - Head of Learning image

    Brenda Gahan - Head of Learning

    Sunshine Coast Grammar School
    star star star star star

    His wealth of knowledge and expertise allows him to tailor each workshop to the specific skill level and interests of the students (and teachers) and ensures that everyone is engaged and challenged.

  • Digital Technologies Teacher image

    Digital Technologies Teacher

    Victoria Point State High School
    star star star star star

    Students engaged in hands-on learning experiences that developed their problem-solving skills. It was a valuable and highly relevant real world learning opportunity.

  • Tammy Grady image

    Tammy Grady

    Senior Project Officer, QMEA
    star star star star star

    Through the Queensland Minerals and Energy Academy (QMEA), Damien has worked with hundreds of students and Teachers from across the State, all with varying levels of robotic and programming experience. Feedback from Damien’s workshops are always full of praise and appreciation, not only for his professional knowledge, but his adaptable teaching style and ease of delivery.

  • STEM Teacher - Years 7-10 image

    STEM Teacher - Years 7-10

    Tannum Sands State High School
    star star star star star

    Damien's professionalism and communications skills are superb. The very best educational expert visitor we have had come to the college.

  • Primary School Teacher - Year 5 image

    Primary School Teacher - Year 5

    Central Queensland Primary School
    star star star star star

    Damien's delivery style captivated students across every year level, from junior to senior. His depth of knowledge and genuine ability to tailor content to our students' abilities made a real difference, keeping everyone engaged and getting the most out of the experience regardless of their starting point.

  • Head of Curriculum image

    Head of Curriculum

    South East QLD Primary School
    star star star star star

    What stood out most was how thoroughly Damien customised the workshop to our school's specific needs. Rather than a one-size-fits-all session, he took the time to understand what we were after and delivered exactly that.