跳到主要內容

自製 V-USB k-shoot mania controller for Arduino


旋鈕製作
http://mowei-tw.blogspot.tw/2015/06/kshootmania.html

V-USB 電路接法參考
http://coopermaa2nd.blogspot.tw/2011/10/v-usb-examples-for-arduino.html

D+ , D- 改接成下面

D+ 接腳2

D-  接腳3

按鈕4~10腳
導通到GND觸發
10接腳分 短按 長按 1秒當作分隔

A0,A1一個旋鈕
A2,A3一個旋鈕

程式碼&hex:https://www.dropbox.com/s/j6nek8wpndflp4b/KshootJOY.rar?dl=0

#include "UsbJoystick.h"

class rotaryDevice {
    private:
        int analogInPin[2];
    int newValue[2];
    int oldValue[2];
    int rotarytrigger;
    unsigned long rotarytime;
    public:
        rotaryDevice(int Pin, int Pin1) {
            analogInPin[0] = Pin;
            analogInPin[1] = Pin1;
            rotarytrigger = 0;
            rotarytime = 0;

            oldValue[0] = analogRead(analogInPin[0]);
            oldValue[1] = analogRead(analogInPin[1]);
        }
    int rotaryChanged() {
        newValue[0] = analogRead(analogInPin[0]);
        newValue[1] = analogRead(analogInPin[1]);
        int rotaryresult = rotary();
        if (rotaryresult == 1) {
            rotarytrigger = 1;
            rotarytime = millis();
            return 1;
        } else if (rotaryresult == -1) {
            rotarytrigger = 2;
            rotarytime = millis();
            return 2;
        }
        if (rotarytrigger > 0) {
            if ((millis() - rotarytime) < 30) {
                if (rotarytrigger == 1) {
                    return 1;
                } else if (rotarytrigger == 2) {
                    return 2;
                }
            } else {
                rotarytrigger = 0;
            }
        }
        return 0;
    }
    int rotary() {
        int type = oldValue[0] - newValue[0];
        int type1 = oldValue[1] - newValue[1];

        if (newValue[0] > 900 || newValue[0] < 1) {
            if (abs(type1) > 15) {
                oldValue[0] = newValue[0];
                oldValue[1] = newValue[1];
                if (type1 < 0) {
                    return 1;
                } else {
                    return -1;
                }
            }
        } else if (newValue[1] > 900 || newValue[1] < 1) {
            if (abs(type) > 15) {
                oldValue[0] = newValue[0];
                oldValue[1] = newValue[1];
                if (type < 0) {
                    return 1;
                } else {
                    return -1;
                }
            }
        } else {
            if (abs(type) > 10 && abs(type1) > 10) {
                oldValue[0] = newValue[0];
                oldValue[1] = newValue[1];
                if (type < 0 && type1 < 0) {
                    return 1;
                } else if (type > 0 && type1 > 0) {
                    return -1;
                }
            }
        }
        return 0;
    }

};

//BUTTON 0~9
int But0 = 4; //4~13
int But9 = 13;
unsigned long startButTime = 0;
unsigned long startButdownTime = 0;
int startButTrigger = 0;
int startButstate = 0;
rotaryDevice rotary = rotaryDevice(A0, A1);
rotaryDevice rotary2 = rotaryDevice(A2, A3);
void setup() {
    for (int i = But0; i <= But9; i++) {
        pinMode(i, INPUT_PULLUP);
    }
    UsbJoystick.setup();
}

