Go Down

Topic: Newbie Sketch--guess my number game (Read 2196 times) previous topic - next topic


Just a quick thanks to some of the code I've learned from all of you sifting through the forums.  I've had a little background in code and electronics for awhile now, but nothing is as much fun or quick as Arduino!
Figured I'd share my first sketch, a quick guess my number game played on a 20X4 LCD screen, using a pot as the input method (tried buttons but found them too tedious)..hope someone finds anything in value from it!  Any suggestions on how to clean up the code would be appreciated as well!
Code: [Select]

#include <LiquidCrystal.h>
#define BUTTON 7  // the input pin where the button is
int rndm; //our random number
int guessPin = 0; //our analog read pin for the guess
int val; //our button reader
int wronguess=0; //our variable for the number of guesses
int highscore=100; //our highscore variable
int lowscore=0; //our lowscore variable
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

void setup() {
 lcd.begin(20, 4); // set up the LCD's number of columns and rows..remember rows start at 0
 pinMode(BUTTON, INPUT); //define our button as an input
  numbergen(); //call numbergen for first random number

void loop() {
 int guess = (analogRead(guessPin)/10); //here our analog pins read from 0-1023 we only need
                                        //a value of 0-100 so (1023/100)=10
if (guess > 100) guess=100; //This tells ensures it will never go over 100

  val = digitalRead(BUTTON); // read input value and store it

 lcd.setCursor(0,0); //set cursor spot on the top left
   lcd.print("  Guess My Number!  "); //give a title to the game
 lcd.setCursor(0, 1); //line 1 row 0
lcd.print("Number of Guesses"); //number of guesses
 lcd.setCursor(18,1); // move cursor to line 1 row 18
lcd.print(wronguess); //print the number of wrong guesses

lcd.setCursor (0,3); //line 3 row 0
lcd.print ("Best"); //print "best"
lcd.setCursor(5,3); // move cursor to line 3 row 5
lcd.print(highscore); //print the value of our highscore

lcd.setCursor (9,3); //line 3 row 9
lcd.print("Worst"); //print "worst"
lcd.setCursor(15,3); //line 3 row 15
lcd.print(lowscore); //print the value of our worst game

 lcd.setCursor(0,2); //line 2 row 0
 lcd.print ("Your Guess:"); //print "your guess"
 lcd.setCursor(12,2); //print our current guess
 if (guess<10){    //this if basically puts a 0 in front of single numbers so
                   // screen doesn't hold numbers when going from 2 digits to 3 digits
 else {
   lcd.print(guess); //if it's 10 or greater just print the number
 if (val==HIGH) {   //if our button goes high (pressed) execute the following
  if (guess==rndm) { //if our guess matches the random number
    lcd.print("     You got it!    "); //change the heading to a win
     if (wronguess<highscore) { //we're still in the if for winning, so update
                                 // the high score if it's better than the existing
    if (wronguess>lowscore) {//we're still in the if for winning, so update
                                 // the low score if it's better than the existing
    delay (3000);
    lcd.clear(); //clear the lcd after 3 seconds
    wronguess=0; //reset our guess number
    numbergen(); //call for another random number and repeat
  else {
    if (guess>rndm){ //if our guess is greater than the random number
lcd.print("      Go LOWER      "); //print go lower
delay (1500);
  if (guess<rndm){  //if our guess is lower than the random number
lcd.print("      Go HIGHER     "); //print go higher
delay (1500);
  wronguess=wronguess+1; //count our number of guesses


void numbergen () { //our random number generator in it's simplest form
  //the numbers aren't so random using this function,
   // first number is always 7, and I forget the rest. As shown in other texts
   //uncommenting out the next line helps
   //randomSeed(analogRead(1)); //which reads the noise on an empty pin to create a "true random"

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131