Author Topic: Simple Game with LCD  (Read 4911 times)

barnacleBudd

  • Newbie
  • *
  • Posts: 21
Simple Game with LCD
« on: February 10, 2013, 11:03:03 pm »
We're having fun with the digispark, working on a number reverse game that demonstrates many of the things you can do with the I2C LCD panel. Digistump offers a shield with everything you need. I had the LCD and a couple of resistors and all works well.

Here's a video of the first art of the project. It simply demonstrates the beginnings of the user interface which will simply be the display and a push button.

 http://youtu.be/D3ZUUmgr3i0

And click here to follow the project on the blog: mypetdigispark.blogspot.com

barnacleBudd

  • Newbie
  • *
  • Posts: 21
Re: Simple Game with LCD
« Reply #1 on: February 11, 2013, 04:55:48 pm »
Hey we're getting closer ...
I realized that the 1 second delay that let's us visualize the digits shifting doesn't play nice with the push button. So I'm using a timer and millis() to let me test for key down during the delay.

Part 4: mypetdigispark.blogspot.com   How to test I/O pins and delay at the same time.


Mark

  • Full Member
  • ***
  • Posts: 196
Re: Simple Game with LCD
« Reply #2 on: February 11, 2013, 05:17:01 pm »
the link is broken, although it looks very close.

Mark

barnacleBudd

  • Newbie
  • *
  • Posts: 21
Re: Simple Game with LCD
« Reply #3 on: February 11, 2013, 08:58:05 pm »
Yes, I see that link only works from my machine try this:
http://mypetdigispark.blogspot.com/
 
 

barnacleBudd

  • Newbie
  • *
  • Posts: 21
Re: Simple Game with LCD
« Reply #4 on: February 14, 2013, 05:33:29 pm »
Here is video of the finished game.

http://youtu.be/SqxVIVnzkR0

Check the blog for details.

and here is the complete sketch including the routines that store the high score in EEPROM so that it is remembered when you power your digispark up and down.......



/* ATtiny85 as an I2C Master   Ex2        BroHogan                           1/21/11
 * Modified for Digistump - Digispark LCD Shield by Erik Kettenburg 11/2012
 * SETUP:
 * ATtiny Pin 1 = (RESET) N/U                      ATtiny Pin 2 = (D3) N/U
 * ATtiny Pin 3 = (D4) to LED1                     ATtiny Pin 4 = GND
 * ATtiny Pin 5 = SDA on DS1621  & GPIO            ATtiny Pin 6 = (D1) to LED2
 * ATtiny Pin 7 = SCK on DS1621  & GPIO            ATtiny Pin 8 = VCC (2.7-5.5V)
 * NOTE! - It's very important to use pullups on the SDA & SCL lines!
 * PCA8574A GPIO was used wired per instructions in "info" folder in the LiquidCrystal_I2C lib.
 * This ex assumes A0-A2 are set HIGH for an addeess of 0x3F
 * LiquidCrystal_I2C lib was modified for ATtiny - on Playground with TinyWireM lib.
 * TinyWireM USAGE & CREDITS: - see TinyWireM.h
 */

// Number Flip-Flop by Budd Churchward, WB7FHC, @barnacleBudd

/* Place a push button on your board
 * Put a 1K resistor between one pin on the button and ground
 * Place a jumper between pin 5 and a point between the button and resistor
 * Place a jumper between 5V and the other side of the button
 */

#include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI - comment this out to use with standard arduinos
#include <LiquidCrystal_I2C.h>          // for LCD w/ GPIO MODIFIED for the ATtiny85
#include <EEPROM.h>
#define GPIO_ADDR     0x27              // (PCA8574A A0-A2 @5V) typ. A0-A3 Gnd 0x20 / 0x38 for A - 0x27 is the address of the Digispark LCD modules.

int address = 0;                        // points to the first address in the EEPROM memory
byte value;                             // stores what we read at that location

LiquidCrystal_I2C lcd(GPIO_ADDR,16,2);  // set address & 16 chars / 2 lines

String winner ="0123456789";            // a test string to easily see if game is over
String workingString = "0123456789";    // these get scrambled
int rnd;                                // will hold a random number for us.
int myButton = 5;                       // hook button to pin 5 with pull down resistor see notes above
int btnPress;                           // is it down or is it up
byte tick = 0;                          // counts the digits we plan to flip
unsigned long myTime;                   // used with mySpeed to set tick rate

int mySpeed = 1000;                     // start with a 1 second interval
byte highScore = 0;                     // so the first score will always beat it
byte thisScore = 120;                    // start with 120 points

void setup(){
 
  TinyWireM.begin();                    // initialize I2C lib - comment this out to use with standard arduinos
  lcd.init();                           // initialize the lcd
  lcd.clear();                          // clear the screen
  lcd.backlight();                      // turn on the backligth
  pinMode (myButton, INPUT);            // pin 5
  pinMode(1, OUTPUT); //LED on Model A  // we light the LED when the button is pressed
  value = EEPROM.read(address);         // read the high score left here from earlier
  highScore = value;                    // move it into our highScore variable
 
  howToReset();                         // Print prompt on display to hold button down to clear high score
  if(digitalRead(myButton)){            // If button is down when we power up ...
    EEPROM.write(address,0);            // Change high score stored in EEPROM to zero
    lcd.clear();
    lcd.print("HIGH SCORE RESET");
    highScore=0;                        // reset the high score used by the sketch
    delay(2000);
  }
   
  lcd.clear();
  delay(1000);
  lcd.print("NUMBER FLIP-FLOP");
  scramblePrompt();                     // Go pick random numbers until user presses button
  scrambleString();                     // puts digits in random order
  displayWorkingString();               // prints digits
  lcd.setCursor(12,1);                  // move to bottom right corner
  lcd.print(thisScore,DEC);             // show starting score
                                        // because thisScore is type 'byte' you need to show it as decimal
  delay(1000);
  myTime = millis();                    // start timer
 
 
 
}

