In this post, you will learn how to generate random numbers on an Arduino and display them on a 7-segment display. This type of project is ideal for understanding how to interact with displays and manage their configuration to show different digits.
The program uses the random() function from Arduino to generate random numbers within a defined range (for example, between 0 and 99) and then translates them into digital signals to control the segments of the display.
Materials:
Connections:
How does it work?
The operation of this project is as follows: When we press the button once, we enter an infinite loop where random numbers will appear on the displays. We will not be able to visualize these numbers since the pause between each number is 10ms (this pause can be modified). When we press the button a second time, we exit the loop and will be able to visualize a number on the displays. Below is the documented code.
Documented code:
/*Code written by: Esteban Carrillo for EDEPTEC.Website: https://www.edeptec.comFacebook: @edeptecYouTube: https://youtube.com/c/EDEPTEC*/// We define the pins of the displays//int pins[] = {0, 1, 2, 3, 4, 5, 6};//{g, f, e, d, c, b, a};#define decenaTr 10 // Pin to activate the tens display#define unidadTr 8 // Pin to activate the unit displaybool previousState = 0; // Variable to store the previous state of the buttonint unit; // Variable to store the unit of the random numberint ten; // Variable to store the ten of the random numbershort pause = 5;// Variable to store the duration of the pause between display activationsbool pulse = 0; // Variable to control between start and stop//Numbers that will be displayed on the 7-segment display in binarybyte numbersAnode[10]{B0000001, // 0B1001111, // 1B0010010, // 2B0000110, // 3B1001100, // 4B0100100, // 5B0100000, // 6B0001111, // 7B0000000, // 8B0000100, // 9};/*** Initializes the pins as outputs and sets a seed for the random number generator.** The decenaTr and unidadTr pins are set as outputs to control the transistor* that activates the tens or units display. Then, the 8 pins are set* necessary to control each segment of the displays as outputs.** Finally, a seed is set for the random number generator* by reading the analog value from pin A2.*/void setup(){pinMode(decenaTr, OUTPUT);pinMode(unidadTr, OUTPUT);for (int led = 0; led < 8; led++){pinMode(pins[led], OUTPUT);}randomSeed(analogRead(A2));}/*** Main loop of the program. It is responsible for:* - Reading the state of the button* - Changing the value of the pulse variable if the button is pressed* - Generate a random number between 0 and 99 when the button is pressed* - Separate the number into tens and units* - Display the number on the displays* - Pause for 10ms* - Display the number on the displays** @return void*/void loop(){bool buttonState = digitalRead(A0);if (buttonState != previousState && buttonState == HIGH)pulse = !pulse;previousState = buttonState;if (pulse){byte randomNum = random(100);separateNumber(randomNum);delay(10);}showNumber();}/*** Separates a random number into its tens and units components.** This method takes a random two-digit number (0-99) and calculates* the value of the tens and units. The calculated values are stored* in the global variables 'unit' and 'tens'.** @param randomNumRandom number to be separated into tens and units.*/void separateNumber(byte randomNum){unit = randomNum / 10;tens = (randomNum % 10) / 1;}/*** Displays the number separated into tens and units on the displays.** This method is responsible for showing on the displays the number* separated into tens and units. It first activates the transistor* for the tens and shows the value of the tens on the display,* then waits a time defined in the variable 'pause' and* finally does the same for the unit.*/void showNumber(){digitalWrite(tensTr, HIGH);digitalWrite(unitTr, LOW);PORTD = anodeNumbers[tens];delay(pause);digitalWrite(tensTr, LOW);digitalWrite(unitTr, HIGH);PORTD = anodeNumbers[unit];delay(pause);}
Post a Comment
Hello! We're so glad you've made it this far and are reading this article on Edeptec.
This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.
» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?
Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.
Thank you for being part of this learning community! Your participation is what makes this project grow.