IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> Arduino Pro Micro 实现游戏手柄Joysticks -> 正文阅读

[嵌入式]Arduino Pro Micro 实现游戏手柄Joysticks

概述

国内网络上关于手柄制作的资料感觉特别的少,最近参加校电赛,其中一个题目是运用单片机制作游戏手柄,所以我打算用atmega32u4单片机运用arduino的ide来实现这些功能。下面的参考链接是我能找到的最符合要求的项目,但是这其中有很多的问题,我先把实现的流程和大家说,出现的问题我留在文章末尾。

参考链接

实现过程

用到的元件:

Arduino Pro Micro * 1
5个引脚的PS2游戏手柄 * 2
轻触按键 * 16
5mm绿色LED * 1
5mm蓝色LED * 1
220Ω电阻 * 2

Arduino IDE的配置

点击Tools > the Board selection tab > Boards Manager.

project-image

找到SparkFun AVR Boards package并下载

请添加图片描述

选择Sparkfun Pro Micro

project-image

在arduino官网下载Keypad库并安装

https://www.arduino.cc/reference/en/libraries/keypad/

引脚连接方式:

//                                左摇杆
// A0  --------------------------- VRX
// A1  --------------------------- VRY
// D14 --------------------------- SW
// 5V  --------------------------- 5V
// GND --------------------------- GND
//                                右摇杆
// A2  --------------------------- VRX
// A3  --------------------------- VRY
// D16 --------------------------- SW
// 5V  --------------------------- 5V
// GND --------------------------- GND
//                                4X4矩阵键盘
// D2  --------------------------- C1
// D3  --------------------------- C2
// D4  --------------------------- C3
// D5  --------------------------- C4
// D6  --------------------------- R1
// D7  --------------------------- R2
// D8  --------------------------- R3
// D9  --------------------------- R4
//                                字符键盘指示灯
// D15 --------------------------- +
//                                数字键盘指示灯
// D10 --------------------------- +

矩阵键盘的连接方式

请添加图片描述

实现的代码

#include <Keypad.h>
#include "Keyboard.h"
#include "Mouse.h"

const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns

// Define the symbols on the buttons of the keypads.
char letterKeys[ROWS][COLS] = {
  {'e','a','r','i'},
  {'o','t','n','s'},
  {'p','m','h','w'},
  {'l','c','u','d'}
};

char numberKeys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'#','0','*','%'},
  {'7','8','9','/'}

};

byte rowPins[ROWS] = {6, 7, 8, 9}; // Connect to the row pinouts of the keypad.
byte colPins[COLS] = {2, 3, 4, 5}; // Connect to the column pinouts of the keypad.

// Initialize an instance of class NewKeypad for each keypad setting - letter and number.
Keypad letterKeypad = Keypad( makeKeymap(letterKeys), rowPins, colPins, ROWS, COLS);
Keypad numberKeypad = Keypad( makeKeymap(numberKeys), rowPins, colPins, ROWS, COLS);

// Define pinouts of joysticks.
#define VRX_L A0
#define VRY_L A1
#define SW_L 14
#define VRX_R A2
#define VRY_R A3
#define SW_R 16

// Define keypad LED pins:
#define let_LED 15
#define num_LED 10

// Define dynamic keypad options - letter and number.
volatile boolean letter = true;
volatile boolean number = false; 

// Define the data holders:
int joystick_left_x, joystick_left_y, joystick_left_button, joystick_right_x, joystick_right_y, joystick_right_button;

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

  // Initialize mouse and keyboard control:
  Mouse.begin();
  Keyboard.begin();

  // Joystick button settings:
  pinMode(SW_L, INPUT);
  digitalWrite(SW_L, HIGH);
  pinMode(SW_R, INPUT);
  digitalWrite(SW_R, HIGH);

  // Keypad LEDs:
  pinMode(let_LED, OUTPUT);
  digitalWrite(let_LED, HIGH);
  pinMode(num_LED, OUTPUT);
  digitalWrite(num_LED, LOW);
}

void loop() {
  // Read joystick positions:
  read_joysticks();

  // Mouse:
  mouse_controls();

  // Keyboard:
  keyboard_controls();

}

