IAN LANG ELECTRONICS

Making a Minigambler with your Arduino.

The first question you'll be asking here is "what in God's name is a minigambler?"

It's a word I've just made up to describe this little device which has 3 modes, one where you place a bet on which LED is going to light, one where two dice roll at the same time, and the other the electronic version of a tosser. That's coins, you understand, not what you might have been thinking. You can go through the modes by means of a pushbutton, and two more let you pick and activate. Here's the schematic:

Go Back

Oooh...no, now mmmm.....

Those of you paying attention will now be saying something along the lines of "hang on I thought this section was stuff you could make with your ARDX kit?? Those buttons didn't come in the kit! You Lang, are a cad and a blackguard!"

It's a fair cop. Those buttons came from Kemoelectronics in Germany and I found them in a lucky bag in a Maplin shop. They come on a board in a pair, and strangely (and I can't fathom why) the behaviour of one affects that of the other.

So I snipped them apart with a pair of kitchen scissors and now they work sublimely.  I used them because they aren't as fiddly to reach as the supplied buttons and they don't have a habit of popping out every now and then. It's still just as feasible to use the supplied buttons though. You set them up like this:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Whichever kind you use the idea is to set the input pins to a reliable default low. In addition to the buttons you'll need three 10k resistors, six green LEDs and six red ones, and 12 560R resistors as the forwards for those LEDS. So much for the hardware, let's look at some code:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

 

5V

GND

Input Pins

5V

GND

Input Pins

////// MiniGamble////////

///// Circuit ard03 /////

///////http://ianlangelectronic.webeden.co.uk/

//MODE NUMBERS////

// 1 is place your bet

// 2 is dice throw

// 3 is coin toss

int redarray []={8,9,10,11,12,13,};

int greenarray []={2,3,4,5,6,7,};

int devmode =1;

int j;

int greencount=0;

int rdm;

int rdn;

void setup () {

  Serial.begin(9600);//~****************************

    for (j=2;j<14;j++){

    digitalWrite (j,HIGH);

    delay (200);

      digitalWrite (j,LOW);

    digitalWrite (greenarray[0],HIGH);

  }

}

 

void loop (){

if (digitalRead (A5)==HIGH){

  modeswitch ();}

  if (digitalRead(A4)==HIGH){

    chooser();}

  if (digitalRead (A3)==HIGH){

  go();}  

}

void modeswitch (){

  switch (devmode){

    case 1:

    devmode = 2;break;

    case 2:

    devmode =3; break;

    case 3:

    devmode=1;break;

     }

     Serial.print("Mode is  ");///*********************

  Serial.print (devmode);///************************

  Serial.println ();///*****************************

      delay (500);

}

 

void chooser (){

 

  if (devmode==1){

    for (j=2;j<8;j++){

    digitalWrite (j,LOW);}

  greencount=greencount +1;

  if (greencount>5) {

    greencount =0;}

     Serial.print("Greencount is  ");///*********************

  Serial.print (greencount);///************************

  Serial.println ();///*****************************

digitalWrite (greenarray[greencount],HIGH);

  delay (300);

  }}

 

  void go () {

   

    switch (devmode){

      int x;

        case 1: ////place your bet

        randomSeed(analogRead (A0));

    rdm =random (0,6);

       Serial.print("Random is  ");///*********************

  Serial.print (rdm);///************************

  Serial.println ();///*****************************

      while (x<40){

    for (j=0;j<6;j++){

         digitalWrite (redarray [j],HIGH);

      delay (x+20);

      digitalWrite (redarray [j],LOW);}

    x=x+1;}

    digitalWrite (redarray [rdm],HIGH);

   

    delay (2000); x=1;

    if (greencount==rdm){

        Serial.print("Match..... ");///*********************

          Serial.println ();

       digitalWrite (redarray [rdm],LOW);

    for (int y=1;y<20;y++){

      for(j=0;j<6;j++){

        digitalWrite (greenarray[j],HIGH);

        delay (30);

         digitalWrite (greenarray[j],LOW);}}

         digitalWrite (greenarray[greencount],HIGH);}

         else{

               for (int y=1;y<20;y++){

      for(j=0;j<6;j++){

        digitalWrite (redarray[j],HIGH);

        delay (30);

         digitalWrite (redarray[j],LOW);}}

         digitalWrite (greenarray[greencount],HIGH);}

         break;

         ///end of place your bets

   case 2:

             randomSeed(analogRead (A0));

    rdm =random (0,6);

     rdn =random (0,6);

      Serial.print("Green  is  ");///*********************

  Serial.print (rdm+1);///************************

  Serial.println ();///*****************************

    Serial.print("Red is  ");///*********************

  Serial.print (rdn+1);///************************

  Serial.println ();///*****************************

    for (j=0;j<20;j++){

      for (int t=0;t<6;t++){

        digitalWrite (greenarray [t],HIGH);

        digitalWrite (redarray [t],HIGH);

        delay (50);

                digitalWrite (greenarray [t],LOW);

        digitalWrite (redarray [t],LOW);}}

        roller ();

              delay (2000);

     if (rdn==rdm){

       for (j=1;j<40;j=j+1){

       for (int t=0;t<6;t++){

         digitalWrite (greenarray [t],HIGH);

         digitalWrite (redarray [t],HIGH);

         delay (20);

                  digitalWrite (greenarray [t],LOW);

         digitalWrite (redarray [t],LOW);}}}

         if (rdn>rdm){

           for (j=0;j<20;j++){

             for (int t=0;t<6;t++){

               digitalWrite (redarray [t],HIGH);

               delay (20);

               digitalWrite (redarray [t],LOW);}}

               roller ();

             }

                   if (rdm>rdn){

           for (j=0;j<20;j++){

             for (int t=0;t<6;t++){

               digitalWrite (greenarray [t],HIGH);

               delay (20);

               digitalWrite (greenarray [t],LOW);}}

           roller ();  

           }  

               

    delay (300);

    break; //end of dice

      case 3:

               randomSeed(analogRead (A0));

    rdm =random (0,2);

         Serial.print("rdm is  ");///*********************

    Serial.print (rdm);///************************

  Serial.println ();///*****************************

   

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

     for (j=2;j<14;j++){

          digitalWrite (j,HIGH);

          delay(50);

          digitalWrite (j,LOW);}}

          for (j=0;j<6;j++){

            if (rdm==1){

              digitalWrite (greenarray[j],HIGH);}

      else {

        digitalWrite (redarray[j],HIGH);}}

        delay (300);

        break;//end of coin toss

     

  }//end of switch

}//end of function

 

void roller (){

for (j=0;j<6;j++){

          if (rdm>=j){

            digitalWrite (greenarray [j],HIGH);}

            if (rdn>=j){

              digitalWrite (redarray [j],HIGH);}}}

         

Yep. This is a doozy. If you build the circuit and copy and paste the code to your board, we'll see over the page what it does.

More>