// potentiometer
const int pot = A5;
// current level
int result = 1;
int buttonPressedCounter = 1;
int currLevel = map(analogRead(pot), 0, 1023, 1, 9);
int lastCurrLevel = currLevel;
// leds array
int Leds[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
// shift register
const int dataPin = 12; // DS of 74HC595
const int latchPin = 11; // STCP of 74HC595
const int clockPin = 10; // SHCP of 74HC595
// buttons
const int buttons[4] = {A0, A1, A2, A3};
const int leds[4] = {9, 8, 7, 6};
const int tones[] = {262, 330, 392, 440};
// piezo
const int piezo = 5;
// Byte patterns for numbers 0-9 on a common cathode seven-segment display
const byte numbers[10] = {
0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000 // 9
};
void setup() {
randomSeed(analogRead(A4));
Serial.begin(9600);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(pot, INPUT);
// setup buttons and leds
for(int i = 0; i< 4; ++i){
pinMode(buttons[i], INPUT_PULLUP);
pinMode(leds[i], OUTPUT);
}
}
bool hasPotValueChanged() {
currLevel = map(analogRead(pot), 0, 1023, 1, 9);
// Check if the potentiometer value has changed significantly
if (currLevel != lastCurrLevel) {
lastCurrLevel = currLevel; // Update the last known value
return true;
}
return false;
}
void loop() {
// display current level in the seven segment display
// if (hasPotValueChanged()) {
// return;
// }
displayDigit(currLevel);
turnOnLeds();
buttonPressedCounter = 1;
// Wait for the player to input the sequence
bool sequenceCorrect = waitForSequence();
if (sequenceCorrect) {
Serial.println("tocno");
// If the sequence is correct, increment the level and move on to the next round.
currLevel++;
}
// Some delay before the next round starts.
delay(1000);
}
void turnOnLeds(){
// display random led currLevel number of times
for(int i = 0; i < currLevel; ++i){
// current led
int currLed = random(4);
Leds[i] = currLed; // index of led
digitalWrite(leds[currLed], HIGH); // led value ( pin )
tone(piezo, tones[i], 600);
Serial.println(currLed);
delay(1000);
digitalWrite(leds[currLed], LOW); // led value ( pin )
}
}
void displayDigit(int digit) {
// Ensure the digit is within range.
if (digit < 0 || digit > 9) return;
// Prepare to shift out the data.
digitalWrite(latchPin, LOW);
// Shift out the data for the current digit.
shiftOut(dataPin, clockPin, MSBFIRST, numbers[digit]);
// Latch the data to update the display.
digitalWrite(latchPin, HIGH);
}
void gameOver(){
// display on all leds for a second
for(int i = 0; i < 4; ++i){
digitalWrite(leds[i], HIGH);
}
delay(2000);
for(int i = 0; i < 4; ++i){
digitalWrite(leds[i], LOW);
}
currLevel = 4;
}
// Call this function to wait for the entire sequence of button presses for the current level.
bool waitForSequence() {
for (int i = 0; i < currLevel; ++i) {
int buttonIndex = waitForButtonPress();
// If the pressed button is not the expected one, return false.
if (buttonIndex != Leds[i]) {
return false;
}
}
// If all button presses match the sequence, return true.
return true;
}
// This function remains the same as before.
int waitForButtonPress() {
while (true) {
for (int i = 0; i < 4; ++i) {
if (digitalRead(buttons[i]) == LOW) {
delay(500); // debounce
while (digitalRead(buttons[i]) == LOW); // Wait for release
return i; // Return the button index
}
}
}
}