Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s
- byte sequence[100]; // Storage for the light sequence
- byte curLen = 0; // Current length of the sequence
- int score = 0; // Score counter
- byte inputCount = 0; // The number of times that the player has pressed a (correct) button in a given turn
- byte lastInput = 0; // Last input from the player
- byte expRd = 0; // The LED that's suppose to be lit by the player
- bool btnDwn = false; // Check if a button is pressed
- bool wait = false; // Waiting for the user to press a button
- bool resetFlag = false; // Used to indicate to the program that once the player lost
- // Arduino joystick pin numbers
- const int SW_pin = 0; // digital pin connected to switch output
- const int X_pin = 2; // analog pin connected to X output
- const int Y_pin = 3; // analog pin connected to Y output
- byte noPins = 4; // Number of buttons/LEDs
- byte pins[] = {2, 3, 5, 6}; // Button input pins and LED output pins - change these values if you want to connect your buttons to other pins
- // The number of elements must match noPins below
- long inputTime = 0; // Timer variable for the delay between user inputs
- void setup() {
- Serial.begin(9600); // Start Serial monitor. This can be removed too as long as you remove all references to Serial below
- //joystick reading
- pinMode(SW_pin, INPUT);
- digitalWrite(SW_pin, HIGH);
- //LDR setup
- pinMode(A5, INPUT);
- pinMode(5, OUTPUT);
- Reset();
- // define pins for score display
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- pinMode(10, OUTPUT);
- pinMode(11, OUTPUT);
- pinMode(12, OUTPUT);
- pinMode(13, OUTPUT);
- }
- /// Sets all the pins as either INPUT or OUTPUT based on the value of 'dir'
- void setPinDirection(byte dir) {
- for (byte i = 0; i < noPins; i++) {
- pinMode(pins[i], dir);
- }
- }
- //send the same value to all the LED pins
- void writeAllPins(byte val) {
- for (byte i = 0; i < noPins; i++) {
- digitalWrite(pins[i], val);
- }
- }
- /// Flashes all the LEDs together
- /// freq is the blink speed - small number -> fast | big number -> slow
- void flash(short freq) {
- setPinDirection(OUTPUT); /// We're activating the LEDS now
- for (int i = 0; i < 5; i++) {
- writeAllPins(HIGH);
- delay(freq);
- writeAllPins(LOW);
- delay(freq);
- }
- }
- ///This function resets all the game variables to their default values
- void Reset() {
- flash(500);
- curLen = 0;
- inputCount = 0;
- lastInput = 0;
- expRd = 0;
- score = 0;
- digitalWrite(7, LOW);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- btnDwn = false;
- wait = false;
- resetFlag = false;
- }
- /// User lost
- void Lose() {
- flash(50);
- }
- /// The arduino shows the user what must be memorized
- /// Also called after losing to show you what you last sequence was
- void playSequence() {
- //Loop through the stored sequence and light the appropriate LEDs in turn
- for (int i = 0; i < curLen; i++) {
- Serial.print("Seq: ");
- Serial.print(i);
- Serial.print("Pin: ");
- Serial.println(sequence[i]);
- digitalWrite(sequence[i], HIGH);
- delay(500);
- digitalWrite(sequence[i], LOW);
- delay(250);
- }
- }
- /// The events that occur upon a loss
- void DoLoseProcess() {
- Lose(); // Flash all the LEDS quickly (see Lose function)
- delay(1000);
- playSequence(); // Shows the user the last sequence - So you can count remember your best score - Mine's 22 by the way :)
- delay(1000);
- Reset(); // Reset everything for a new game
- }
- ///
- /// Game Logic
- ///
- void loop() {
- if (!wait) {
- //****************//
- // Arduino's turn //
- //****************//
- setPinDirection(OUTPUT); // We're using the LEDs
- randomSeed(analogRead(A0)); // https://www.arduino.cc/en/Reference/RandomSeed
- sequence[curLen] = pins[random(0, noPins)]; // Put a new random value in the next position in the sequence - https://www.arduino.cc/en/Reference/random
- curLen++; // Set the new Current length of the sequence
- playSequence(); // Show the sequence to the player
- wait = true; // Set Wait to true as it's now going to be the turn of the player
- inputTime = millis(); // Store the time to measure the player's response time
- } else {
- //***************//
- // Player's turn //
- //***************//
- setPinDirection(INPUT); // We're using the buttons
- //joystick readings
- //LDR reading to pin 2, the green LED in my setup
- if (analogRead(X_pin) < 400) { //pushing up
- digitalWrite(2, HIGH);
- }
- if (analogRead(X_pin) > 400) { //release up
- digitalWrite(2, LOW);
- }
- //LDR reading to pin 3, the blue LED in my setup
- int val = analogRead(A5);
- Serial.println(val);
- if (val < 950) {
- digitalWrite(3, HIGH);
- }
- if (val > 950){
- digitalWrite(3, LOW);
- }
- if (millis() - inputTime > PLAYER_WAIT_TIME) { // If the player takes more than the allowed time,
- DoLoseProcess(); // Lose process
- return;
- }
- if (!btnDwn) { //
- expRd = sequence[inputCount]; // Find the value we expect from the player
- Serial.print("Expected: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- Serial.println(expRd); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- for (int i = 0; i < noPins; i++) { // Loop through the all the pins
- if (pins[i] == expRd)
- continue; // Ignore the correct pin
- if (digitalRead(pins[i]) == HIGH) { // Is the button pressed
- lastInput = pins[i];
- resetFlag = true; // Set the resetFlag - this means you lost
- btnDwn = true; // This will prevent the program from doing the same thing over and over again
- Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- }
- }
- }
- if (digitalRead(expRd) == 1 && !btnDwn) // The player pressed the right button
- {
- inputTime = millis(); //
- lastInput = expRd;
- inputCount++; // The user pressed a (correct) button again
- btnDwn = true; // This will prevent the program from doing the same thing over and over again
- Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above
- } else {
- if (btnDwn && digitalRead(lastInput) == LOW) { // Check if the player released the button
- btnDwn = false;
- delay(20);
- if (resetFlag) { // If this was set to true up above, you lost
- DoLoseProcess(); // So we do the losing sequence of events
- }
- else {
- if (inputCount == curLen) { // Has the player finished repeating the sequence
- wait = false; // If so, this will make the next turn the program's turn
- inputCount = 0; // Reset the number of times that the player has pressed a button
- score++;
- //7 segment display for score 1-9
- if (score == 0) {
- digitalWrite(7, LOW);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (score == 1) {
- digitalWrite(7, LOW);
- digitalWrite(8, LOW);
- digitalWrite(9, LOW);
- digitalWrite(10, HIGH);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, HIGH);
- }
- else if (score == 2) {
- digitalWrite(7, HIGH);
- digitalWrite(8, LOW);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- digitalWrite(13, LOW);
- }
- else if (score == 3) {
- digitalWrite(7, HIGH);
- digitalWrite(8, LOW);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, LOW);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (score == 4) {
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, LOW);
- digitalWrite(10, HIGH);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, HIGH);
- }
- else if (score == 5) {
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, LOW);
- digitalWrite(11, LOW);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (score == 6) {
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, LOW);
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (score == 7) {
- digitalWrite(7, LOW);
- digitalWrite(8, LOW);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, HIGH);
- }
- else if (score == 8) {
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, HIGH);
- digitalWrite(12, HIGH);
- digitalWrite(13, HIGH);
- }
- else if (score == 9) {
- digitalWrite(7, HIGH);
- digitalWrite(8, HIGH);
- digitalWrite(9, HIGH);
- digitalWrite(10, HIGH);
- digitalWrite(11, LOW);
- digitalWrite(12, LOW);
- digitalWrite(13, HIGH);
- }
- delay(1500);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement