/**
Simon Game for Arduino with Score display
Copyright (C) 2022, Uri Shaked
Released under the MIT License.
*/
#include "pitches.h"
/* Constants - define pin numbers for LEDs,
buttons and speaker, and also the game tones: */
const uint8_t ledPins[] = {9, 8, 7, 6}; //JVRB
const uint8_t buttonPins[] = {A3, A2, A1, A0}; //JVRB
#define SPEAKER_PIN 3
// These are connected to 74HC595 shift register (used to show game score):
const int LATCH_PIN = 11; // 74HC595 pin 12 RCK
const int DATA_PIN = 10; // 74HC595pin 14 SER
const int CLOCK_PIN = 12; // 74HC595 pin 11 SCK
#define MAX_GAME_LENGTH 100
const int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
const int CMD_DIG2 = 5;
const int CMD_DIG1 = 4;
/* Global variables - store the game state */
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
/**
Set up the Arduino board and initialize Serial communication
*/
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(CMD_DIG1, OUTPUT);
pinMode(CMD_DIG2, OUTPUT);
// The following line primes the random number generator.
// It assumes pin A3 is floating (disconnected):
randomSeed(analogRead(A4));
}
/* Digit table for the 7-segment display */
const uint8_t digitTable[] = {
0b00111111,
0b00000110,
0b01011011,
0b01001111,
0b01100110,
0b01101101,
0b01111101,
0b00000111,
0b01111111,
0b01101111,
};
const uint8_t DASH = 0b01000000;
unsigned long int TpDis = 0;
bool dig2ToDisplay = 0;
void display2Digits(uint8_t dig1, uint8_t dig2)
{
unsigned long tc = millis();
if(tc-TpDis>40)
{
if(dig2ToDisplay)
{
digitalWrite(CMD_DIG1, HIGH);
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, dig2);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(CMD_DIG2, LOW);
}
else
{
digitalWrite(CMD_DIG2, HIGH);
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, dig1);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(CMD_DIG1, LOW);
}
TpDis = millis();
dig2ToDisplay = !dig2ToDisplay;
}
}
/*void displayScore() {
int high = gameIndex % 100 / 10;
int low = gameIndex % 10;
display2Digits(high ? digitTable[high] : 0xff, digitTable[low]);
}*/
void loop()
{
display2Digits(digitTable[4], digitTable[7-2]);
for (uint64_t i = 0 ; i <100000 ; i++)
{
uint64_t j=i/10000;
display2Digits(digitTable[j], digitTable[9-j]);
}
}