Arduino

Making Lottery Winner using Arduino Uno

The Arduino family provides an easy way for designing a variety of circuits and also makes it easy for the new user to understand the working of the circuits as well. There are a variety of projects that can be made using the Arduino platform and, in this discourse, a simple Arduino code of lottery winner is made. The compiled Arduino code in Arduino IDE is then implemented on the circuit designed for lottery winners using the Arduino Uno board.

Making lottery winner using Arduino Uno

To design the circuit for lottery winner we have used the following components which are:

  • Arduino Uno
  • Jumper wires
  • Breadboard
  • Potentiometer
  • LCD
  • Push button

The image for the schematic of the circuit designed for creating a lottery winner is posted below:

Arduino Lottery Winner hardware implementation

We have connected the components in such a way that first all the devices are placed on the breadboard. Next we have used connecting wires to interface the components with the Arduino.

We have used the pins 6,5,4,3 and 2 of Arduino to send data over the LCD. Also we have given the supply to the top most  line of the breadboard as they are connected horizontally and grounded the next line to the top most line of the breadboard  using the 5 volt and ground pin of Arduino.

From there we can give supply to the components placed on the breadboard and the  potentiometer  output is connected to the V0 pin of the LCD for controlling  the brightness of the LCD.

The push button is connected to the reset pin of the Arduino and its other pin is connected to the ground pin of the breadboard.

For the giving the clear picture of the connections of each component in the project  we have given an image of the hardware assembly below:

Arduino code for lottery winner project 

To design a lottery winner we have used two main functions one is the random() and the other is the randomSeed() function.

The random() function  needs two inputs to define the range for generating the random numbers: the first one is the lowest number from which it will start and the second one is the highest number that defines the maximum number it can generate. The minimum value is an optional argument as the function takes zero as a minimum value. This function generates numbers in a certain range specified by the user.

Similarly, the randomSeed() function is used for generating  different  random sequences  every time when  the code is compiled. This function has one argument that is the output of the analog pin of the Arduino which is not connected.It is important to note that without randomSeed() function you will get the same values.

Random number= random(minimum, maximum);

randomSeed(analogRead(pin));

The Arduino code for lottery winner project is:

// declaring the random number variables with long data type  
long rn1;
long rn2;
long rn3;
long rn4;
#include   // library for the LCD        
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);// Arduino pins for LCD
void setup() {
  Serial.begin(9600); // serial communication
  lcd.begin(16,2);// initializing the dimensions of LCD
   randomSeed(analogRead(0));// function for shuffling the random numbers
  lcd.setCursor(4,0); // setting place for the data to be displayed
  lcd.print("Arduino");// data to be printed on LCD
  lcd.setCursor(1,1);// setting place for the data to be displayed
  lcd.print("Lottery Winner");// data to be printed on LCD
delay(3000); // time for the data will be displayed in the LCD
lcd.clear();// clearing the LCD
   rn1=random(200);// generating the random number up to 300
   rn2=random(500);// generating the random number up to 500
   rn3=random(800);// generating the random number up to 800
   rn4=random(700);// generating the random number up to 700
 lcd.setCursor(0,0);// setting place for the data to be displayed
  lcd.print("Generating a");// data to be printed on LCD
  lcd.setCursor(0,1);// setting place for the data to be displayed
  lcd.print("Lottery Number");// data to be printed on LCD
  delay(3000); // time for the data will be displayed in the LCD
  lcd.clear();//clearing the LCD
  lcd.setCursor(0,0);// setting place for the data to be displayed
lcd.print("Winner is ");// data to be printed on LCD
  // using for loops the number of generating the lottery number using the generated random numbers
for (int i=0; i<=rn1; i++){
  lcd.setCursor(0,1);// setting place for the data to be displayed
  lcd.print(i);// displaying the first number in the lottery
}
lcd.setCursor(2,1);// setting place for the data to be displayed
lcd.print("-");// data to be printed on LCD
for (int b=0; b<=rn2; b ++){
  lcd.setCursor(3,1);// setting place for the data to be displayed
  lcd.print(b);// displaying the second number in the lottery
}
lcd.setCursor(5,1);// setting place for the data to be displayed
lcd.print("-");// data to be printed on LCD
for (int a=0; a<=rn3; a++){
  lcd.setCursor(6,1);// setting place for the data to be displayed
  lcd.print(a);// displaying the third number in the lottery
}
lcd.setCursor(8,1);// setting place for the data to be displayed
lcd.print("-");// data to be printed on LCD
for (int c=0; c<=rn4; c++){
  lcd.setCursor(9,1);// setting place for the data to be displayed
  lcd.print(c);// displaying the fourth number in the lottery
}
}
void loop(){}

In the Arduino code first, we have declared the variables in which the random numbers will be stored. After that the library for the LCD is defined and then the pins of the Arduino connected to the LCD are initialized.

After that the dimensions of the LCD are initialized and some data is printed in the LCD using the lcd.print() and lcd.setCursor() function.

We have generated 4 random numbers using the random() function and to shuffle the sequences every time we have used randomSeed() function by giving it the output of an unconnected pin of the Arduino using analogRead() function.

After the random numbers are generated, we have used the for loops to further generate a number ranging from 0 to that specific generated random number.

To re-run the code and to generate another lottery number again, we have used an external reset button by connecting its one pin to the ground and the other pin to the reset pin of the Arduino Uno. Also we have used the potentiometer to control the brightness of the LCD. The numbers are then displayed by using the lcd.print() and lcd.setCursor function on the LCD.

Arduino Lottery Winner simulation

First we have done the simulation on a proteus software and is shown below:

Arduino Lottery Winner output on hardware

After the simulation we have done the hardware implementation and  the images  for the output of the Arduino code compiled for lottery winner implementation are posted in the program execution sequence.

Generating a lottery number by pressing the button:

The generated lottery number is shown below:

Conclusion

To make circuits on beginner or advanced level the Arduino platform is a viable option as it makes the interfacing of peripherals easy with microcontrollers. In this write-up we have made a lottery number generator by getting random numbers using the random() function. Moreover, to change the sequence of the generation of the random numbers we have used the randomSeed() function.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.