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("--------------------------");
}]