IAN LANG ELECTRONICS

So, let's make a device that does something that our microcontroller can get its teeth into. What we're going to do here is to make a toy that simulates a fruit machine, it has a display and makes musical noises when something happens, and there are six glyphs in the display, a cherry, a lemon, an orange, a bar, a lucky seven and a bell. There are four reels, and a text screen over the top telling you how much funds you have left. There are two buttons, one to roll and one to save your current funds.

 LCD Display Fruit Machine Toy

Go Back

It's this last one that introduces a new concept, that of reading and writing to the EEPROM memory. Some of you may be wondering what EEPROM is. Well, way back in the 1980s we had ROM, which is Read Only Memory, and this was a non-volatile (i.e. it wasn't erased when you switched the power off) storage memory. ROM, once programmed, could not be re-programmed; it was locked down tighter than a mediaeval nun's chastity belt. It's main use was in pocket calculators and computers; the manufacturers did not want ham-fisted end users messing about with the critical control code for the device, and the BIOS (basic input output system) of all computers was on a ROM. Advanced users moaned about not being able to change the bios, and so a PROM (Programmable ROM) that you could write to (yes, I know) came along. Then somebody had the bright idea of constructing EPROM (Erasable PROM) that  you could alter as you felt like. In the 2000s there was a fashion for sticking an E in front of everything and so now EPROM has become EEPROM (Electrically Erasable Programmable Read Only Memory). I think they just like lots of big words.

Well, whatever the superspoffs want to get up to we are going to wire up our LCD display to our Arduino just as we did in the last two pieces and according to Oomlout's schematic up there. Then we're going to do two buttons for a default low, just as below,then we are going to slap our board with the code on the left.

5V

GND

Input Pins

5V

Input Pins

GND

#include <EEPROM.h>

void setup(){

    EEPROM.write(0,50);}

void loop(){ }

Those of you of a more astute nature will not have failed to notice that as you uploaded the code above left it became apparent that quite a lot singularly failed to happen.  Ah, but you were not paying close enough attention, because what did happen was that a tiny portion of the EEPROM was prodded and given a numeric value of 50 to hold on to. Now slap your board with this:

 

#include <LiquidCrystal.h>

#include <EEPROM.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte bell[8] =  {B00100,B01010,B01010,B01010,B10001,B11111,B00100,};

byte cherry [8] =  {B11000,B00100,B10001,B11011,B11111,B11111,B11111,B01110};

byte bar [8] =  {B00000,B00000,B11111,B11111,B11111,B11111,B00000,};

byte lseven[8] =  {B1111,B1111,B00011,B00110,B0110,B01100,B01100,B11000};

byte orange [8] =  {B00000,B01110,B11001,B11111,B11111,B01110,B00000,};

byte lemon [8] =  {B00100,B01110,B11001,B11111,B11111,B11111,B01110,B00100};

int reel [4];

char rowclear [] ="                ";

int rolltone= 440;

int funds=EEPROM.read(0);

void setup() {

Serial.begin(9600);

lcd.begin(16,2);

lcd.clear();

lcd.setCursor (0,0);

lcd.createChar(0,bell);

lcd.createChar(1,cherry);  

lcd.createChar(2,bar);  

lcd.createChar(3,lseven);  

lcd.createChar(4,orange);

lcd.createChar(5,lemon);

funddisplay();

 

}

void loop (){

  if (funds==0){youlose();}

  if (digitalRead(A1)==1){saves();}

if (digitalRead(A0)==1&&funds>0){

  int rollout=5;

  funds=funds-1;

  lcd.clear();

  lcd.setCursor(0,0);

funddisplay();

for (int j=0;j<15;j++){

tone(10,rolltone,30);rolltone=rolltone+50;

if (rolltone>3000){rolltone=390;}

Serial.print("GO");

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

randomSeed(analogRead(A5));

reel[t]=random(0,6);

}//close of t<4

lcd.setCursor(7,1);

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

 Serial.print(reel[t]);

lcd.write(reel[t]);

 rollout=rollout+2;

delay (rollout);

}//close of tfor (int t=0;t<4;t++)

Serial.println();

  }//close of  for (int j=0;j<15;j++)

  if (reel[3]==reel[2]){

    tworesult();

  }//close of if (reel[3]==reel[4])

}//close of primary if  

}//close of function

 

