#include <LedControl.h>
LedControl lc = LedControl(5, 7, 6, 1);
int x = 3, y = 3;
void setup() {
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(2, INPUT_PULLUP);
}
void loop() {
int joyX = analogRead(A0);
int joyY = analogRead(A1);
int sw = digitalRead(2);
if (joyX < 400) x--;
else if (joyX > 600) x++;
if (joyY < 400) y++;
else if (joyY > 600) y--;
x = constrain(x, 0, 7);
y = constrain(y, 0, 7);
if (sw == LOW) {
x = 0;
y = 0;
}
lc.clearDisplay(0);
// 中心點
lc.setLed(0, y, x, true);
// 上下左右
if (y > 0) lc.setLed(0, y-1, x, true); // 上
if (y < 7) lc.setLed(0, y+1, x, true); // 下
if (x > 0) lc.setLed(0, y, x-1, true); // 左
if (x < 7) lc.setLed(0, y, x+1, true); // 右
delay(150);
}
留言
張貼留言