Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
IdeiaLabJonatha Menezes
Published © GPL3+

Random number generator for 7 segment display

Generate random numbers for your display with the push of a button (with a nice animation too).

BeginnerFull instructions provided2 hours718
Random number generator for 7 segment display

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 330 ohm
Resistor 330 ohm
×2
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Texas Instruments CD 4511
×1
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Tinkercad
Autodesk Tinkercad
Used to draw the circuit

Story

Read more

Schematics

The circuit

Code

Code

Arduino
// Namespace that contains the global constants 
namespace Constante
{
  // The pin connected to the push button
  constexpr byte BOTAO = 13;

  // CD 4511 BCD pins; A is the LSb, D is the MSb
  constexpr byte A = 4, B = 5, C = 6, D = 7;
  
  // Minimum and maximum number to be generated
  constexpr byte VALOR_MINIMO = 0, VALOR_MAXIMO = 9;

  // Maximum delay between generations
  constexpr long DELAY_MAXIMO_GERACOES = 500; 

  // Number of times the for loop will execute
  constexpr byte REPETICOES = 20;
}

// Namespace that contains the global variablas 
namespace Variavel
{
  // Array of size 4, will hold the bits to write
  // to the CD 4511
  byte bits[4];
}

void setup() 
{
  // Determines the seed for the random generation
  // based on the analog read of the pin 0
  randomSeed(analogRead(0));

  pinMode(Constante::BOTAO, INPUT_PULLUP);
  pinMode(Constante::A, OUTPUT);
  pinMode(Constante::B, OUTPUT);
  pinMode(Constante::C, OUTPUT);
  pinMode(Constante::D, OUTPUT);
}

void loop() 
{
  // Reads the current state of the push button 
  bool botaoPressionado = digitalRead(Constante::BOTAO);

  // If the button has not been pressed, loop() will not
  // continue
  if(not botaoPressionado)
  { return; }

  // else
  for(byte i = 1; i <= Constante::REPETICOES; ++i)
  {
    // Sets all bits to 0
    limpaBits(Variavel::bits);

    // Generates and stores a random number
    byte numeroGerado = random(Constante::VALOR_MINIMO, Constante::VALOR_MAXIMO + 1);

    // Converts the number and stores in the bits array
    decimalParaBinario(numeroGerado, Variavel::bits);

    // Writes the values of the bits array to the CD 4511
    escritaNosPinos(Variavel::bits);

    // Defines the time to be delayed based on loop number
    long delayAtual = map(i, 0, Constante::REPETICOES, 0, Constante::DELAY_MAXIMO_GERACOES);

    delay(delayAtual);
  }
}

/// Sets all bits to 0
void limpaBits(byte* bits)
{
  bits[0] = 0;
  bits[1] = 0;
  bits[2] = 0;
  bits[3] = 0;
}

/// Takes in the decimal number as first argument and converts
/// to binary, storing the results in the second argument
void decimalParaBinario(byte decimal, byte* vetorBinario)
{
  byte posicao = 0;

  while(decimal > 1)
  {
    vetorBinario[posicao] = decimal % 2;

    decimal /= 2;

    ++posicao;
  }

  vetorBinario[posicao] = decimal;
}

void escritaNosPinos(const byte* bits)
{
  // Position 0 has the least significant bit
  digitalWrite(Constante::A, bits[0]);
  digitalWrite(Constante::B, bits[1]);
  digitalWrite(Constante::C, bits[2]);
  digitalWrite(Constante::D, bits[3]);
}

Github

Make sure to check out our Gihub

Credits

IdeiaLab
1 project • 1 follower
Jonatha Menezes
0 projects • 1 follower
Systems Analysis and Development student; I love gaming and programming, started with hardware in the high school electronic course.

Comments