Помогите соединить два скетча для ButtonBox
- Войдите на сайт для отправки комментариев
Ср, 10/07/2019 - 13:15
Здравствуйте! Есть скетч. В нем прописано 4 энкодера. Как в нем убрать два энкодера, и добавить два джойстика из второго скетча?
1 скетч:
001 | //Simple buttonbox sketch |
002 | //Supports up to 25 buttons and up to 4 encoders |
003 | //version 0.2 by TOPMO3 |
004 | // |
005 | // |
006 | //Arduino IDE 1.6.6 (or above) ! |
007 | // |
008 | //Joystick library from Matthew Heironimus, <a href="https://github.com/MHeironimus/ArduinoJoystickLibrary" rel="nofollow">https://github.com/MHeironimus/ArduinoJoystickLibrary</a> |
009 | // |
010 | //Encoders code from Ben Buxton |
011 | //More info: <a href="http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html" rel="nofollow">http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html</a> |
012 | // |
013 | //Thank you guys! :) |
014 | // |
015 |
016 |
017 |
018 | //Uncomment this for HALFSTEP encoder operation |
019 | //#define HALF_STEP |
020 |
021 | #include <Keypad.h> |
022 | #include <Joystick.h> |
023 |
024 |
025 |
026 | #define ENABLE_PULLUPS |
027 | #define NUMROTARIES 4 |
028 | #define NUMBUTTONS 25 |
029 | #define NUMROWS 5 |
030 | #define NUMCOLS 5 |
031 |
032 |
033 | //define the symbols on the buttons of the keypads |
034 | byte buttons[NUMROWS][NUMCOLS] = { |
035 | {1,2,3,4,5}, |
036 | {6,7,8,9,10}, |
037 | {11,12,13,14,15}, |
038 | {16,17,18,19,20}, |
039 | {21,22,23,24,25}, |
040 | }; |
041 |
042 |
043 |
044 | struct rotariesdef { |
045 | byte pin1; |
046 | byte pin2; |
047 | int ccwchar; |
048 | int cwchar; |
049 | volatile unsigned char state; |
050 | }; |
051 |
052 | rotariesdef rotaries[NUMROTARIES] { |
053 | {0,1,26,27,0}, |
054 | {2,3,28,29,0}, |
055 | {4,5,30,31,0}, |
056 | {6,7,32,33,0}, |
057 | }; |
058 |
059 |
060 |
061 | #define DIR_CCW 0x10 |
062 | #define DIR_CW 0x20 |
063 | #define R_START 0x0 |
064 |
065 | #ifdef HALF_STEP |
066 | // Use the half-step state table (emits a code at 00 and 11) |
067 | #define R_CCW_BEGIN 0x1 |
068 | #define R_CW_BEGIN 0x2 |
069 | #define R_START_M 0x3 |
070 | #define R_CW_BEGIN_M 0x4 |
071 | #define R_CCW_BEGIN_M 0x5 |
072 | const unsigned char ttable[6][4] = { |
073 | // R_START (00) |
074 | {R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START}, |
075 | // R_CCW_BEGIN |
076 | {R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START}, |
077 | // R_CW_BEGIN |
078 | {R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START}, |
079 | // R_START_M (11) |
080 | {R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START}, |
081 | // R_CW_BEGIN_M |
082 | {R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW}, |
083 | // R_CCW_BEGIN_M |
084 | {R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW}, |
085 | }; |
086 | #else |
087 | // Use the full-step state table (emits a code at 00 only) |
088 | #define R_CW_FINAL 0x1 |
089 | #define R_CW_BEGIN 0x2 |
090 | #define R_CW_NEXT 0x3 |
091 | #define R_CCW_BEGIN 0x4 |
092 | #define R_CCW_FINAL 0x5 |
093 | #define R_CCW_NEXT 0x6 |
094 |
095 | const unsigned char ttable[7][4] = { |
096 | // R_START |
097 | {R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START}, |
098 | // R_CW_FINAL |
099 | {R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW}, |
100 | // R_CW_BEGIN |
101 | {R_CW_NEXT, R_CW_BEGIN, R_START, R_START}, |
102 | // R_CW_NEXT |
103 | {R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START}, |
104 | // R_CCW_BEGIN |
105 | {R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START}, |
106 | // R_CCW_FINAL |
107 | {R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW}, |
108 | // R_CCW_NEXT |
109 | {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START}, |
110 | }; |
111 | #endif |
112 |
113 |
114 | byte rowPins[NUMROWS] = {21,20,19,18,15}; //connect to the row pinouts of the keypad |
115 | byte colPins[NUMCOLS] = {14,16,10,9,8}; //connect to the column pinouts of the keypad |
116 |
117 | //initialize an instance of class NewKeypad |
118 | Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS); |
119 |
120 | //initialize an Joystick with 34 buttons; |
121 | Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, |
122 | JOYSTICK_TYPE_JOYSTICK, 34, 0, |
123 | false , false , false , false , false , false , |
124 | false , false , false , false , false ); |
125 |
126 |
127 | void setup () { |
128 | Joystick.begin(); |
129 | rotary_init(); |
130 | } |
131 |
132 |
133 |
134 | void loop () { |
135 |
136 | CheckAllEncoders(); |
137 |
138 | CheckAllButtons(); |
139 |
140 | } |
141 |
142 |
143 | void CheckAllButtons( void ) { |
144 | if (buttbx.getKeys()) |
145 | { |
146 | for ( int i=0; i<LIST_MAX; i++) // Scan the whole key list. |
147 | { |
148 | if ( buttbx.key[i].stateChanged ) // Only find keys that have changed state. |
149 | { |
150 | switch (buttbx.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED |
151 | case PRESSED: |
152 | case HOLD: |
153 | Joystick.setButton(buttbx.key[i].kchar, 1); |
154 | break ; |
155 | case RELEASED: |
156 | case IDLE: |
157 | Joystick.setButton(buttbx.key[i].kchar, 0); |
158 | break ; |
159 | } |
160 | } |
161 | } |
162 | } |
163 | } |
164 |
165 |
166 | /* Call this once in setup(). */ |
167 | void rotary_init() { |
168 | for ( int i=0;i<NUMROTARIES;i++) { |
169 | pinMode(rotaries[i].pin1, INPUT); |
170 | pinMode(rotaries[i].pin2, INPUT); |
171 | #ifdef ENABLE_PULLUPS |
172 | digitalWrite(rotaries[i].pin1, HIGH); |
173 | digitalWrite(rotaries[i].pin2, HIGH); |
174 | #endif |
175 | } |
176 | } |
177 |
178 |
179 | /* Read input pins and process for events. Call this either from a |
180 | * loop or an interrupt (eg pin change or timer). |
181 | * |
182 | * Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction. |
183 | */ |
184 | unsigned char rotary_process( int _i) { |
185 | unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1); |
186 | rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate]; |
187 | return (rotaries[_i].state & 0x30); |
188 | } |
189 |
190 | void CheckAllEncoders( void ) { |
191 | for ( int i=0;i<NUMROTARIES;i++) { |
192 | unsigned char result = rotary_process(i); |
193 | if (result == DIR_CCW) { |
194 | Joystick.setButton(rotaries[i].ccwchar, 1); delay(50); Joystick.setButton(rotaries[i].ccwchar, 0); |
195 | }; |
196 | if (result == DIR_CW) { |
197 | Joystick.setButton(rotaries[i].cwchar, 1); delay(50); Joystick.setButton(rotaries[i].cwchar, 0); |
198 | }; |
199 | } |
200 | } |
2 скетч:
Moderator : пожалуйста, вставьте код правильно - http://arduino.ru/forum/obshchii/vstavka-programmnogo-koda-v-temukommentarii