Go Down

Topic: SainSmart 16x2 LCD Shield [SIMPLE BINARY GAME] (Read 864 times) previous topic - next topic

TenKOhms

Feb 13, 2013, 03:25 am Last Edit: Feb 13, 2013, 03:30 am by TenKOhms Reason: 1
hey all, first post so sorry if im breaking any rules. I made a small Decimal to Binary "game" that is compatible with the SainSmart LCD Shield that I thought I would share. It's not perfect but it works :)

Used an Arduino Mega as the board, needed something with an extra analog pin to get a random seed, but it should still work on the Uno. I used Arduino 1.0 to compile and upload the sketch as well as the 1.0 library provided by SainSmart.

3 levels, Noob, Engineer, and MLG Pr0. Each one changes the rounds 3,5, and 7 respectively. A random Decimal number is displayed and your score is based on how fast u can convert into binary. after you complete the number of rounds that u picked, your score will be displayed.

Level choose screen, select to confirm, up/down to change levels.

game screen left/right to change cursor position, up to change bit to a 1, down to change to a 0. after you change a bit the cursor automatically moves to the right (towards LSB).


Download:
https://www.box.com/s/linu4l1es0vx4r8o4n3o

or the code below:
Code: [Select]
#include <LiquidCrystal.h>
#include <DFR_Key.h>

//Pin assignments for SainSmart LCD Keypad Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//---------------------------------------------

DFR_Key keypad;

///SCORE TRX/////



////SCORE TRX END//////


////PRE_GAME////

void GAME_CALL (int &SCORE, int SCORE_LIMIT)
{
 int GAMES_COUNT = 0;
 Serial.println(SCORE_LIMIT);
 for(GAMES_COUNT = 1; GAMES_COUNT <= SCORE_LIMIT; GAMES_COUNT++)
 {
   PLAY_THE_GAME(SCORE);
 }
}
///////////////

////PLAY THE GAME////
int PLAY_THE_GAME (int &SCORE)
{
 int RANDOM_INT = 0;
 int USER_INPUT[9];
 int ARRAY_INDEX = 0;
 int BUTTON_Y = 0;
 int USER_COMPARE = 0;
 int EXPONENT = 0;
 int BINARY_INPUT = 0;
 int OLD_BUTTON_Y = 0;
 int COUNTER = 0;
 int POSITION = 0;
 int BLINK = 0;
 float TIME = 0;
 char CHAR_HOLDER[10];

 randomSeed(analogRead(7));
 RANDOM_INT = random(1,256);
 
 COUNTER = 0;
 lcd.clear();
 lcd.setCursor(4,0);
 sprintf(CHAR_HOLDER, "%d", RANDOM_INT);
 lcd.print(CHAR_HOLDER);
 lcd.setCursor(0,1);

 for (COUNTER = 0; COUNTER <= 7; COUNTER++)
 {
   USER_INPUT[COUNTER] = 0;
   sprintf(CHAR_HOLDER, "%d", USER_INPUT[COUNTER]);
   lcd.print(CHAR_HOLDER);
 }
 USER_INPUT[7] = 0;
 COUNTER = 0;
 POSITION = 0;
 while (1)
 {
   BUTTON_Y = keypad.getKey();
   
   if (BUTTON_Y != 0)
   {
     if (BUTTON_Y == 3)
     {
       BINARY_INPUT = 1;
     }

     if (BUTTON_Y == 4)
     {
       BINARY_INPUT = 0;
     }

     if (BUTTON_Y == 2)
     {
       if (POSITION <= 0)
       {
         POSITION = 0;
       }
       else
       {
         lcd.setCursor(POSITION,1);
         lcd.print(USER_INPUT[POSITION]);
         POSITION--;
       }
     }

     if (BUTTON_Y == 5)
     {
       if (POSITION >= 7)
       {
         POSITION = 7;
       }
       else
       {
         lcd.setCursor(POSITION,1);
         lcd.print(USER_INPUT[POSITION]);
         POSITION++;
       }
     }

     if ( (BUTTON_Y == 4) || (BUTTON_Y == 3) )
     {
       lcd.setCursor(POSITION, 1);
       sprintf(CHAR_HOLDER, "%d", BINARY_INPUT);
       lcd.print(CHAR_HOLDER);
       USER_INPUT[POSITION] = BINARY_INPUT;
       USER_COMPARE = 0;
       ARRAY_INDEX = 7;
       for(COUNTER = 0; COUNTER <= 7; COUNTER++)
       {
         EXPONENT = pow(2, COUNTER);
         if (COUNTER > 1)
         {
           EXPONENT++;
         }
         USER_COMPARE = USER_COMPARE + (USER_INPUT[ARRAY_INDEX] * EXPONENT);
         ARRAY_INDEX--;
       }//end USER_COMPARE change
       Serial.println(USER_COMPARE);
       POSITION++;
       if (POSITION >= 7)
       {
         POSITION = 7;
       }
       if (POSITION <= 0)
       {
         POSITION = 0;
       }
       
     }
     if (USER_COMPARE == RANDOM_INT)
     {
       break;
     }
     BUTTON_Y = 0;
     delay(125);
     TIME = TIME + .125;
   }
   if (BUTTON_Y == 0)
   {
  delay(10);
   TIME = TIME + .10;
   if (BLINK >= 3)
   {
     lcd.setCursor(POSITION,1);
     sprintf(CHAR_HOLDER, "%d", USER_INPUT[POSITION]);
     lcd.print(CHAR_HOLDER);
     lcd.setCursor(POSITION,1);
     BLINK = 0;
   }
   if (BLINK == 1)
   {
     lcd.setCursor(POSITION,1);
     lcd.print("_");
     BLINK++;
     lcd.setCursor(POSITION,1);
   }
   BLINK++;
   }
 }
 SCORE = SCORE + (600/TIME);
 Serial.println(SCORE);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("SUCCESS!");
}