void read_joysticks(){
  joystick_left_x = analogRead(VRX_L); 
  joystick_left_y = analogRead(VRY_L);
  joystick_left_button = digitalRead(SW_L);
  
  joystick_right_x = analogRead(VRX_R); 
  joystick_right_y = analogRead(VRY_R);  
  joystick_right_button = digitalRead(SW_R);

  /*
  Serial.print(joystick_right_x);
  Serial.print("\t");
  Serial.print(joystick_right_y);
  Serial.print("\t\t");
  Serial.print(joystick_right_button);
  Serial.print("\n\n");
  */
}

void mouse_controls(){
  // Move mouse according to the left joystick movements.
  if(joystick_left_y > 700)  Mouse.move(0, -15); // UP
  if(joystick_left_y < 300)  Mouse.move(0, 15);  // DOWN
  if(joystick_left_x > 700)  Mouse.move(-15, 0); // LEFT
  if(joystick_left_x < 300)  Mouse.move(15, 0);  // RIGHT
  if(joystick_left_button == 0) Mouse.click(MOUSE_LEFT); // LEFT CLICK
  if(joystick_right_button == 0) Mouse.click(MOUSE_RIGHT); // RIGHT CLICK
  delay(100);
}

void keyboard_controls(){
  // Change keypad settings (letter or number) and press modifier keys according to the right joystick movements.
  if(joystick_right_y > 700){ Keyboard.press(KEY_RETURN); delay(100); Keyboard.releaseAll(); }    // RETURN
  if(joystick_right_y < 300){ Keyboard.press(KEY_BACKSPACE); delay(100); Keyboard.releaseAll(); } // BACKSPACE
  if(joystick_right_x > 700){ letter = true; number = false; digitalWrite(let_LED, HIGH); digitalWrite(num_LED, LOW); } // Letter Keypad
  if(joystick_right_x < 300){ letter = false; number = true; digitalWrite(let_LED, LOW); digitalWrite(num_LED, HIGH); } // Number Keypad
  
  // Get the custom key depending on the selected keypad type - letter or number.
  char customKey;
  if(letter == true) customKey = letterKeypad.getKey();
  if(number == true) customKey = numberKeypad.getKey();
  
  // Write the custom key.
  if (customKey){
    Keyboard.write(customKey);
  }
}

存在的问题

如果你只是跟着上面的步骤去制作的话,你会发现根本大不了游戏,因为键盘没有接地,单片机无法扫描,代码的倒数第九行的getkey()只会触发一次。但是启用引脚的pull up功能,然后把按键独立接地,这些功能就可以实现了,经过我几个夜晚的调试,最终完成了以下代码:

射击游戏:

#include <Key.h>
#include <Keypad.h>

#include <Mouse.h>

#include <Keyboard.h>

#define VRX_L A0
#define VRY_L A1
#define SW_L 14
#define VRX_R A2
#define VRY_R A3
#define SW_R 16


#define let_LED 15
#define num_LED 10

char KeyboardNum[8] = {  
  'q',
  'w',
  'e', 
  'r',
  'a',  
  's',
  'd',
  ' ',
  };



int joystick_left_x, joystick_left_y, joystick_left_button, joystick_right_x, joystick_right_y, joystick_right_button;

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

  Mouse.begin();
  Keyboard.begin();

  // Joystick button settings:
  pinMode(SW_L, INPUT);
  digitalWrite(SW_L, HIGH);
  pinMode(SW_R, INPUT);
  digitalWrite(SW_R, HIGH);

  // Keypad LEDs:
  pinMode(let_LED, OUTPUT);
  digitalWrite(let_LED, HIGH);
  pinMode(num_LED, OUTPUT);
  digitalWrite(num_LED, LOW);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
}

void loop() {
  // joystick positions:
  read_joysticks();

  // Mouse:
  mouse_controls();

  // Keyboard:
  keyboard_controls();

}

