Roulette

by Kladenstein2 in Circuits > Arduino

66 Views, 1 Favorites, 0 Comments

Roulette

14dff4ab-5d03-4bf8-b2cc-8059dfedce49.jpg

This is a simple Roulette project made with Arduino Mega 2560. Roulette is a popular casino game that involves a spinning wheel, a small ball, and a betting table. The objective of the game is to predict where the ball will land after the wheel stops spinning. In this project, spinning wheel is the LED circle (18 red, 18 blue and 1 green), ball is represented as turning on and off the leds.

Supplies

10k-potans.jpg
10mm led.jpg
Arduino Mega 2560.jpg
Button.jpg
Keypad.png
LCD 16x2.png
  1. 1 Arduino Mega 2560
  2. 1 4x4 Keypad
  3. 1 4-Pins Push Button
  4. 1 16x2 LCD Screen
  5. 1 Potentiometer
  6. 18 Red 10mm LED
  7. 18 Blue 10mm LED
  8. 1 Green 10mm LED

Ekran görüntüsü 2024-10-13 145023.png

The first step is to solder the LEDs onto the stripboard in a circular shape.

f669c86f-f3fc-4162-b515-a69063e25177.jpg

The second step is to connect the LEDs to the Mega's I/O pins from the back of the stripboard, using the pins you've chosen (I chose pins 22 through 53 and 2 through 6).

1062a3c2-3f46-4fd1-8549-15e609e1df80.jpg

The second step is to solder the LCD and potentiometer onto the stripboard. Next, solder the VDD and Ground pins of both the LCD and potentiometer together, and then connect them to the Mega. After that, solder the VO pin of the LCD to the control pin of the potentiometer to control the brightness of the LCD. (I chose pins 7, 8, 9, 10, 11, and 12 for connecting the LCD to the Mega.)

6c066f86-3c20-463f-a3f0-c6b3c88fe7e5.jpg
c31552ad-45cd-4b39-bee3-9026d6eb8ab5.jpg

Add a button to reset the code and start the program again. First solder the button onto stripboard and connect it to the reset pin of the Mega (Mega resets when inputs zero to reset pin = the reset pin is active-low).

06b9ee63-afe9-4684-8c96-dc6e388baab4.jpg
48e72217-22d2-4b19-812e-783abdcfa17c.jpg

Stick the keypad onto stripboard (not backside) and connect the pins to Mega from the backside (row pins = A15,A14,A13,A12, column pins = A11,A10,A9,A8).

Roulette Project with Arduino Mega 2560

At last, code the Arduino Mega.


#include <Keypad.h>

#include <LiquidCrystal.h>


#define MAX_PLAYERS 9 // max num. of players

int target_number;

int bet_counter = 0;

char target_color;

int counter_to_determine_players = 0;

int counter_to_determine_bet = 0;

int players=0;

int counter =0;

int del = 10;

int numbers[37] = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,2,3,4,5,6}; //pins of the LEDs

int curserPos=0;

struct Players_input{

 int bet;

 char color;

};


Players_input player_list[MAX_PLAYERS];

int player_counter=0;


const byte rows = 4; // byte = unsigned char or uint8_t

const byte columns = 4;


char keys [rows][columns] = {

 {'1','2','3','A'},

 {'4','5','6','B'},

 {'7','8','9','C'},

 {'*','0','#','D'}

};


byte rowpins[rows] = {A15,A14,A13,A12};

byte columnpins[columns] = {A11,A10,A9,A8};


Keypad keypad = Keypad(makeKeymap(keys), rowpins, columnpins, rows, columns);

LiquidCrystal lcd(7,8,9,10,11,12);


void setup() {

 randomSeed(generateComplexSeed());

 target_number = random(0,37);

 if(target_number == 22||target_number ==24||target_number ==26||target_number ==28||target_number ==30||

 target_number ==32||target_number ==34||target_number ==36||target_number ==39||target_number ==41||

 target_number ==43||target_number ==45||target_number ==47||target_number ==49||target_number ==51||

 target_number ==53||target_number ==3||target_number ==5){

  target_color = 'C'; //blue

 }

 else if (target_number == 23||target_number ==25||target_number ==27||target_number ==29||target_number ==31||

 target_number ==33||target_number ==35||target_number ==38||target_number ==40||target_number ==42||target_number ==44||

 target_number ==46||target_number ==48||target_number ==50||target_number ==52||target_number ==2||target_number ==4||target_number ==6) {

  target_color = 'B'; //red

 }

 else{

  target_color = 'D'; // green

 }


 for (int i=0; i<=36; i++) {

  pinMode(numbers[i], OUTPUT);

 }

 Serial.begin(9600);

 lcd.begin(16,2);

 delay(1000);

 lcd.clear();

 lcd.print("Player Count: ");

 lcd.setCursor(0,1); // column 0, line 1

 lcd.blink();


}


