我的GAMEBOY自己做_搖桿控制8X8LED上的光點

 #include <LedControl.h>


LedControl lc = LedControl(5, 7, 6, 1); // DIN=5, CLK=7, CS=6


int x = 3; // 初始位置

int y = 3;


void setup() {

  lc.shutdown(0, false);

  lc.setIntensity(0, 8);

  lc.clearDisplay(0);


  pinMode(A0, INPUT); // 搖桿 X 軸

  pinMode(A1, INPUT); // 搖桿 Y 軸

  pinMode(2, INPUT_PULLUP); // 搖桿 SW 按鈕

}


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);


  // SW 按下 → 回到原點

  if (sw == LOW) {

    x = 0;

    y = 0;

  }


  // 顯示光點

  lc.clearDisplay(0);

  lc.setLed(0, y, x, true);


  delay(150); // 控制移動速度

}





留言