Arduino find the number game

Find the number game

Find the number game

I think I have programmed my first computer game. Computer being an 8bit micro controller 🙂 I hooked up 4 push buttons and an I2C LCD to an Arduino Uno. The first push button adds 1 to the number shown on the LCD, the second detracts 1, the third multiplies the number by 2 and the fourth divides by 2.  The goal is to find a random number between 0 and 99.  This was my first struggle, how do you generate a random number in Arduino? The C/C++ language has the random function but this generates the same value every time you setup the Arduino.  So someone smarter then me made a library that generates true random numbers based on the noise of the analog 0 input. There is one downside, this library doesn’t work with the Arduino Mega. Strange but that’s the way it is. By using the “abs” function the sketch will calculate the difference of the number and the random number, based on this difference the LCD will tell if you are close to the random number. It did take me some time before I came up with a sketch that did what I wanted, but I learned some new things. And that was the reason I made this 🙂

Here is the sketch:

// http://www.bajdi.com
// Find a number between 0 and 99 with 4 push buttons.
// The first push adds 1, second button subtracts 1, 
// the third button multiplies by 2, the fourth button divides by 2
// Numbers are displayed on an I2C LCD.
// Use the reset button on the Arduino to restart the game.
// The TrueRandom library does NOT work with the Arduino Mega

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TrueRandom.h>

LiquidCrystal_I2C lcd(0x27,16,2);

const int buttonplus = 2;    // the number of the +1 pushbutton pin
const int buttonmin = 3;    // the number of the -1 pushbutton pin
const int button2 = 4;    // the number of the x2 pushbutton pin
const int buttond2 = 5;    // the number of the /2 pushbutton pin
const int led =  13;
int buttonpluspresses = 0;
int buttonminpresses = 0;
int tempnumber;

int lastButtonplusState;
int lastButtonminState;
int lastButton2State;
int lastButtond2State;
int ledState = LOW;   

int readingplus = 0;
int readingmin = 0;
int reading2 = 0;
int readingd2 = 0;

const int winningnumber = TrueRandom.random(100); 

void setup() {
  pinMode(buttonplus, INPUT);
  pinMode(buttonmin, INPUT);
  pinMode(button2, INPUT);
  pinMode(buttond2, INPUT);
  pinMode(led, OUTPUT);
  
  lcd.init(); // initialize the lcd
  lcd.backlight();
}

void loop()
{
 
  readingplus = digitalRead(buttonplus);
  readingmin = digitalRead(buttonmin);
  reading2 = digitalRead(button2);
  readingd2 = digitalRead(buttond2);
  
  if (tempnumber <= 0)
  {
  tempnumber = 0;
  }
  
  lcd.setCursor(0, 0);
  lcd.print(tempnumber);
  
  if (readingplus != lastButtonplusState) 
  {
    if (readingplus == LOW)
    {
    buttonpluspresses ++;
    tempnumber = tempnumber + buttonpluspresses;
    lcd.setCursor(0, 0);
    lcd.print(tempnumber);
    }
    lastButtonplusState = readingplus;
    buttonpluspresses = 0;
    }
  if (readingmin != lastButtonminState) 
  {
    if (readingmin == LOW)
    {
    buttonminpresses ++;
    tempnumber = tempnumber - buttonminpresses;
    lcd.setCursor(0, 0);
    lcd.print(tempnumber);
    }
    lastButtonminState = readingmin;
    buttonminpresses = 0;
  }
  if (reading2 != lastButton2State) 
  {
    if (reading2 == LOW)
    {
    tempnumber = tempnumber * 2;
    lcd.setCursor(0, 0);
    lcd.print(tempnumber);
    }
    lastButton2State = reading2;
  }
  if (readingd2 != lastButtond2State) 
  {
    if (readingd2 == LOW)
    {
    tempnumber = tempnumber / 2;
    lcd.setCursor(0, 0);
    lcd.print(tempnumber);
    }
    lastButtond2State = readingd2;
  }
    
  if (tempnumber == winningnumber)
  { lcd.setCursor(0,1);
    lcd.print("You have won!!!!");
    digitalWrite(led, HIGH);
  }
  if (tempnumber != winningnumber)
  {
    digitalWrite(led,LOW);
  }
  if (abs(tempnumber - winningnumber) > 10)
   {
    lcd.setCursor(0,1);
    lcd.print("Try harder"); 
   }
  if (abs(tempnumber - winningnumber) <= 10 && abs(tempnumber - winningnumber) > 3)
   {
    lcd.setCursor(0,1);
    lcd.print("Close"); 
   }
  if (abs(tempnumber - winningnumber) <= 3 && abs(tempnumber - winningnumber) >= 1)
   {
    lcd.setCursor(0,1);
    lcd.print("Very close"); 
   }
  delay(150);
  lcd.clear();
 }

Leave a Reply

*