////PLAY THE GAME END//////


void setup()
{
 Serial.begin(9600);
 lcd.begin(16, 2);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("**Binary Game!**");
 lcd.setCursor(0,1);
 lcd.print("A NiTyTech Corp.");
 delay(2500);
 randomSeed(analogRead(15));

}

void loop()
{
 int SCORE = 0;
 int LEVEL = 0;
 int LEVEL_CHANGE = 0;
 int SCORE_LIMIT = 0;
 int BUTTON_Y = 0;
 char CHAR_HOLDER[7];
 
 LEVEL_CHANGE = 1;
 LEVEL = 1;
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Choose a level:");
 lcd.setCursor(0,1);
 lcd.print("NOOB [3]");
 SCORE_LIMIT = 3;

 while (BUTTON_Y != 1)
 {
   BUTTON_Y = keypad.getKey();
   if (BUTTON_Y == 3)
   {
     if (LEVEL >= 3)
     {
       LEVEL = 3;
     }
     else
     {
       LEVEL++;
     }
   }

   if (BUTTON_Y == 4)
   {
     if (LEVEL <= 1)
     {
       LEVEL = 1;
     }
     else
     {
       LEVEL--;
     }
   }
   if (LEVEL != LEVEL_CHANGE)
   {
     LEVEL_CHANGE = LEVEL;
     switch (LEVEL)
     {
       
     case 1:
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Choose a level:");
       lcd.setCursor(0,1);
       lcd.print("NOOB [3]");
       SCORE_LIMIT = 3;
       delay(300);
       break;

     case 2:
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Choose a level:");
       lcd.setCursor(0,1);
       lcd.print("Engineer [5]");
       SCORE_LIMIT = 5;
       delay(300);
       break;

     case 3:
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Choose a level:");
       lcd.setCursor(0,1);
       lcd.print("MLG Pr0 [7]");
       SCORE_LIMIT = 7;
       delay(300);
       break;
       
     }
   }
 }

 lcd.clear();
 lcd.setCursor(0,0);

 GAME_CALL(SCORE, SCORE_LIMIT);
 
 while(1)
 {
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("    GAME OVER   ");
   lcd.setCursor(0,1);
   sprintf(CHAR_HOLDER, "%d", SCORE);
   lcd.print("SCORE: ");
   lcd.print(CHAR_HOLDER);
   delay(10000);
 }

}

Go Up