#include <LedControl.h>
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 2
LedControl lc(11,12,10,4);
int place_pre[2] = {0,0};
int place_now[2] = {0,0};
int nutRAW = 0,nutCOL = 0;
void nut(){
nutRAW = random(8);
nutCOL = random(16);
while (place_now[0] == nutRAW && place_now[1] == nutCOL)
{
nutRAW = random(8);
nutCOL = random(16);
}
lc.setLed(nutCOL/8,nutRAW,nutCOL%8,1);
lc.setLed(nutCOL/8+2,nutRAW,nutCOL%8,1);
}
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
randomSeed(analogRead(0));
lc.setLed(0,place_now[0],place_now[1],1);
lc.setLed(2,place_now[0],place_now[1],1);
attachInterrupt(digitalPinToInterrupt(SEL_PIN), nut , FALLING);
}
void loop() {
int vert = analogRead(VERT_PIN); //讀取A0的資訊 按下回傳0 按上回傳1023
int horz = analogRead(HORZ_PIN); //讀取A1的資訊 按左回傳0 按右回傳1023
bool selPressed = digitalRead(SEL_PIN) == LOW;
if (vert > 700 && place_now[0] > 0) // 向上
place_now[0]--;
else if (vert < 300 && place_now[0] < 7) //向下
place_now[0]++;
if (horz > 700 && place_now[1] < 15) //向右
place_now[1]++;
else if (horz < 300 && place_now[1] > 0) //向左
place_now[1]--;
if (place_pre[0] != place_now[0] || place_pre[1] != place_now[1])
{
lc.setLed(place_pre[1]/8,place_pre[0],place_pre[1]%8,0);
lc.setLed(place_pre[1]/8+2,place_pre[0],place_pre[1]%8,0);
place_pre[0] = place_now[0];
place_pre[1] = place_now[1];
}
lc.setLed(place_now[1]/8,place_now[0],place_now[1]%8,1);
lc.setLed(place_now[1]/8+2,place_now[0],place_now[1]%8,1);
delay(100);
}