void loop() {
    UsbJoystick.update();

    int rotaryresult = rotary.rotaryChanged();
    if (rotaryresult == 1) {
        UsbJoystick.setButton(12);
    } else if (rotaryresult == 2) {
        UsbJoystick.setButton(13);
    }
    int rotaryresult2 = rotary2.rotaryChanged();
    if (rotaryresult2 == 1) {
        UsbJoystick.setButton(14);
    } else if (rotaryresult2 == 2) {
        UsbJoystick.setButton(15);
    }


    for (int i = 0; i < 6; i++) {
        if (digitalRead(But0 + i) == LOW) {
            UsbJoystick.setButton(i);
        }
    }

    if (startButTrigger == 0 && digitalRead(10) == LOW) {
        startButTrigger = 1;
        startButTime = millis();
    }
    if (startButTrigger == 1 && digitalRead(10) == HIGH) {
        if ((millis() - startButTime) < 1000) {
            UsbJoystick.setButton(6);
            startButstate = 1;
            startButdownTime = millis();
        } else {
            UsbJoystick.setButton(7);
            startButstate = 2;
            startButdownTime = millis();
        }
        startButTrigger = 0;
    }
    if (startButstate > 0) {
        if ((millis() - startButdownTime) < 100) {
            if (startButstate == 1) {
                UsbJoystick.setButton(6);
            } else if (startButstate == 2) {
                UsbJoystick.setButton(7);
            }
        } else {
            startButstate = 0;
        }
    }
    UsbJoystick.sendMsg();
}





留言

這個網誌中的熱門文章

小蟻智慧攝影機支援RTSP

來源: http://en.miui.com/thread-196713-1-1.html 小蟻智慧攝影機版本: 1.8.5.1l_201511061661(台灣版) 下載檔案 https://drive.google.com/file/d/0ByjBix7wpeJJY0ZWZXlvWl9Za1E/view?usp=sharing 把攝影機SD卡拿讀卡機讀取 把壓縮檔內的test資料夾放進SD根目錄 插回SD卡至攝影機 重開 攝影機會呈現橘燈並執行更新 等恢復至藍燈或是用app確認攝影機開啟 用VLC或相關程式讀取串流 Main stream:rtsp:// <CameraIP> :554/ch0_0.h264 Minor stream:rtsp:// <CameraIP> :554/ch0_1.h264 Audio stream:rtsp:// <CameraIP> :554/ch0_3.h264 Telnet與FTP開啟加上 # Telnet if [ ! -f "/etc/init.d/S88telnet" ]; then     echo "#!/bin/sh" > /etc/init.d/S88telnet     echo "telnetd &" >> /etc/init.d/S88telnet     chmod 755 /etc/init.d/S88telnet fi # FTP echo "#!/bin/sh" > /etc/init.d/S89ftp echo "tcpsvd -vE 0.0.0.0 21 ftpd -w / &" >> /etc/init.d/S89ftp chmod 755 /etc/init.d/S89ftp User:  root Password:  1234qwer 使用passwd改密碼 參考: http://en.miui.com/thread-224653-1-1.html

Arduino 滑鼠滾輪編碼器/旋轉編碼器

滑鼠拆下來的滾輪編碼器 時序為                      → CW A  -----------          -------------    HIGH                ----------                  LOW                      ← CCW B  --------          ----------------    HIGH             ----------                     LOW A B 1  1↓CW 1  0 0  0 0  1 1  1 1  0 0  0 0  1↑CCW 程式碼參考底下網址來源修改而來 #define ENCODER_A_PIN 2 #define ENCODER_B_PIN 3 unsigned long time = 0; long position = 0; long num = 0; void setup() { pinMode(ENCODER_A_PIN,INPUT); pinMode(ENCODER_B_PIN,INPUT); attachInterrupt(0, RotaryEncoder, LOW); Serial.begin(9600); ti...

V-USB Joystick for Arduino

上次做好沒存檔 這次再翻出來從弄一次...QAQ 花了我一天阿阿阿阿 D+ 接腳2 D-  接腳3 詳情請看usbconfig.h 想改搖桿配置可以用 HID Descriptor Tool 改完記得回usbconfig.h填 USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 長度 程式碼: https://www.dropbox.com/s/n0rgkb370wto2lv/UsbJOY.rar?dl=0