冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 557|回覆: 0
打印 上一主題 下一主題

[開源] Arduino遊戲機

[複製鏈接]

2609

主題

0

好友

945

積分

高級會員

Rank: 4

UID
373967
帖子
7378
主題
2609
精華
0
積分
945
楓幣
914
威望
925
存款
26000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
405 小時
註冊時間
2023-1-12
最後登入
2024-4-27

2023端午節紀念勳章 2023中秋節紀念勳章 2023聖誕節紀念勳章

跳轉到指定樓層
1
發表於 2023-3-30 06:04:00 |只看該作者 |倒序瀏覽
Arduino遊戲機是一個非常有趣的 DIY 電子項目,以下是一些基本的零件、連接腳位和程式碼,您可以參考這些來建立自己的遊戲機。

零件:

Arduino UNO 開發板
OLED 顯示屏
搖桿模塊
蜂鳴器
按鈕
連接腳位:

OLED 顯示屏:

SDA:A4
SCL:A5
搖桿模塊:

VRx:A0
VRy:A1
SW:D2
蜂鳴器:D3

按鈕:

按鈕 1:D4
按鈕 2:D5
程式碼:

以下是一個基本的程式碼框架,可以幫助您開始建立自己的遊戲機。


#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

int x_axis = A0;
int y_axis = A1;
int button = 2;
int buzzer = 3;
int button1 = 4;
int button2 = 5;

int toneVal = 2000; // 音調的頻率
int durationVal = 100; // 蜂鳴器的持續時間

void setup() {
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 開啟 OLED 顯示屏
  display.clearDisplay(); // 清除顯示屏
  display.display(); // 更新顯示屏
}

void loop() {
  int x = analogRead(x_axis); // 讀取 x 軸的值
  int y = analogRead(y_axis); // 讀取 y 軸的值
  int btn = digitalRead(button); // 讀取按鈕的值

  if(btn == LOW){
    tone(buzzer, toneVal, durationVal); // 播放音效
    delay(100); // 延遲一段時間,防止重複觸發
  }

  if(digitalRead(button1) == LOW){ // 按鈕 1 按下
    display.clearDisplay(); // 清除顯示屏
    display.setCursor(0,0);
    display.println("Button 1 Pressed"); // 顯示按下的按鈕編號
    display.display(); // 更新顯示屏
  }

  if(digitalRead(button2) == LOW){ // 按鈕 2 按下
    display.clearDisplay(); // 清除

display.setCursor(0,0);
display.println("Button 2 Pressed"); // 顯示按下的按鈕編號
display.display(); // 更新顯示屏
}

// 控制搖桿方向
if(x < 100){ // 左
display.clearDisplay(); // 清除顯示屏
display.setCursor(0,0);
display.println("Left"); // 顯示方向
display.display(); // 更新顯示屏
}
else if(x > 900){ // 右
display.clearDisplay(); // 清除顯示屏
display.setCursor(0,0);
display.println("Right"); // 顯示方向
display.display(); // 更新顯示屏
}

if(y < 100){ // 上
display.clearDisplay(); // 清除顯示屏
display.setCursor(0,0);
display.println("Up"); // 顯示方向
display.display(); // 更新顯示屏
}
else if(y > 900){ // 下
display.clearDisplay(); // 清除顯示屏
display.setCursor(0,0);
display.println("Down"); // 顯示方向
display.display(); // 更新顯示屏
}
}
[發帖際遇]: 洪嵐峰 投入「普悠瑪脫軌」救災,行為值得讚賞,獲得 2 楓幣 幸運榜 / 衰神榜
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-28 02:31

回頂部