NeoMatrix Series

# Online NeoMatrix Series

This series of videos and exercises are designed to develop learners' Arduino C programming skills.

If you are new to Arduino C, we would recommend that you complete our online Introduction to Arduino C and Programming a Self-Driving Robot series first.

Select a session heading to get started.

### Video Lesson

Tinkercad is a free in browser software that allows users to build and program simulated circuits using a selection of micro-controllers.

You will need an account, these are free to register, or you can sign in using an existing Google, Apple, Microsoft, or Facebook account.

We have made our NeoMatrix simulated circuit available for these activities.

This file also includes a program for set-up - as Tinkercad cannot access the NeoMatrix library, the functions it contains have been pre-written into the code.

### Exercises

We have put together a set of exercises for you to get started with programming a simulated NeoMatrix in Tinkercad.

All learners should start with the bronze level and work their way up as far as they can.

Click on each challenge heading to expand.

The program we provide with the circuit includes the libraries and NeoMatrix set-up.

1. We have already provided a function called randomColor, but it is incomplete. Can you make it so that this function generates a random output value for the brightness of the red, green, and blue outputs?

Tip: The lower and upper variables have already been set in the function's parameters.

• We have already created the local variables for each colour as r, g, and b and set them to zero.

• Call the random() function (already created as part of one of the libraries) for each colour's variable.

• The parameters for the random() function should be set to match the variables created inside the randomColor() parameters.

• For example, to set the brightness of the red colour to a random value;

r = random(lower, upper);

This is then the same instruction for g and b.

uint16_t randomColor(uint8_t lower=0, uint8_t upper=255)
{
uint8_t r,g,b = 0;
r = random(lower, upper);
g = random(lower, upper);
b = random(lower, upper);
return matrix.Color(r, g, b);
}

2. Create a new function that will fill the whole matrix with a random colour.

Tip: Use the return command at the end, not matrix.show(). You should only use the show command inside the void loop().

• You can call a function inside another function's parameters.

• Call the matrix.fillScreen() function (this is included in the libraries already added).

• Call your randomColor() function from inside the brackets of the matrix.fillScreen() command.

For our answer program, we have called this new function 'randomFill'.

void randomFill()
{
matrix.fillScreen(randomColor());
}

3. Move the matrix.fillRect() command from the void loop() to a new function called 'redSquare'. Then, delete the rest of the void loop() program.

The new function:

void redSquare()
{
matrix.fillRect (1, 1, 6, 6, matrix.Color(255, 0, 0));
}

The void loop():

void loop()
{

}

4. In the now empty void loop(), start a new program that does the following;

1. Clears the matrix.
2. Calls your function for filling the screen with a random colour.
3. Shows the results on the matrix.
4. Waits for a second.

• Call the matrix.Clear() function from the supplied libraries to clear the screen.

• The matrix.show() function is called to refresh the matrix and display the current programmed image.

void loop()
{
matrix.clear();
randomFill();
matrix.show();
delay(1000);
}

5. Use the documentation tags (/** and */) to document (make notes on) your functions as a reminder of what they do, what the parameters are, and why. This will make it easier for you to understand when you look back through your code later.

/** This function is a random colour generator for RGB pixels/LEDs.
The minimum brightness is 0 and the maximum is 255.
The function has local variables for the RGB values as well as the lower and upper limits of the brightness.
The lower and upper varaibles are created and set inside the function's parameters.*/

This continues to build on the same program.

1. Create a new function that will draw 3 different sized rectangles inside each other on the matrix using a random colour for each.

Tip: You already have a redSquare() function you can look at for guidance, as well as the video for this session.

2. Document this new function and then call it in you void loop() program. Remember, you will need to clear the matrix before adding this new section of program.

3. Repeat the previous two steps for a function that draws three different sized and randomly coloured circles on your matrix. You may decide on positioning/overlapping. Remember to include documentation again.

This continues to build on the same program.

1. Write a function that will set all the pixels in a column the same colour, and fills the matrix with each column a different colour.

Tip: This will require a nested for-loop.

2. Write a function that will set all the pixels in a row the same colour, and fills the matrix with each row a different colour.

3. Call these functions from your void loop(), with screen clearings and pauses where needed.