THIS PAGE IS UNDERGOING CHANGES - PLEASE RETURN SHORTLY
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.
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.
For more information regarding how to use Tinkercad, please visit our Online Electronics Series.
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.
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.
Tip: Use the return
command at the end, not matrix.show()
. You should only use the show command inside the void loop()
.
r
, g
, and b
and set them to zero.
random()
function (already created as part of one of the libraries) for each colour's variable.
random()
function should be set to match the variables created inside the randomColor()
parameters.
r = random(lower, upper);
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);
}
matrix.fillScreen()
function (this is included in the libraries already added).
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());
}
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()
{
}
void loop()
, start a new program that does the following;
matrix.Clear()
function from the supplied libraries to clear the screen.
matrix.show()
function is called to refresh the matrix and display the current programmed image.
void loop()
{
matrix.clear();
randomFill();
matrix.show();
delay(1000);
}
/**
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.
Tip: You already have a redSquare()
function you can look at for guidance, as well as the video for this session.
redSquare()
function. This uses one line of code to produce a square - a rectangle where all the sides are the same length.
matrix.fillRect(Start Column, Start Row, Column Span, Row Span, Colour);
Remember, as this is a programming language, the first row and column are numbered as 0.
The column span is how many columns the rectangle is drawn over, whilst the row span is how many rows.
randomColor()
function replaces the matrix.Color() for this exercise.
fillRect()
, the rectangles will be added to the neomatrix in the order you write them. This means you'll need to write the functions in order of largest first. Alternatively, you could use the drawRect()
function which just produces an outline, removing this issue.
In our solution program, we have named this new function tripleRect()
. Your rectangles may have different start points and/or dimensions.
void tripleRect()
{
matrix.fillRect (1, 1, 10, 6, randomColor());
matrix.fillRect (2, 2, 8, 4, randomColor());
matrix.fillRect (3, 3, 6, 2, randomColor());
}
void loop()
program. Remember, you will need to clear the matrix before adding this new section of program.
Tip: Add to the current program, as the next image, rather than deleting the previous exercise.
delay()
or else the image will not hold on the neomatrix long enough to see.
void tripleRect()
{
//uses structure (Start Column, Start Row, Column Span, Row Span, Colour).
matrix.fillRect (1, 1, 10, 6, randomColor());
matrix.fillRect (2, 2, 8, 4, randomColor());
matrix.fillRect (3, 3, 6, 2, randomColor());
}
void loop()
{
matrix.clear();
randomFill();
matrix.show();
delay(1000);
matrix.clear();
tripleRect();
matrix.show();
delay(1000);
}
matrix.fillCircle()
and/or matrix.drawCircle()
functions.
fillCircle(centre column, centre row, shape radius, colour):
This means that we are positioning the centre of the circle, not the top left corner.
void loop()
.
Our function names, shape positionings and size may vary to your own.
void tripleCircle()
{
//uses structure (Centre Column, Centre Row, Radius, Colour)
matrix.fillCircle(4, 3, 3, randomColor());
matrix.fillCircle(9, 3, 3, randomColor());
matrix.drawCircle(6, 5, 2, randomColor());
}
void loop()
{
matrix.clear();
randomFill();
matrix.show();
delay(1000);
matrix.clear();
tripleRect();
matrix.show();
delay(1000);
matrix.clear();
tripleCircle();
matrix.show();
delay(1000);
}
This continues to build on the same program.
Tip: This will require a nested for-loop.
void loop()
, with screen clearings and pauses where needed.
Here are some suggestions of other things you could attempt at this stage.