#include "LedControl.h"

#define data 12
#define clock 11
#define  chipSelect 10
#define matrixNumber  1
LedControl lc = LedControl(data, clock, chipSelect, matrixNumber);

const byte horizontalPin = A0; // x
const byte verticalPin = A1; // y
const byte selectPin =  2; // pushbutton

int horizontalReading, verticalReading, selectReading, x = 0, y = 0, x1, y1, DEADBAND = 10;

void setup() {
  Serial.begin(115200);

  pinMode(horizontalPin, INPUT);
  pinMode(verticalPin, INPUT);
  pinMode(selectPin, INPUT_PULLUP);

  lc.clearDisplay(0);// clear screen
  delay(1000); // wait for no good reason
}

void loop() {
  horizontalReading = analogRead(horizontalPin);
  verticalReading = analogRead(verticalPin);
  selectReading  = digitalRead(selectPin);
  delay(100); // debounce the readings - find a better way to debounce

  if (horizontalReading < 512 - DEADBAND) // joystick right
    x += 1;
  if (horizontalReading > 512 + DEADBAND) // joystick left
    x -= 1;
  if (horizontalReading > 512 - DEADBAND && horizontalReading < 512 + DEADBAND) // stationary
    x += 0;

  if (verticalReading < 512  - DEADBAND) // joystick up
    y += 1;
  if (verticalReading > 512 + DEADBAND) // joystick down
    y -= 1;
  if (verticalReading > 512 - DEADBAND && verticalReading < 512 + DEADBAND) // stationary
    x += 0;

  if (x > 7) // right border
    x = 7;
  if (x < 0) // left border
    x = 0;
  if (y > 7) // top border
    y = 7;
  if (y < 0) // bottom border
    y = 0;

  lc.setLed(0, x, y, 1); // light the LED

  if (!selectReading) // select button
    lc.clearDisplay(0); // clear display
}