IAN LANG ELECTRONICS

Making a Minigambler with your Arduino.

Go Back Page 1

If I were you I'd go and put the kettle on now. Fetch a new packet of ciggies whilst you're at it and a bag or two of Monster Munch, a couple of sandwiches and a Wagon Wheel. You might want to consider filling a flask. This is going to be a long one. Ready? Let's begin.

 

When you build the circuit and fire your board up with the code, the LEDs will flash on and off once except for the first green one which will stay on. Now if you open the window on your Arduino IDE you'll see precisely nothing in it, but your board might reset itself.  Make sure the Baud rate is set to 9600.

There are three buttons on this device, Mode, Choose and Go. Mode is whichever button you've attached to pin A5 of your Arduino board. Press it, and your window should now say:

 

Mode is  2

 

Mode 2 is the dice mode. Whilst we're in it we might as well have a go at it. Press the Go button. (it's whichever one you've attached to A3) and the window should tell you what the value of green and red is going to be. In my case it said:

 

Green  is  4

Red is  3

 

and sure enough four green LEDs lit up and so did three red ones. Whichever is the winning roll (in this case green) wil flash briefly.

 

Press the mode button again and the window will tell you it's now in mode 3. Mode 3 is the coin tosser. press the Go button, and the lights will bounce up and down. Eventually either all the red LEDs will come on or all the green ones will. The chance of either is completely even and can't be predicted.

 

Press the mode button again and we go back to mode 1, which is place your bet. For this mode we need the chooser button attached to pin A4 of your board. Press it and you'll see the green LEDs change lit postions. This is letting you choose which corresponding red you think is going to light  when the cycle is over. Press the Go button and watch what happens both in your window and on your LED display. If you lose, the red LEDs flash and go off.  Keep going until you get a win. The Green LEDs will flash and then go back to their original state.

 

The serial output to the IDE window need not be there; I used it to help debug as I went. If you want rid of it delete every line in the code which is followed by //****************************

 

Going through the code in chunks:

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 () {

 

All of these lines are setting up the variables we need. The two arrays hold the pin numbers of the pins the LEDs are attached to, demode keeps track of the mode the device is currently in, j is a variable we are going to use in for/next loops, greencount will be used in mode 1 to keep track of where the lit green LED is, rdm and rdn are to be random numbers.

 

 

void setup () {

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

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

    digitalWrite (j,HIGH);

    delay (200);

      digitalWrite (j,LOW);

    digitalWrite (greenarray[0],HIGH);

 

Not much happens in the setup, just some switching on and off of the LEDs in a test pattern. It's achieved by having a for/next loop switch on then rapidly off each LED in turn.

 

 

void loop (){

if (digitalRead (A5)==HIGH){

  modeswitch ();}

  if (digitalRead(A4)==HIGH){

    chooser();}

  if (digitalRead (A3)==HIGH){

  go();}  

}

 

void loop has hardly any code of its own. It consists merely of three conditionals telling it what to do when the buttons are pressed. The default mode of the device is mode 1 (place your bet) and so let's assume that's what we're in to start with. If we pressed the mode button (attached to pin A5) we'd call the function modeswitch which looks like this:

 

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);

}

 

In the switch......case statement we can take a variable with a numeric checkflag (in this case devcount) and state what to do in each possible state it can be in. It's more convenient than using a series of if statements. As you can see, we increment the checkflag by 1 unless it's at 3 (which is the highest value) in which case we reset it to one. Again the serial output lines don't need to be here (I used them to debug) and we could delete them without harming the program. The last command in the function is a delay and is there to eliminate button bounce and slow down the change rate.

Once the code is executed the run returns to void loop ( ) and encounters:

if (digitalRead(A4)==HIGH){

    chooser();}

  if (digitalRead (A3)==HIGH){

  go();}  

}

 

The first line of chooser ( ) is :

 

 if (devmode==1){

 

and the whole function is enclosed in that if. If we are not in mode 1 then pressing the chooser button will have no effect- it has been locked out by that if statement. Let's say we are in mode 1.. The next lines to be executed are:

 

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

    digitalWrite (j,LOW);}

 

which turn the whole range of green LEDs off. It's followed by:

 

  greencount=greencount +1;

 

which increments the variable we are using to keep track of where the lit LED is (or rather is going to be at the moment) and following that:

 

if (greencount>5) {

    greencount =0;}

 

This is because there are only six LEDs in that range, and we're beginning the array at position zero, not position one. So position 5 is the top of the range, so we set it back to position zero, the bottom.

 

Next is:

 

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

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

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

digitalWrite (greenarray[greencount],HIGH);

 

 

Again, the serial output was used for debugging and could be deleted if you want to.

 

digitalWrite (greenarray[greencount],HIGH);

 

The above line switches on the LED in the greencountth postion of greenarray by setting the pin it's attached to high, and

 

  delay (300);

 

Eliminates button bounce and slows the rate of change. This is the last line of the code and the run returns to void loop ( ) and encounters:

 

  if (digitalRead (A3)==HIGH){

  go();}  

 

which in plain English means if we press the Go button, the code in function go gets executed. It looks, in its entirity, like below:

 

 

       

 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

It is a very complex beast and has conditionals within conditionals and nested loops all over the place. It's about 2 hours worth of agile programming, but it's going to take me more than that to write the explanation from start to finish. Remember that tea and those sandwiches I mentioned earlier? Break them out and settle in, this is going to be a long one.

More