void read_joysticks(){
  joystick_left_x = analogRead(VRX_L); 
  joystick_left_y = analogRead(VRY_L);
  joystick_left_button = digitalRead(SW_L);
  
  joystick_right_x = analogRead(VRX_R); 
  joystick_right_y = analogRead(VRY_R);  
  joystick_right_button = digitalRead(SW_R);

  /*
  Serial.print(joystick_right_x);
  Serial.print("\t");
  Serial.print(joystick_right_y);
  Serial.print("\t\t");
  Serial.print(joystick_right_button);
  Serial.print("\n\n");
  */
}

void mouse_controls(){
  if(joystick_left_y > 700)  Mouse.move(-15,0); // UP
  if(joystick_left_y < 300)  Mouse.move(15,0);  // DOWN
  if(joystick_left_x > 700)  Mouse.move(0,15); // LEFT
  if(joystick_left_x < 300)  Mouse.move(0,-15);  // RIGHT
  if(joystick_left_button == 0) Mouse.click(MOUSE_LEFT); // LEFT CLICK
  if(joystick_right_button == 0) Mouse.click(MOUSE_RIGHT); // RIGHT CLICK
}

void keyboard_controls(){
  if(joystick_right_y > 700){ Keyboard.press(KEY_RETURN); delay(100); Keyboard.releaseAll(); }    // RETURN
  if(joystick_right_y < 300){ Keyboard.press(KEY_BACKSPACE); delay(100); Keyboard.releaseAll(); } // BACKSPACE
  if(joystick_right_x > 700){  digitalWrite(let_LED, HIGH); digitalWrite(num_LED, LOW); } // Letter Keypad  
  if(joystick_right_x < 300){ digitalWrite(let_LED, LOW); digitalWrite(num_LED, HIGH); } // Number Keypad
  
  if(digitalRead(2) == LOW) {Keyboard.press(KeyboardNum[0]);}
  if(digitalRead(2)==HIGH){Keyboard.release(KeyboardNum[0]);} 
  if(digitalRead(3) == LOW) {Keyboard.press(KeyboardNum[1]);}
  if(digitalRead(3)==HIGH){Keyboard.release(KeyboardNum[1]);}
  if(digitalRead(4) == LOW) {Keyboard.press(KeyboardNum[2]);}
  if(digitalRead(4)==HIGH){Keyboard.release(KeyboardNum[2]);}
  if(digitalRead(5) == LOW) {Keyboard.press(KeyboardNum[3]);}
  if(digitalRead(5)==HIGH){Keyboard.release(KeyboardNum[3]);}
  if(digitalRead(6) == LOW) {Keyboard.press(KeyboardNum[4]);}
  if(digitalRead(6)==HIGH){Keyboard.release(KeyboardNum[4]);}
  if(digitalRead(7) == LOW) {Keyboard.press(KeyboardNum[5]);}
  if(digitalRead(7)==HIGH){Keyboard.release(KeyboardNum[5]);}
  if(digitalRead(8) == LOW) {Keyboard.press(KeyboardNum[6]);}
  if(digitalRead(8)==HIGH){Keyboard.release(KeyboardNum[6]);}
  if(digitalRead(9) == LOW) {Keyboard.press(KeyboardNum[7]);}
  if(digitalRead(9)==HIGH){Keyboard.release(KeyboardNum[7]);}
}

格斗游戏

#include <Key.h>
#include <Keypad.h>

#include <Mouse.h>

#include <Keyboard.h>

#define VRX_L A0
#define VRY_L A1
#define SW_L 14
#define VRX_R A2
#define VRY_R A3
#define SW_R 16


#define let_LED 15
#define num_LED 10

char KeyboardNum[8] = {  
  'u',
  'i',
  'o', 
  'p',
  'h',  
  'j',
  'k',
  'l',
  };



int joystick_left_x, joystick_left_y, joystick_left_button, joystick_right_x, joystick_right_y, joystick_right_button;

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

  Mouse.begin();
  Keyboard.begin();

  // Joystick button settings:
  pinMode(SW_L, INPUT);
  digitalWrite(SW_L, HIGH);
  pinMode(SW_R, INPUT);
  digitalWrite(SW_R, HIGH);

  // Keypad LEDs:
  pinMode(let_LED, OUTPUT);
  digitalWrite(let_LED, HIGH);
  pinMode(num_LED, OUTPUT);
  digitalWrite(num_LED, LOW);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
}

