Dear All.
I really appreciate your response.
- I dont use 4051 to 'read'.
- I just want more digital output.
I knew that I can stack more 595, but it'll need me to do Shiftout more then once.
I want use this schematic for Digital/Analaog input that will read by Arduino Analog pin.
The 3x 8 output pin of 4051 will threaded as 'Row', and the Arduino Analog pins as 'Column'
This is the basic sketch :
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
uint8_t rows[]={8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
32,
33,
34,
35,
36,
37,
38,
39};
byte readmode[]={'t', // 't' = digital read, and
't', // ~ (NOT) the last value
't',
't',
't',
'm', // 'm' = digital read, Send HIGH=NoteOn , LOW=NoteOFF
'a', // 'a' = Analog read, Send CC
'a',
't',
't',
't',
't',
't',
'm',
'a',
'a',
't',
't',
't',
't',
't',
'm',
'a',
'a'};
uint8_t midchans[]={1,2,3}; //MIDI Channel of Left, Master, Right
uint8_t keyvals[3][2][24] = {};
uint8_t x,y,z =0; //Z=Row, Y=Sub-Group/Chan , X=Group/Chan
int loopcount=0;
long startmils=0;
void setup(){
Serial.begin(115200);
pinMode(latchPin, OUTPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
for (x = 0 ; x <3 ; x++){
for (y = 0 ; y<2; y++) {
for (z = 0 ; z<24; z++) {
keyvals[x][y][z]=0;
}
}
}
}
void loop(){
for (z = 0; z < 24; z++) {
Serial.print("Z");
moverow(z);
for (y=0 ; y<3; y++){
//pinstart = x*2
Serial.print("Y");
for (x=0; x<2; x++ ){
Serial.print("X");
//readpin=(x*2)+y
prockey(x,y,z);
}
}
}
}
void moverow(int thisrow){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, rows[thisrow]);
digitalWrite(latchPin, HIGH);
}
void prockey(int proc_x, int proc_y, int proc_z){
//First we read the analog pin
int readpin=(proc_x*2)+proc_y ;
int keyval=0;
switch (readmode[proc_z]){
case 't' : //Only Togled every time it's HIGH
keyval=digitalRead(14+readpin);
if (keyval) { //button pressed
keyvals[proc_x][proc_y][proc_z]=~keyvals[proc_x][proc_y][proc_z]; //togle last key val.
//Here you must send the midi :
sendNote(proc_x,proc_y,proc_z);
}
break;
case 'm' : // Only togled when current_keyval != last_keyvals
keyval=digitalRead(14+readpin);
if (keyval != keyvals[proc_x][proc_y][proc_z]){
keyvals[proc_x][proc_y][proc_z]=keyval;
sendNote(proc_x,proc_y,proc_z);
}
break;
case 'a' : //Only Togled when current_keyval != last_keyvals
keyval=int((analogRead(14+readpin)/1023)*127); //converted to 0-127 for midi compatible
if (keyval != keyvals[proc_x][proc_y][proc_z]){
keyvals[proc_x][proc_y][proc_z]=keyval;
/*Send MidiChange
midi channel = midichans[proc_x]
keynumber = keynum
value = keyval
*/
}
break;
}
}
void sendNote(int send_x, int send_y, int send_z){
int keynum=(send_z*(send_y+1))+1;
if (keyvals[send_x][send_y][send_z]){
/*NoteON with :
midi channel = midichans[send_x]
keynumber = keynum
midival = 1
*/
}
else {
/*NoteOff with :
midi channel = midichans[send_x]
keynumber = keynum
*/
}
}