ringringrain/arduino/rainboard-pad/rainboard-pad.ino

104 lines
2.6 KiB
C++

//
// ringringrain - pad(digital input) part. (arduino micro)
//
// D. Yi @ 2023 5 5
//
////pin usage
// 5 6 7 8 9 10 11 12 + 18(a0) 19(a1) 20(a2) 21(a3) 22(a4) 23(a5) == 14 digital in
//
// 0 1 => uart
// 0 1 => all works.. use serial (TTL+WIFI) or (RS232)
////pad structure
#define FLIPPED
// --> comment/uncomment for horizontal flipping of the installation.
#define NUMKEYS 14 // columns : 14, rows : 1
// --> how many keys are available for this pad
int keystat[NUMKEYS] = {0, };
// --> a buffer to store statuses of all keys
// --> needed to extract only 'the change' of key statuses
#define MAXCHANGES 10
int keychanges[MAXCHANGES] = {0, };
int n_keychg = 0;
// --> for monitoring only 'changed' keys.
#define NUMCOLS 14
#ifdef FLIPPED
int pins_cols[NUMCOLS] = {5, 6, 7, 8, 9, 10, 11, 12, 18, 19, 20, 21, 22, 23}; //14
// --> cols is horizontal offsets on the board
// --> to be read as input.
#else
int pins_cols[NUMCOLS] = {23, 22, 21, 20, 19, 18, 12, 11, 10, 9, 8, 7, 6, 5}; //14
// --> cols is horizontal offsets on the board
// --> to be read as input.
#endif
void setup()
{
//disable TX/RX led blinking: Arduino Micro specific
// pinMode(LED_BUILTIN_TX,INPUT);
// pinMode(LED_BUILTIN_RX,INPUT);
//serial
Serial.begin(57600); //debug (USB)
Serial1.begin(57600); //esp8266
//pinmodes
// cols : to be read as input. (PULL-UP)
for (int idx = 0; idx < NUMCOLS; idx++) {
pinMode(pins_cols[idx], INPUT_PULLUP);
}
}
void loop()
{
// perform a full scan of the key statuses!
// read all cols
for (int col = 0; col < NUMCOLS; col++) {
int key = col;
if (key < NUMKEYS) {
int state = !digitalRead(pins_cols[col]); //logic inversion. (HIGH = noteoff, LOW = noteon.)
// if it is 'changed',
if (state != keystat[key]) {
// if there is a room to record this change,
if (n_keychg < MAXCHANGES) {
// make a memo.
int keycoded = (key) * 10 + state; // for example : C4 note-on --> 601 ( == 60*10 + 1)
keychanges[n_keychg] = keycoded;
n_keychg++;
// send msg. over Serial.
byte * b = (byte*)&keycoded;
Serial1.write(b, 2);
//
// Serial.printf("sizeof(keycoded): %d\n", sizeof(keycoded)); // result == 2 (16bit machine)
}
}
// okay. good. now, apply the change.
keystat[key] = state;
}
}
// print out the changes.
for (int chg = 0; chg < n_keychg; chg++) {
Serial.print(keychanges[chg]);
Serial.print(" ");
}
// clear 'keychanges' array
for (int idx = 0; idx < MAXCHANGES; idx++) {
keychanges[idx] = 0;
n_keychg = 0;
}
//
delay(50);
}