void tworesult (){///adds winnings to funds and displays win message

  if (reel[1]==reel[2]){

    threeresult();

  }//close of if (reel[1]==reel[2]){

    else{

      lcd.setCursor(0,0);

      lcd.print(rowclear);

        lcd.setCursor(0,0);

      lcd.print("  MATCH TWO!");

      int (nl)=19;

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2};

for (int j=0; j<nl;j=j+1){//loop for nl value

tone (10,f[j],150*d[j]); // tone (PIN,FREQUENCY,DURATION)

delay (150*d [j]);} //wait until duration period has passed

delay (500);

switch (reel[3]){

case 0:

funds=funds+2;break;

case 1:

funds=funds+1;break;

case 2:

funds=funds+2;break;

case 3:

funds=funds+3;break;

 case 4:

funds=funds+1;break;

case 5:

funds=funds+1;break;

  }//close of switch

lcd.setCursor(0,0);

lcd.print(rowclear);

funddisplay();

Serial.print ("Match Two");

    }//close of else

}//close of function

void threeresult(){

if (reel[0]==reel[3]){

jackpot();

 }//close of if (reel[1]==reel[2]){

else{

lcd.setCursor(0,0);

lcd.print(rowclear);

lcd.setCursor(0,0);

lcd.print("  MATCH THREE!");

int (nl)=19;

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2};

for (int j=0; j<nl;j=j+1){//loop for nl value

tone (10,f[j],150*d[j]); // tone (PIN,FREQUENCY,DURATION)

delay (150*d [j]);} //wait until duration period has passed

delay (500);

switch (reel[3]){

case 0:

funds=funds+15;break;

case 1:

funds=funds+10;break;

case 2:

funds=funds+15;break;

case 3:

funds=funds+20;break;

case 4:

funds=funds+10;break;

case 5:

funds=funds+10;break;

 

  }//close of switch

lcd.setCursor(0,0);

lcd.print(rowclear);

funddisplay();

Serial.print ("Match Three");

 

 

}//close of else

}//close of function

 

void jackpot(){

  lcd.setCursor(0,0);

lcd.print(rowclear);

lcd.setCursor(0,0);

lcd.print("  JACKPOT!");

  int (nl)=19;

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2};

for (int j=0; j<nl;j=j+1){//loop for nl value

tone (10,f[j],150*d[j]); // tone (PIN,FREQUENCY,DURATION)

delay (150*d [j]);} //wait until duration period has passed

delay (500);

switch (reel[3]){

case 0:

funds=funds+25;break;

case 1:

funds=funds+22;break;

case 2:

funds=funds+23;break;

case 3:

funds=funds+50;break;

case 4:

funds=funds+24;break;

case 5:

funds=funds+21;break;

 

  }//close of switch

lcd.setCursor(0,0);

lcd.print(rowclear);

funddisplay();

Serial.print ("Jackpot");

}

 

void youlose(){

lcd.setCursor(0,0);lcd.print (rowclear);delay(300);

lcd.setCursor(2,0);lcd.print ("YOU LOSE!");delay (500);

int losed[]={2,2,2,2,3,2,3,2,4};

int losef []={262,262,392,392,330,659,659,523,523};

int losenl=8;

for (int j=0; j<losenl;j=j+1){//loop for nl value

tone (10,losef[j],50*losed[j]); // tone (PIN,FREQUENCY,DURATION)

delay (50*losed [j]);} //wait until duration period has passed

delay (1000);

}

void funddisplay(){

lcd.setCursor(0,0);

lcd.print ("Funds:  ");lcd.print (funds);

  }

  void saves (){

        EEPROM.write(0,funds);

  }

 

You will notice that your funds total 50. Take the small bit of code that was uploaded first and change that 50 to 25. Then re-upload the code. Then re-upload the big bit of code. You'll notice that the funds have changed to 25 too. Over the page we discuss how this comes about, and also the big bit of code which is the fruit machine.

Mini tactile types are wired like this:                              Two-terminal types are wired like this:

One of the buttons inputs to A0 and the other to A1. Doesn't matter which button goes to which input, as long as one goes to one and the other to the other.

More>