Snake library for Arduino and 7-segments display
This library allows to run snake on 7-segments display driven by LedControl. It was made as ‘screen saver’ for idle mode in our product.
Library available in the library manager.
Also, you can find the latest version on github project release page.
#include <LedControl.h>
#include <Snake.h>
// connect display with LedControl
LedControl ledControl = LedControl(12, 11, 10, 1);
// create Snake instance
// parameters: ledControl (required), digitsCount = 4, delay = 400 ms
Snake snake(ledControl);
void setup() {
// start snake
snake.start();
}
void loop() {
// move snake
snake.tick();
}
Make sure that you have LedControl and it was added to your project.
#include <Snake.h>
Snake snake(ledControl);
First parameter is required and should be the LedControl instance.
Also there are two optional parameters that you can pass to Snake constructor:
digitsCount
— numbers of digits on your display, 4 by defaultdelay
— the minimum delay in milliseconds between snake movement, 400 ms by defaultLike this:
Snake snake(ledControl);
Snake snake(ledControl, 2);
Snake snake(ledControl, 8, 100);
Snake library does not use real delay()
call, so pause between movements are not blocking.
snake.start();
This command initialize position of snake’s head (put it to te display in random place) and make some other preparation before run.
You can choose two ways to move the snake. The first is to call snake.tick()
on each iteration of main loop
:
void loop() {
snake.tick();
}
In this case, the snake will be automatically moved when delay will over.
The second way is to move snake manually via calling the snake.move()
manually, when you need it.
void start(bool resetHead = true);
Prepares snake to move.
Parameter resetHead
defines do library need to re-generate head position or use existing.
Using it as false
allows to implement continue after pause
behavior instead of restart.
void stop(bool hide = true);
Stops snake on display.
If parameter hide
will be true (default behavior), segments of the snake will be switched off.
Otherwise, it just stops movements (exclude manually) and allows to implement pause
.
void move();
Manually moves snake on display.
Do not use snake.enabled
flag.
void tick();
Allows to check time and move snake in main sketch’s loop
.
Type bool
, represent state of snake.
if false
, snake wont move on tick calls.
Managed by snake.start
and snake.stop
methods.
Make display similar to yours. And then customize the snake:
And here is your constructor:
Snake snake(ledControl);
Please, use browser that supports JS modules
If you find an error or have any other questions, please open issue on the github project page. Or you can contact me using e-mail: dev@Liksu.com.
© 2019 Petro Borshchahivskyi