void loop() {
  // joystick positions:
  read_joysticks();

  // Mouse:
  mouse_controls();

  // Keyboard:
  keyboard_controls();

}

void read_joysticks(){
  joystick_left_x = analogRead(VRX_L); 
  joystick_left_y = analogRead(VRY_L);
  joystick_left_button = digitalRead(SW_L);
  
  joystick_right_x = analogRead(VRX_R); 
  joystick_right_y = analogRead(VRY_R);  
  joystick_right_button = digitalRead(SW_R);

  /*
  Serial.print(joystick_right_x);
  Serial.print("\t");
  Serial.print(joystick_right_y);
  Serial.print("\t\t");
  Serial.print(joystick_right_button);
  Serial.print("\n\n");
  */
}

void mouse_controls(){
  if(joystick_left_y > 700)  { Keyboard.press(KEY_LEFT_ARROW); delay(100); Keyboard.releaseAll(); }
  if(joystick_left_y < 300)  { Keyboard.press(KEY_RIGHT_ARROW); delay(100); Keyboard.releaseAll(); }
  if(joystick_left_x > 700)  { Keyboard.press(KEY_DOWN_ARROW); delay(100); Keyboard.releaseAll(); }
  if(joystick_left_x < 300)  { Keyboard.press(KEY_UP_ARROW); delay(100); Keyboard.releaseAll(); }
  if(joystick_left_button == 0) Mouse.click(MOUSE_LEFT); // LEFT CLICK
  if(joystick_right_button == 0) Mouse.click(MOUSE_RIGHT); // RIGHT CLICK
}

void keyboard_controls(){
  if(joystick_right_y > 700){ Keyboard.press(KEY_RETURN); delay(100); Keyboard.releaseAll(); }    // RETURN
  if(joystick_right_y < 300){ Keyboard.press(KEY_BACKSPACE); delay(100); Keyboard.releaseAll(); } // BACKSPACE
  if(joystick_right_x > 700){  digitalWrite(let_LED, HIGH); digitalWrite(num_LED, LOW); } // Letter Keypad  
  if(joystick_right_x < 300){ digitalWrite(let_LED, LOW); digitalWrite(num_LED, HIGH); } // Number Keypad
  
  if(digitalRead(2) == LOW) {Keyboard.press(KeyboardNum[0]);}
  if(digitalRead(2)==HIGH){Keyboard.release(KeyboardNum[0]);} 
  if(digitalRead(3) == LOW) {Keyboard.press(KeyboardNum[1]);}
  if(digitalRead(3)==HIGH){Keyboard.release(KeyboardNum[1]);}
  if(digitalRead(4) == LOW) {Keyboard.press(KeyboardNum[2]);}
  if(digitalRead(4)==HIGH){Keyboard.release(KeyboardNum[2]);}
  if(digitalRead(5) == LOW) {Keyboard.press(KeyboardNum[3]);}
  if(digitalRead(5)==HIGH){Keyboard.release(KeyboardNum[3]);}
  if(digitalRead(6) == LOW) {Keyboard.press(KeyboardNum[4]);}
  if(digitalRead(6)==HIGH){Keyboard.release(KeyboardNum[4]);}
  if(digitalRead(7) == LOW) {Keyboard.press(KeyboardNum[5]);}
  if(digitalRead(7)==HIGH){Keyboard.release(KeyboardNum[5]);}
  if(digitalRead(8) == LOW) {Keyboard.press(KeyboardNum[6]);}
  if(digitalRead(8)==HIGH){Keyboard.release(KeyboardNum[6]);}
  if(digitalRead(9) == LOW) {Keyboard.press(KeyboardNum[7]);}
  if(digitalRead(9)==HIGH){Keyboard.release(KeyboardNum[7]);}
}

使用以上代码时,按键相对独立,8个按键分别由8个引脚控制。

成品图:

请添加图片描述

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-11-15 16:02:04  更:2021-11-15 16:03:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 8:45:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码