void loop() {

 char key = keypad.getKey();

 //-----------------------------------------------------------------------------------------

 //player count

 if (key && key >= '0' && key <= '9' && counter_to_determine_players!=1) { // Check if the key is a digit

  players = key - '0';                        // Convert character to integer

  counter_to_determine_players++;

  for(int i=0;i<players;i++){

   player_list[i].bet=0;

   player_list[i].color='N';

  }

  lcd.clear();

  delay(500);

  key = keypad.getKey();

 }

 //-----------------------------------------------------------------------------------------

 //bet


 if(counter_to_determine_players && player_counter != players){ //after first input, you always enter here after initializing char key = keypad.getKey();

  lcd.setCursor(0, 0);

  lcd.print(String(player_counter+1) + ". Bet Amount: ");

  lcd.setCursor(curserPos,1);

  lcd.blink();

  

  if (key && key >= '0' && key <= '9' && counter_to_determine_bet == 0){

   player_list[player_counter].bet=key-'0';

   counter_to_determine_bet++;

   lcd.print(key);

   curserPos++;

  }

  else if(key && key >= '0' && key <= '9' && counter_to_determine_bet == 1){

   player_list[player_counter].bet=player_list[player_counter].bet*10 + (key-'0'); //key -'0' subtraction on characters with respect to their ascii numbers

   counter_to_determine_bet++;

   lcd.print(key);

   curserPos++;

  }

  else if (key && key >= '0' && key <= '9' && counter_to_determine_bet == 2){

   player_list[player_counter].bet=player_list[player_counter].bet*10 + (key-'0');

   counter_to_determine_bet++;

   lcd.print(key);

   curserPos++;

  }

   

  else if (key =='B'){

   lcd.clear();

   delay(1000);

   key = keypad.getKey();

   curserPos = 0;

   lcd.print("Color:B(Red)");

   lcd.setCursor(0, 1);

   lcd.print("C(Blue),D(Green)");

   while (key != 'B' && key != 'C' && key != 'D'){

    key = keypad.getKey();

    if (key){

     player_list[player_counter].color=key;

     player_counter++;

     counter_to_determine_bet=0;

     bet_counter++;

     lcd.clear();

     delay(1000);

    }

   }

  }

 }

 //bet

 //-------------------------------------------------------------------------------

 //determine color

 if (player_counter == players && bet_counter){

  rotating_roulette();

  for (int i =0;i<players;i++){

   if (player_list[i].color == target_color){

    lcd.print(String(i+1)+". "+String(player_list[i].bet * 2) +" $ You won");

    delay(5000);

    lcd.clear();

    delay(1000);

   }

  }

  while(1){}

 }

 //determine color

 //-------------------------------------------------------------------------------

}


// rotate the roulette

int rotating_roulette() {

  while(counter <4){

   for (int k=0; k<3;k++){

    for (int i=0; i<=36; i++){

     digitalWrite(numbers[i], HIGH);

     delay(del);

     digitalWrite(numbers[i], LOW);

   }

  }

 del+=10;

 counter++;

 }

 del+=30;

 for (int b=0; b<2;b++){

  for (int i=0; i<=36; i++){

   digitalWrite(numbers[i], HIGH);

   delay(del);

   digitalWrite(numbers[i], LOW);

  }

  del+=10;

 }

 

 for(int n=0;n<target_number;n++){

 digitalWrite(numbers[n], HIGH);

 delay(del);

 digitalWrite(numbers[n], LOW);} 

 digitalWrite(numbers[target_number], HIGH);

 return 0;

}


unsigned long generateComplexSeed() {

 unsigned long seed = analogRead(0);

 seed ^= (unsigned long)analogRead(1) << 16;


 return seed;

}