Simple GAME to test and learn to use the Multi Function Shield

Hi!! I dont know how to program... and I used the base of the grame from:
https://forum.arduino.cc/index.php?topic=516532.0
but I can not replay that topic.

So I have updated the code..
In this game you have to input the correct number shown in binary (from 1 to 15) in 10 seconds.

Its working... ok.... but is not very random.... each time y restart the arduino it will start the same sequence of numbers.
Thanks again to the original developer
DanielBnl

#include <TimerOne.h>            // De TimerOne library wordt gebruikt
#include <Wire.h>               // De Wire library wordt gebruikt
#include <MultiFuncShield.h>   // De MultiFuncShield library wordt gebruikt

int number = 0;
const int ledPin1 = 10;           //Declaratie van de LED die op pin 10 aangesloten is
const int ledPin2 = 11;          //Declaratie van de LED die op pin 10 aangesloten
const int ledPin3 = 12;         //Declaratie van de LED die op pin 10 aangesloten
const int ledPin4 = 13;        //Declaratie van de LED die op pin 10 aangesloten
const int buttonPin = A1;     //Declaratie van de buttonPin


/* Define shift register pins used for seven segment display */
#define LATCH_DIO 4
#define CLK_DIO 7
#define DATA_DIO 8
 
/* Segment byte maps for numbers 0 to 9 */
const byte SEGMENT_MAP[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90};
/* Byte maps to select digit 1 to 4 */
const byte SEGMENT_SELECT[] = {0xF1,0xF2,0xF4,0xF8};


unsigned long Cur_ms_Count; // Stores the current time in ms
unsigned long Last_ms_Count; // Stores the last time in ms the counter was last updated
int Count; // Stores the value that will be displayed


//array van alle karakters dat in hexadecimaal past
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

int buttonState = 0;
int prevButtonState = 0;
int counting = 0;
int countTime = 10;  // tiempo para responder
int HighScore = 0;  // Puntaje

long startTime;
long InicioCuentaRegresiva;
byte hasError;

void printNumber(int number) {
  Serial.println();
  Serial.println("--------------------------");
  Serial.print("Random number: ");
  Serial.print(number);
  Serial.println();
  Serial.println("--------------------------");
}

void convertToBinary(byte number, int binary[4]) {

  for (int i = 0; i <= 3; i++) {
    if (bitRead(number, i) == 1) {
      binary[i] = 0;
    }
    else {
      binary[i] = 1;
    }
  }

}

void displayBinary(byte number) {

  int binary[4];
  convertToBinary(number, binary);

  digitalWrite(ledPin1, binary[0]);
  digitalWrite(ledPin2, binary[1]);
  digitalWrite(ledPin3, binary[2]);
  digitalWrite(ledPin4, binary[3]);

}


void WriteNumberToDigit(byte digit, byte Value)
{
  digitalWrite(LATCH_DIO,LOW);
  shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
  shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[digit] );
  digitalWrite(LATCH_DIO,HIGH);   
}

void setup() {
    /* Set DIO pins to outputs */
  pinMode(LATCH_DIO,OUTPUT);
  pinMode(CLK_DIO,OUTPUT);
  pinMode(DATA_DIO,OUTPUT);
  
  Timer1.initialize();
  MFS.initialize();    // initialiseert the Multi Function Display en de Timer1 bibliotheek
  Serial.begin(9600);        // initialiseert de seriële monitor
  randomSeed(analogRead(0));
  InicioCuentaRegresiva = millis();
  number = random(1, 16); // genereert willekeurige cijfers van 1 tot en met 15
  hasError = 0;
  startTime = millis();
  printNumber(number);
  displayBinary(number);
  HighScore = 0;  // Puntaje

  pinMode(ledPin1, OUTPUT);      // declareert LED D4 als output
  pinMode(ledPin2, OUTPUT);     // declareert LED D3 als output
  pinMode(ledPin3, OUTPUT);    // declareert LED D2 als output
  pinMode(ledPin4, OUTPUT);   // declareert LED D1 als output
  pinMode(buttonPin, INPUT); // declareert pushbutton S1 als output
}

void reset() {
  hasError = 0;
  startTime = millis();
  number = random(1, 16);
  printNumber(number);
  displayBinary(number);
  counting=0;
  countTime=10;
}

void loop() {

  byte btn = MFS.getButton();
  buttonState = digitalRead(buttonPin); 
  
  if (millis() - InicioCuentaRegresiva > 1000) {
    WriteNumberToDigit(0 , countTime);
    countTime--;
    InicioCuentaRegresiva = millis() ;

    if (countTime <= 0 ) {
      MFS.write("LOSE") ; 
      HighScore=0;
      while (!MFS.getButton()) {
      }
      delay(100);
      reset();
    }
  
  }

  if (btn == BUTTON_1_SHORT_RELEASE )
  {
    counting++;
    if ( counting > 15 )
    {
      counting = 15; 
    }
    MFS.write(counting);
  }
  
  if (btn == BUTTON_2_SHORT_RELEASE )
  {
    counting--;
    if ( counting < 0 )
    {
      counting = 0; 
    }
    MFS.write(counting);
  }

  if (btn == BUTTON_3_SHORT_RELEASE )
  {
 // Check click numbers
    if (counting == number ) {
      HighScore++;
      MFS.write(HighScore) ; 
      while (!MFS.getButton()) {
      }

      reset();
    } else {
      MFS.write("LOSE") ;
      while (!MFS.getButton()) {
      }
      HighScore = 0;
      reset();
    }
  }
}

If you want a different series of random numbers every time you restart, you need to seed the random number generator with some value - typically reading analog0 with nothing attached. Look at the docs for randomSeed()

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.