|
用PS2手柄玩游戏都见过,但是用没有蓝牙的PS2手柄控制电脑,操控红外设备还是第一次见。这篇文章将让你的PS2手柄摇身一变,让你知道PS2手柄不光只能连上ps2主机玩游戏,还可以和控制现实生活里的万物。  【硬件列表】 | 名称 | | | | | | | | 2PS2手柄接受模块 | | | | 3红外发射模块 | | | | 4oled12864 | | | | 5温湿度传感器 | | | | 6倾斜传感器 | | | | 7USB线 | | | | 8三拼线 | | |
【实验过程】 1链接各个模块与主板: (1) 将PS2手柄接收器的DAT, CMD, CS,CLK分别连在D4-D7引脚。 (2) 将OLED12864显示器SDA和SCL连在D2和D3引脚上(因为和UNO不同,Leonardo的SCL和SDA分别在D3和D2引脚上) (3) 将DHT11温湿度传感器连接在D9引脚 (4) 将红外发射连接在D8引脚 (5) 将倾斜传感器连接在D10引脚 2 注入灵魂(烧录代码)(我是用MAND+做的程序是我复制的,亲谅解,后续会学会使用IDE的) (1) 功能1:操作电脑——使用ps2手柄左摇杆控制鼠标,右摇杆方向键上下左右,L2,R2为鼠标左右键,L1,R2为Ctrl,shift,键。
- void DF_DianNaoCaoZuo() {
- Keyboard.releaseAll();
- oled12864.fillScreen(0);
- oled12864.setCursor(0, 1);
- oled12864.print(" 操作电脑");
- while (!ps2x.Button(PSB_SELECT)) {
- ps2x.read_gamepad();
- delay(30);
- if ((ps2x.Analog(PSS_LX)>=230)) {
- Mouse.move(20, 0, 0);
- }
- if ((ps2x.Analog(PSS_LY)>=230)) {
- Mouse.move(0, 20, 0);
- }
- if ((ps2x.Analog(PSS_LY)<=40)) {
- Mouse.move(0, -20, 0);
- }
- if ((ps2x.Analog(PSS_LX)<=40)) {
- Mouse.move(-20, 0, 0);
- }
- if (((ps2x.Analog(PSS_LX)>=140) && (ps2x.Analog(PSS_LX)<230))) {
- Mouse.move(10, 0, 0);
- }
- if (((ps2x.Analog(PSS_LY)>=160) && (ps2x.Analog(PSS_LY)<230))) {
- Mouse.move(0, 10, 0);
- }
- if (((ps2x.Analog(PSS_LY)>40) && (ps2x.Analog(PSS_LY)<100))) {
- Mouse.move(0, -10, 0);
- }
- if (((ps2x.Analog(PSS_LX)>40) && (ps2x.Analog(PSS_LX)<100))) {
- Mouse.move(-10, 0, 0);
- }
- if (ps2x.Button(PSB_RED)) {
- delay(20);
- Keyboard.press(KEY_F3);
- }
- else {
- Keyboard.release(KEY_F3);
- }
- if (ps2x.Button(PSB_GREEN)) {
- delay(20);
- Keyboard.press(KEY_F1);
- }
- else {
- Keyboard.release(KEY_F1);
- }
- if (ps2x.Button(PSB_PINK)) {
- delay(20);
- Keyboard.press(KEY_F2);
- }
- else {
- Keyboard.release(KEY_F2);
- }
- if (ps2x.Button(PSB_BLUE)) {
- delay(20);
- Keyboard.press(KEY_F4);
- }
- else {
- Keyboard.release(KEY_F4);
- }
- if ((ps2x.Button(PSB_PAD_RIGHT) || (ps2x.Analog(PSS_RX)>=200))) {
- delay(20);
- Keyboard.press(KEY_RIGHT_ARROW);
- }
- else {
- Keyboard.release(KEY_RIGHT_ARROW);
- }
- if ((ps2x.Button(PSB_PAD_DOWN) || (ps2x.Analog(PSS_RY)>=200))) {
- delay(20);
- Keyboard.press(KEY_DOWN_ARROW);
- }
- else {
- Keyboard.release(KEY_DOWN_ARROW);
- }
- if ((ps2x.Button(PSB_PAD_UP) || (ps2x.Analog(PSS_RY)<=100))) {
- delay(20);
- Keyboard.press(KEY_UP_ARROW);
- }
- else {
- Keyboard.release(KEY_UP_ARROW);
- }
- if ((ps2x.Button(PSB_PAD_LEFT) || (ps2x.Analog(PSS_RX)<=100))) {
- delay(20);
- Keyboard.press(KEY_LEFT_ARROW);
- }
- else {
- Keyboard.release(KEY_LEFT_ARROW);
- }
- if (ps2x.Button(PSB_L1)) {
- delay(20);
- Keyboard.press(KEY_LEFT_CTRL);
- }
- else {
- Keyboard.release(KEY_LEFT_CTRL);
- }
- if (ps2x.Button(PSB_L2)) {
- delay(20);
- Mouse.press(MOUSE_LEFT);
- }
- else {
- Mouse.release(MOUSE_LEFT);
- }
- if (ps2x.Button(PSB_R1)) {
- delay(20);
- Keyboard.press(KEY_RIGHT_SHIFT);
- }
- else {
- Keyboard.release(KEY_RIGHT_SHIFT);
- }
- if (ps2x.Button(PSB_R2)) {
- delay(20);
- Mouse.press(MOUSE_RIGHT);
- }
- else {
- Mouse.release(MOUSE_RIGHT);
- }
- ps2x.read_gamepad();
- delay(30);
- }
- DF_XuanZeMoShi();
- }
复制代码
(2) 功能2:红外操作,可以用ps2的按键状态传感,用红外发射模块控制红外设备。 - void DF_HongWaiCaoZuo() {
- oled12864.fillScreen(0);
- oled12864.setCursor(0, 1);
- oled12864.print(" 红外操作");
- while (!ps2x.Button(PSB_SELECT)) {
- ps2x.read_gamepad();
- delay(30);
- if (ps2x.Button(PSB_RED)) {
- delay(200);
- // 开关
- remoteSend_8.sendNEC(0xFF00FF, 32);
- delay(150);
- }
- if (ps2x.Button(PSB_GREEN)) {
- delay(200);
- // 摆头
- remoteSend_8.sendNEC(0xFF40BF, 32);
- delay(150);
- }
- if (ps2x.Button(PSB_PAD_UP)) {
- delay(200);
- // 风量
- remoteSend_8.sendNEC(0xFF20DF, 32);
- delay(150);
- }
- if (ps2x.Button(PSB_PAD_DOWN)) {
- for (int index = 0; index < 7; index++) {
- delay(30);
- remoteSend_8.sendNEC(0xFF20DF, 32);
- delay(20);
- }
- }
- if (ps2x.Button(PSB_PAD_RIGHT)) {
- delay(200);
- // 定时
- remoteSend_8.sendNEC(0xFF609F, 32);
- delay(150);
- }
- if (ps2x.Button(PSB_PAD_LEFT)) {
- for (int index = 0; index < 9; index++) {
- delay(30);
- remoteSend_8.sendNEC(0xFF609F, 32);
- delay(20);
- }
- }
- if (ps2x.Button(PSB_BLUE)) {
- delay(200);
- // 静音
- remoteSend_8.sendNEC(0xFF10EF, 32);
- delay(150);
- }
- ps2x.read_gamepad();
- delay(30);
- }
- DF_XuanZeMoShi();
- }
复制代码
(3) 功能3:通道显示,可以显示出来ps2手柄的各个通道的数值。 - void DF_TongDaoXianShi() {
- oled12864.fillScreen(0);
- while (!ps2x.Button(PSB_SELECT)) {
- ps2x.read_gamepad();
- delay(30);
- oled12864.setCursor(0, 0);
- oled12864.print((String("L x:") + String(ps2x.Analog(PSS_LX))));
- oled12864.setCursor(65, 0);
- oled12864.print((String("L y:") + String(ps2x.Analog(PSS_LY))));
- oled12864.setCursor(0, 1);
- oled12864.print((String("R x:") + String(ps2x.Analog(PSS_RX))));
- oled12864.setCursor(65, 1);
- oled12864.print((String("R y:") + String(ps2x.Analog(PSS_RY))));
- if (ps2x.Button(PSB_GREEN)) {
- oled12864.setCursor(0, 2);
- oled12864.print(" △ ");
- }
- else {
- oled12864.setCursor(0, 2);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_RED)) {
- oled12864.setCursor(25, 2);
- oled12864.print(" ○ ");
- }
- else {
- oled12864.setCursor(25, 2);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_BLUE)) {
- oled12864.setCursor(51, 2);
- oled12864.print(" x ");
- }
- else {
- oled12864.setCursor(51, 2);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_PINK)) {
- oled12864.setCursor(76, 2);
- oled12864.print(" □ ");
- }
- else {
- oled12864.setCursor(76, 2);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_PAD_UP)) {
- oled12864.setCursor(0, 3);
- oled12864.print(" ↑ ");
- }
- else {
- oled12864.setCursor(0, 3);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_PAD_RIGHT)) {
- oled12864.setCursor(25, 3);
- oled12864.print(" → ");
- }
- else {
- oled12864.setCursor(25, 3);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_PAD_DOWN)) {
- oled12864.setCursor(51, 3);
- oled12864.print(" ↓ ");
- }
- else {
- oled12864.setCursor(51, 3);
- oled12864.print(" ");
- }
- if (ps2x.Button(PSB_PAD_LEFT)) {
- oled12864.setCursor(76, 3);
- oled12864.print(" ← ");
- }
- else {
- oled12864.setCursor(76, 3);
- oled12864.print(" ");
- }
- }
- DF_XuanZeMoShi();
- }
复制代码
(4) 功能4:睡眠模式,可以让显示器进入睡眠模式进行温湿度显示。 - void DF_ShuiMianMoShi() {
- oled12864.fillScreen(0);
- oled12864.setCursor(0, 1);
- oled12864.print(" 睡眠模式");
- delay(3000);
- while (!(ps2x.Button(PSB_SELECT) || (!digitalRead(10)))) {
- oled12864.setCursorLine(3);
- oled12864.printLine(dht11_9.getTemperature());
- oled12864.setCursorLine(4);
- oled12864.printLine(dht11_9.getHumidity());
- }
- DF_XuanZeMoShi();
- }
复制代码
【功能演示】 1, 操作电脑 2, 红外操作 3, 通道显示 4, 睡眠模式 [size=18.6667px]【总结】
本项目利用了主要利用Leonardo的usb通讯并且使用电器的红外编码来实现控制电脑和红外电器的如果有感兴趣的朋友欢迎来网盘下载程序https://ayccode.lanzoue.com/b01eupl2af密码:ayccode
|