void loop()  {
  btnPress = digitalRead(myButton);     // read pin 5
  if (btnPress==1){                     // When button is down ...
    digitalWrite(1,1);                  // light up on-board LED
    flip();                             // go flip some digits
    delay(1000);
  }else{                                // When button is up ...
    digitalWrite(1,0);                  // turn off on-board LED
  }
  if(millis() - myTime > mySpeed ){     // when we have waited long enough
    shiftLeft();                        // move the current digit left
    myTime = millis();                  // reset interval timer
  }
}

void flip(){
  int i=0;                              // a counter
  if(tick==1){tick=10;}                 // power play, flip all if only 1 digit
  tick--;                               // undo the last increment, we don't need it
  do { 
    swap(i,tick-i);                     // swap two digits
    i++;                                // increment counter
  } while (i<=(tick/2));                // do half of them and we're done
  //lcd.clear();
  displayWorkingString();               // put the new order on the screen
  tick= 0;                              // reset the digit counter
  myTime = millis();                    // reset the interval timer
}


void shiftLeft(){
  if (tick < 10){                       // keep us in the range...
    lcd.setCursor(tick + 2,0);          // move to spot in front of our digit
    lcd.print(workingString[tick]);     // put a copy of digit in the new spot
    lcd.print(' ');                     // erase the original digit
    tick++;                             // increment the digit counter
    thisScore--;                        // subtract a point
    int x = 12;                         // cursor location for score when its 3 digits
    if(thisScore<100){x++;}             // nudge location for 2 digit score
    if(thisScore<10){x++;}              // nudge again if only 1 digit
    lcd.setCursor(x,1);                 // set the cursor location
    lcd.print(' ');                     // erase digit if we are dropping from 100's to 10's
    lcd.print(thisScore,DEC);           // show current score
   
    if(thisScore==0){                   // When points are all gone ...
      youLose();                        // Tell the user he lost
      return;                           // go back to loop()
    }

  }else{                                // if user doesn't press the button...
    tick=0;                             // start again at the beginning
    displayWorkingString();             // move the digits back in place
    }
 
  }

void scramblePrompt(){
  lcd.noCursor();                       // some LCDs want to flash at you
  lcd.print("NUMBER FLIP-FLOP");        // show the title on the top line
  lcd.setCursor(0,1);                   // move to the bottom line
  lcd.print("Press Button Now");        // prompt the user to press the button

  do {
    rnd=random(9);                      // pick random numbers over and over
  } while (!digitalRead(myButton));     // until user presses the button
 }


void howToReset(){
  lcd.print("HIGH SCORE=");
  lcd.print(highScore,DEC);
  lcd.setCursor(0,1);
  lcd.print("HOLD BUTTON NOW");
  delay(2000);
  lcd.setCursor(0,1);
  lcd.print("ZERO HIGH SCORE");
  delay(2000);
  }
 
void scrambleString() {
  lcd.clear();                          // clear the screen
  lcd.print("NUMBER FLIP-FLOP");        // print the title again
 
  for (int i=0; i<10; i++){             // do this once for each digit
    rnd=random(9);                      // pick a random spot
    swap(i,rnd);                        // swap two of the digits
  }
  delay(1000);
  lcd.clear();                          // clear the screen again
  lcd.setCursor(0,1);                   // move to bottom row
  lcd.print("HS=");                     // print the high score
  lcd.print(highScore,DEC); 
}

void swap(int x, int y){
  byte hold=workingString
  • ;           // store the first digit

  workingString
  • =workingString[y];    // copy the second digit to the first spot

  workingString[y]=hold;                // put the first digit in the second spot
}
 

void displayWorkingString(){
  lcd.setCursor(2,0);                   // move to top row
  lcd.print(' ');                       // erase the first digit
  lcd.print(workingString);             // put the whole string back in place
 
  if (workingString==winner){           // if the digits are all in order ...
    youWin();                           // go do the cool stuff for the winner
   }
}

void youWin(){
  lcd.setCursor(0,1);                   // move to the second row
  if(thisScore > highScore){            // when we get a new high score
    lcd.print("HIGH SCORE!");           
    highScore = thisScore;              // update the new high score
   
  }else{                                // when score doesn't beat high score
    lcd.print("WINNER");               
  }
  delay(3000);
  mySpeed = mySpeed - 50;               // lower mySpeed means faster game
  if(mySpeed<400){mySpeed=400;}         // don't go too fast
    EEPROM.write(address, highScore);   // store it now for a power down later
    playAgain();                        // Do it all again
}

void youLose(){
  lcd.setCursor(0,1);                   // move to the second row
  lcd.print("YOU LOSE");
  delay(3000);
  mySpeed = mySpeed + 200;              // higher mySpeed means slower game
  if(mySpeed>1000){mySpeed=1000;}       // never slower than once a second
  playAgain();                          // do it all again
}

void playAgain(){
  lcd.clear();                          // clear the screen
  scrambleString();                     // go mix up the digits again
  displayWorkingString();               // show us the new order
  thisScore = 120;                      // start again with 120 points
}