Saturday, February 9, 2013

LCD Game Part 2 - Random Numbers & Push Buttons

Hey, Digispaklers, good to see you back. In this post we are going to solve the problem of generating random numbers that are truly random. If you go back to the previous post and load the code onto your own Digispark you will see that each time you power up the device, you get the same ten digit number ... that's not very random.

There are various ways to solve this problem, we will demonstrate my favorite. We are going to take advantage of the most random thing we have available: The User!

The plan is simple. When the program launches we will display a message on the LCD telling the user to press a button to begin. While we are waiting for the player to do that, we will send Sparky into a loop chasing his tail and picking random number after random number thousands of times a second. We won't start the game until that button gets pressed. This loop spins so quickly that it is highly unlikely that anyone is going to see the same random pattern twice in a row.

Let's start with the hardware. Of course, you are going to needs a button. Sometime ago I bought a bunch of them on eBay. They are not expensive. The button has four little legs. If you turn it the right way it will straddle the gab down the middle of your breadboard. 

It is important to know that the pins that cross the gap are always connected to each other. The pins that your are switching on and off run parallel to the gap. Be real careful with your board layout. When you plug one of these mini buttons in, you are linking the five points of each side of the board together. You can no longer use these sets of points for two different purposes.

We will use the button to switch 5V on and off and read it on pin 5 of the digispark. It's not quite that simple. Sparky will easily know when the pin is HIGH and the voltage is there, but he get's confused if you take the voltage away. You would think that it should go to LOW, but that might not be so. If nothing is connected to a pin, we say that it is FLOATING. And Sparky might think its LOW one moment and HIGH the next. To sort this out and make things clear, we add a 'pull down' resistor. This locks the pin down and won't let it float. The schematic here shows how we wire it up.

Almost any value resistor from 470 up will do. If your project is going to be battery powered, you will want to use a higher value to make the battery last longer. I use a 1K resistor (brown black red).

Before we go any further, we should make sure we have everything wired right. Add the highlighted lines below to your sketch and try it out. We will use the on-board LED to test our button. This LED already has its own limiting resistor so it is easy to use.


int rnd;     // will hold a random number for us.
int myButton = 5; // our button hooks up here
int btnPress;     // is it down or not


The following two lines go into setup(). These define pin 5, myButton, for input and the on-board LED on pin 1 for output.

  pinMode (myButton, INPUT);
  pinMode(1, OUTPUT); //LED on Model A



These next lines are actually the first ones that we have placed inside our Loop() function.


void loop(){
  btnPress = digitalRead(myButton);
  if (btnPress==1){
    digitalWrite(1,1);
  }else{
    digitalWrite(1,0);
  }
}

We check P5 to see if it is HIGH or LOW, or the way I like to write the code: 1 or 0. Your sketch will do everything it did before, including putting the number on the LCD. If you press the button, you should see the green LED status light come on. It goes off when you release the button.  Make sure this happens before you continue.


Ok, now let's put Sparky to work. Add the following line to startup()

  scramblePrompt();
  scrambleString();
  displayWorkingString();



And then enter the function itself


void scramblePrompt(){
  lcd.noCursor();
  lcd.print("NUMBER FLIP-FLOP");
  lcd.setCursor(0,1);
  lcd.print("Press Button Now");
  
  do {
    rnd=random(9);
  } while (!digitalRead(myButton));
 }


This is where Sparky chases his tail. Our do loop keeps picking random numbers over and over. It also check's pin 5, myButton. !digitalRead(myButton)is an efficient way to see if it is zero. The '!' means NOT. So it keeps looping as long as the pin is 'not 1'. Control won't leave this loop until you press the button. Then it goes back to call the rest of the functions in setup() and you will soon see a new ten digit number. Sparky doesn't have a reset button so you have to pull the power and put it back to see that each time you recycle, you get a new random number.


Before the next post I am going to experiment with two ways of giving the user a way to select how many of the digits on the left he or she wants to flip. One that I have done before involves programming my own character to draw a line across the top of the second row. A new one that I want to test nudges the digits one at a time one place to the left so a scrolling gap works its way across the screen. 

You can copy the whole sketch below. Check back later to see what trick we teach Sparky next. C U then.



No comments:

Post a Comment