Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Krishna Lalith
Published © GPL3+

Lights Out Game

Turn on all the LEDs using a 4x4 keypad with only constraint that the apart from selected LED its neighbors also flips its state.

BeginnerShowcase (no instructions)2 hours935
Lights Out Game

Things used in this project

Hardware components

Webduino Fly + Arduino UNO
Webduino Fly + Arduino UNO
×1
Button Pad 4x4 - LED Compatible
SparkFun Button Pad 4x4 - LED Compatible
×1
LED Dot Matrix Display, Red
LED Dot Matrix Display, Red
×1

Story

Read more

Schematics

Lights Out

Code

Lights Out Game

Arduino
#include <Keypad.h>                                                           //keypad library
#include <LedControl.h>

const byte ROWS = 4; //four rows         ;                                    //declare rows
const byte COLS = 4; //four columns                                           //declare columns
char keys[ROWS][COLS] = {                                                     //declare 2 dimensional array to map keys
  {'M', 'N', 'O', 'P'},
  {'I', 'J', 'K', 'L'},
  {'E', 'F', 'G', 'H'},
  {'A', 'B', 'C', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};                                            //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9};                                            //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );     //create instance of Keypad called keypad


int DIN = 12;
int CS =  11;
int CLK = 10;
LedControl lc = LedControl(DIN, CLK, CS, 0);

int FIVER[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int r, c, i, j;
int flag = 1;


void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false);      //The MAX72XX is in power-saving mode on startup
  lc.setIntensity(0, 1);      // Set the brightness to maximum value
  lc.clearDisplay(0);         // and clear the display
  randomSeed(analogRead(0));
}

void loop() {
  if (flag == 1) {
    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        FIVER[i][j] = random(0, 2);
      }
    }
    
    flag = 0;
    
    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        if (FIVER[i][j] == 1) {
          lc.setLed(0, 2 * i, 2 * j , true);
          lc.setLed(0, 2 * i + 1, 2 * j , true);
          lc.setLed(0, 2 * i, 2 * j + 1 , true);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , true);
        }
        if (FIVER[i][j] == 0) {
          lc.setLed(0, 2 * i, 2 * j , false);
          lc.setLed(0, 2 * i + 1, 2 * j , false);
          lc.setLed(0, 2 * i, 2 * j + 1 , false);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , false);
        }
      }
    }
  }

  char key = keypad.getKey();                                                 //use keypad.getKey function to
  //to define the value of Char variable key

  if (key == 'A') {
    r = 0;
    c = 0;
  }
  if (key == 'B') {
    r = 0;
    c = 1;
  }
  if (key == 'C') {
    r = 0;
    c = 2;
  }
  if (key == 'D') {
    r = 0;
    c = 3;
  }
  if (key == 'E') {
    r = 1;
    c = 0;
  }
  if (key == 'F') {
    r = 1;
    c = 1;
  }
  if (key == 'G') {
    r = 1;
    c = 2;
  }
  if (key == 'H') {
    r = 1;
    c = 3;
  }
  if (key == 'I') {
    r = 2;
    c = 0;
  }
  if (key == 'J') {
    r = 2;
    c = 1;
  }
  if (key == 'K') {
    r = 2;
    c = 2;
  }
  if (key == 'L') {
    r = 2;
    c = 3;
  }
  if (key == 'M') {
    r = 3;
    c = 0;
  }
  if (key == 'N') {
    r = 3;
    c = 1;
  }
  if (key == 'O') {
    r = 3;
    c = 2;
  }
  if (key == 'P') {
    r = 3;
    c = 3;
  }

  if (key) {
    FIVER[r][c] = 1 - FIVER[r][c];
    if (r > 0) {
      FIVER[r - 1][c] = 1 - FIVER[r - 1][c];
    }
    if (c > 0) {
      FIVER[r][c - 1] = 1 - FIVER[r][c - 1];
    }
    if (r < 3) {
      FIVER[r + 1][c] = 1 - FIVER[r + 1][c];
    }
    if (c < 3) {
      FIVER[r][c + 1] = 1 - FIVER[r][c + 1];
    }

    for (i = 0; i < 4; i++) {
      for (j = 0; j < 4; j++) {
        if (FIVER[i][j] == 1) {
          lc.setLed(0, 2 * i, 2 * j , true);
          lc.setLed(0, 2 * i + 1, 2 * j , true);
          lc.setLed(0, 2 * i, 2 * j + 1 , true);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , true);
        }
        if (FIVER[i][j] == 0) {
          lc.setLed(0, 2 * i, 2 * j , false);
          lc.setLed(0, 2 * i + 1, 2 * j , false);
          lc.setLed(0, 2 * i, 2 * j + 1 , false);
          lc.setLed(0, 2 * i + 1, 2 * j + 1 , false);
        }
      }
    }
  }
}

Credits

Krishna Lalith

Krishna Lalith

11 projects • 2 followers

Comments