Due to the simplicity of the project, one would think the intended target audience is the people who are now beginning their life in the Arduino world, but I would rather say this project is a nice little treat for everyone, perhaps even someone who's a bit more advanced can benefit from this project.
What it doesThe idea of this project is really simple: you press the button a single time, and the program takes care of the rest. It rapidly generates a bunch of numbers in a short span of time until it rests on a number, at which point you can interact (pressing the button) with the system again. See for yourself:
Worry not; pressing the button again while the numbers are being generated has no effect, try it yourself!
How it doesNow that you've seen it in action, let's get to the fun part! First you're going to see how the circuit is assembled, and then we'll get to the code.
Though you have to use many wires to be able to use the CD 4511, the assembly is on the easier side of things, one just has to be careful as to where they are connecting these wires. Make sure you double check everything!
Now that that's out of the way, let's analyze how our code works.
General idea of the codeUpon pressing the push button, the program enters a loop that generates a number within the given minimum (inclusive) and max (inclusive). The program then converts this number from the decimal base to binary, storing each bit on a variable. It then writes these bits to the CD 4511, which then displays the number in the seven segment display, and a delay is called. This is done an X amount of times, this amount is determined beforehand with a constant, as well as the minimum and maximum values.
The detailsWithin the sete-segmentos-aleatorios repository, we have a folder with the same name, which contains an Arduino sketch. The sketch contains all of the code for this project. The first thing you will see upon opening the file is these declarations:
namespace Constante
{
constexpr byte BOTAO = 13;
constexpr byte A = 4, B = 5, C = 6, D = 7;
constexpr byte VALOR_MINIMO = 0, VALOR_MAXIMO = 9;
constexpr long DELAY_MAXIMO_GERACOES = 500;
constexpr byte REPETICOES = 20;
}
namespace Variavel
{
byte bits[4];
}
I have created two different namespaces, the first contains the "global" constants of the program, and the second, a single "global" variable, declared by the C++ keyword constexpr, this is how compile-time constants are to be defined in modern C++, rather than using the define preprocessor directive. Though the naming convention would be a giveaway that the variables are constant, the namespaces are just way of organizing your program (in this case, at least). Take it with a grain of salt though, you don't have to follow this style if you don't want to.
The constants are:
- BOTAO: The pin connected to the push button;
- A, B, C and D: These are the bits recieved by the CD 4511, A is the LSb (least significant bit), D is the MSb (most significant bit);
- VALOR_MINIMO: The minimum value to be generated, 0 in this case (0b0000);
- VALOR_MAXIMO: The maximum value to be generated, 9 in this case (0b1001);
- DELAY_MAXIMO_GERACOES: Each number generation calls a delay that grows each iteration from 0 to this max value;
- REPETICOES: The number of times the program will randomize a number and display it.
The variable is:
- bits: A simple array of four slots that contains the bits (only 0s and 1s) to be sent to the CD 4511.
by the way, there are two ways to access a namespace component, first by writing the name of the namespace, the scope resolution operator (::
) and then, the name of the component you are trying to access e.g.: Constante::BOTAO;
, or you could write using namespace Constante;
. In which case, you wouldn't need to specify the namespace to access its components.
The setup is fairly simple, first it sets a seed for the random function using the analogread of the pin 0, then it defines the pin mode for BOTAO as INPUT_PULLUP and the four bits (A, B, C and D) as OUTPUT.
void setup()
{
randomSeed(analogRead(0));
pinMode(Constante::BOTAO, INPUT_PULLUP);
pinMode(Constante::A, OUTPUT);
pinMode(Constante::B, OUTPUT);
pinMode(Constante::C, OUTPUT);
pinMode(Constante::D, OUTPUT);
}
The loop starts by storing the value of the digital read of the button in a boolean variable, if the button was pressed, the value will be true. Immediatly after, it's checked whether the button has been pressed or not, if it wasn't, the loop returns, executing no more instructions. If it was pressed, the program proceeds to a for loop, which will execute REPETICOES amount of times
void loop()
{
bool botaoPressionado = digitalRead(Constante::BOTAO);
if(not botaoPressionado)
{ return; }
for(byte i = 1; i <= Constante::REPETICOES; ++i)
{
limpaBits(Variavel::bits);
byte numeroGerado = random(Constante::VALOR_MINIMO, Constante::VALOR_MAXIMO + 1);
decimalParaBinario(numeroGerado, Variavel::bits);
escritaNosPinos(Variavel::bits);
long delayAtual = map(i, 0, Constante::REPETICOES, 0, Constante::DELAY_MAXIMO_GERACOES);
delay(delayAtual);
}
}
The most important part of the program lies in this for loop. It contains 6 instructions total:
limpaBits()
: This function sets all bits of the bits array to 0;numeroGerado
: In the second instruction we declare and initialize a variable with a random number given byrandom()
with the minimum and maximum values considered. Note that the byte type is an unsigned integer of 8 bits, that means it cannot store values less than 0;decimalParaBinario()
: This one is straightforward, it copies the value from the first parameter (decimal number), converts to binary and stores the bits in the bits array (second parameter);escritaNosPinos()
: This function writes the values of the bits array to the CD 4511, which will show the number on the display.delayAtual
: Contains the mapped value ofthe current loop number (variable I) from 0 to REPETICOES to a delay from 0 to DELAY_MAXIMO_GERACOES;delay()
: Freezes program execution for delayAtual milliseconds.
Check out our repository to see the implementation of the functions.
Check this link for more about the conversion method
CloningIf you are having trouble cloning the repository, I recommend you read this: Cloning a repository - GitHub Docs
Comments