Display variabel on LED/LCD display on the Multi Function Shield

Dear sir/madam,

I’m experiencing an problem regarding displaying the right amount of button pressed, on the LCD/LED display of my Multi Function Shield. What my program has to do is that it is an number guessing game. What I mean is that whenever you see LED’s burning, you have to guess the right number and press the push-button the correct amount of times. An example. My code generates the number 10. I have to press the push-button 10 times and if I do that then I win. If I do not press the button within 2 seconds I lose. I have to guess the generated number without the serial monitor. So what I’m trouble with is that whenever the random number reaches 10 or above, it has to show the hexadecimal character a all the way till f.

What I tried doing is experimenting on my code and tried to put in variables that seem logical to me to put in MFS.write (MFS.write is the piece of code you use to let the LCD/LED display show something). I also searched Google for awhile but the only I found were codes that were really hard to understand (I’m an beginner).

I’m asking if any of you could assist me with tips or possible solutions. I put my code below for anyone who is interested in helping me.

[#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


//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;

long startTime;
byte hasError;

void setup() {
  Timer1.initialize();
  MFS.initialize(&Timer1);    // initialiseert the Multi Function Display en de Timer1 bibliotheek
  Serial.begin(9600);        // initialiseert de seriële monitor
  randomSeed(analogRead(0));
  number = random(1, 15); // genereert willekeurige cijfers van 1 tot en met 15

  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 loop() {

  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW && prevButtonState == HIGH) {

    prevButtonState = HIGH;

    // Generate new random number
    if (counting == 0) {
      hasError = 0;
      startTime = millis();
      number = random(1, 15);
      printNumber(number);
      displayBinary(number);
    }

    // Check if 2 seconds have passed between two clicks.
    if (millis() - startTime > 2000) {
      Serial.println("Error");
      hasError = 1;
    }

    // Check click numbers
    if (counting == number - 1) {
      Serial.println("Success");
      counting = 0;
      delay(100);
    }
    else {
      if (hasError == 0) {
        counting++;
        startTime = millis();
        Serial.println(counting);
        String stringOne =  String(counting, HEX);
        Serial.println("hexadecimaal = " + stringOne);
        MFS.write(counting);
        delay(100);
      }
      else {
        counting = 0;
      }
    }
  }

  // Set previous button state so we can count clicks and not state changes.
  if (buttonState == HIGH && prevButtonState == LOW) {
    prevButtonState = HIGH;
  }
}
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 convertToBinary(byte number, int binary[4]) {

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

}

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

So what I’m trouble with is that whenever the random number reaches 10 or above, it has to show the hexadecimal character a all the way till f.

What does? And why?

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

Is the code right? Or the useless comments?

  number = random(1, 15); // genereert willekeurige cijfers van 1 tot en met 15

The value returned by random() will be between 1 and 14, not 1 and 15.

Hi Paul,

What do you mean by this?

PaulS:
What does? And why?

PaulS:
Is the code right? Or the useless comments?

The code is right. I'm declaring LED's on the Multi Function Shield to represent binary numbers. The code may not be right for that, but that's something I want to fix on my own should that be the case.

PaulS:
The value returned by random() will be between 1 and 14, not 1 and 15.

Thanks for noticing the flaw in the randNumber I will change it ASAP.

Can you please provide a link the the shield that you are using ?

UKHeliBob:
Can you please provide a link the the shield that you are using ?

Sure. I could not find an link of the manufacturer but I found one on an website.

Link:

The pictures called Gambar 1 and 2 is exactly how mine MFS (Multi Function Shield) looks like.

I am confused as to whether you are trying to display something on one or more of the LEDs or on the 4 digit 7 segment display which, to confuse thins, also uses LEDs

UKHeliBob:
I am confused as to whether you are trying to display something on one or more of the LEDs or on the 4 digit 7 segment display which, to confuse thins, also uses LEDs

I'm trying to display just the right amount of LED's needed to show hexadecimal characters on the 4 digit 7 segment display on the MFS. For example. If I press the button 11 times then the display should show b instead of the decimal number 11.

Start with this example and add more entries to the SEGMENT_MAP array

/* FILE:    ARD_Multifunction_Shield_Seven_Segment_Example
   DATE:    28/05/14
   VERSION: 0.1
   
REVISIONS:

28/05/14 Created version 0.1

This is an example of how to use the 4 digit seven segment display on the Hobby 
Components Arduino compatible Multi Function experimenter shield (HCARDU0085).

This example sketch will display a decimal number on the seven segment LED display 
which will increment by one wever 100ms.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products 
that directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* 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

void setup ()
{
  /* Set DIO pins to outputs */
  pinMode(LATCH_DIO,OUTPUT);
  pinMode(CLK_DIO,OUTPUT);
  pinMode(DATA_DIO,OUTPUT); 
  
  /* Initiliase the registers used to store the crrent time and count */
  Cur_ms_Count = millis();
  Last_ms_Count = 0;
  Count = 0;
}

/* Main program */
void loop()
{
 
  /* Get how much time has passed in milliseconds */
  Cur_ms_Count = millis();
  
  /* If 1000ms has passed then add one to the counter */
  if(Cur_ms_Count - Last_ms_Count >= 1000)
  {

    Last_ms_Count = Cur_ms_Count;
    
    if(Count < 9999)
    {
      Count++;
    } else
    {
      Count = 0;
    }
  }
  
  /* Update the display with the current counter value */
  WriteNumber(Count);
}


/* Write a decimal number between 0 and 9999 to the display */
void WriteNumber(int Number)
{
  WriteNumberToDigit(0 , Number / 1000);
  WriteNumberToDigit(1 , (Number / 100) % 10);
  WriteNumberToDigit(2 , (Number / 10) % 10);
  WriteNumberToDigit(3 , Number % 10);
}

/* Write a decimal number between 0 and 9 to one of the 4 digits of the display */
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);    
}