ok, where to begin,
i am what you would you would call green, and when i say green i mean like only a month into learning how to use an arduino, since im out of work due to injury, i have delved into the rabbit hole so far i can safely say theirs no way out for me.
anyway;
weeks into learning how to use the arduino i finally finished my first sketch. and i have to say im pretty damn proud of it.
but it isnt complete;
i modified a sketch from the library, two enable me to keyboard press characters using encoder rotation each encoder also has a button which blinks an led.
i initially wanted to master/slave two pro micros, i also read into shift registers as well but the learning curve is just beyond me at the moment. so i decided to power the button box using two independent arduino pro micro development board
one, two run half the buttons
the other, two run more leds and encoders.
and just run their individual usb's to power and communicate.
although i would eventually want to run the sketch as master/slave,
right now i would just want to finish the sketch.
i want to know if its feasible to blink an led while i rotate an encoder in any direction
it doesnt have to be perfect, i just want feedback that im using the encoder.
like i said before i am relatively new at this, so what you see in the sketch is essentially what ive learned. i still dont know how to shortcut many functions.
sorry if i am unclear, still learning the correct terms to use.
here is the sketch so far.
#include <Keypad.h>
#include <Keyboard.h>
#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS 9
#define NUMROWS 2
#define NUMCOLS 3
//define the symbols on the buttons of the keypads
char buttons[NUMROWS][NUMCOLS] = {
//row pins
{'q','w','x',}, // 8
{'y','u','v',}, // 9
// col pins
//14 15 16
};
struct rotariesdef {
byte pin1;
byte pin2;
int ccwchar;
int cwchar;
volatile unsigned char state;
};
//{encoder pins,characters press
rotariesdef rotaries[NUMROTARIES] {
{0,1,'r','s',0},
{2,3,'t','u',0},
{4,5,'y','z',0},
{6,7,'0','p',0},
};
#define ledpinA10 A10
#define ledpinA0 A0
#define ledpinA1 A1
#define ledpinA2 A2
#define ledpinA3 A3
#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttable[7][4] = {
// R_START
{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CW_FINAL
{R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
// R_CW_BEGIN
{R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
// R_CW_NEXT
{R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
// R_CCW_BEGIN
{R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
// R_CCW_FINAL
{R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
// R_CCW_NEXT
{R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};
byte rowPins[NUMROWS] = {8,9,}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {14,15,16,}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
// test to see funtion
//boolean ledpinA0_state;
//boolean ledpinA1_state;
//boolean ledpinA2_state;
//boolean ledpinA3_state;
void setup() {
// initialize all analog pins as output
pinMode(ledpinA10,OUTPUT);
pinMode(ledpinA0,OUTPUT);
pinMode(ledpinA1,OUTPUT);
pinMode(ledpinA2,OUTPUT);
pinMode(ledpinA3,OUTPUT);
//led check set-up
digitalWrite(ledpinA10,HIGH);
digitalWrite(ledpinA0,HIGH);
digitalWrite(ledpinA1,HIGH);
digitalWrite(ledpinA2,HIGH);
digitalWrite(ledpinA3,HIGH);
delay(200);
digitalWrite(ledpinA10,LOW);
digitalWrite(ledpinA0,LOW);
digitalWrite(ledpinA1,LOW);
digitalWrite(ledpinA2,LOW);
digitalWrite(ledpinA3,LOW);
delay(200);
digitalWrite(ledpinA10,HIGH);
digitalWrite(ledpinA0,HIGH);
digitalWrite(ledpinA1,HIGH);
digitalWrite(ledpinA2,HIGH);
digitalWrite(ledpinA3,HIGH);
delay(200);
digitalWrite(ledpinA10,LOW);
digitalWrite(ledpinA0,LOW);
digitalWrite(ledpinA1,LOW);
digitalWrite(ledpinA2,LOW);
digitalWrite(ledpinA3,LOW);
delay(200);
// led final setup all led stay on
digitalWrite(ledpinA10,HIGH);
delay(200);
digitalWrite(ledpinA0,HIGH);
delay(200);
digitalWrite(ledpinA1,HIGH);
delay(200);
digitalWrite(ledpinA2,HIGH);
delay(200);
digitalWrite(ledpinA3,HIGH);
delay(200);
Keyboard.begin();
rotary_init();
}
void loop() {
CheckAllEncoders();
CheckAllButtons();
}
void CheckAllButtons(void) {
char key = buttbx.getKey();
if (key == 'v') {
Keyboard.write(key);
digitalWrite(ledpinA3,LOW);
delay(150);
Keyboard.release(key);
digitalWrite(ledpinA3,HIGH);
}
if (key == 'x') {
Keyboard.write(key);
digitalWrite(ledpinA2,LOW);
delay(150);
Keyboard.release(key);
digitalWrite(ledpinA2,HIGH);
}
if (key == 'q') {
Keyboard.write(key);
digitalWrite(ledpinA1,LOW);
delay(150);
Keyboard.release(key);
digitalWrite(ledpinA1,HIGH);
}
if (key == 'w') {
Keyboard.write(key);
digitalWrite(ledpinA0,LOW);
delay(150);
Keyboard.release(key);
digitalWrite(ledpinA0,HIGH);
}
}
/* Call this once in setup(). */
void rotary_init() {
for (int i=0;i<NUMROTARIES;i++) {
pinMode(rotaries[i].pin1, INPUT);
pinMode(rotaries[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(rotaries[i].pin1, HIGH);
digitalWrite(rotaries[i].pin2, HIGH);
#endif
}
}
/* Read input pins and process for events. Call this either from a
* loop or an interrupt (eg pin change or timer).
*
* Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction.
*/
unsigned char rotary_process(int _i) {
unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
return (rotaries[_i].state & 0x30);
}
void CheckAllEncoders(void) {
for (int i=0;i<NUMROTARIES;i++) {
unsigned char result = rotary_process(i);
if (result) {
Keyboard.write(result == DIR_CCW ? rotaries[i].ccwchar : rotaries[i].cwchar );
}
}
}[/td]
[/tr]
[/table]