|
> No.771[元記事へ]
nekosanさんへのお返事です。
お世話になります!
> B0Hや00Hなどの表記は、プログラム言語に因らない
> 16進数の一般表記なので、C言語系で16進数表記を
> 行うには、C言語流に、先頭を数値(0~9)で書き
> はじめる必要があるかと思います。
>
> →B0Hだと、先頭がアルファベットなので、定義済み
> の変数や予約語と認識するでしょうし、00Hは、
> C言語では最後のHが文法にマッチしません。
>
> C言語で16進数の定数を書くには、先頭に「0x」を
> つけます。B0Hなら、「0xB0」といった具合です。
> (ちなみに、xは「hex」→16進数のhexadecimalで、
> 8進数なら「0o」、2進数なら「0b」をつけます)
ご指摘、有り難う御座います!
そうなのですね、ちょうどその部分がコンパイルで引っかかっておりました・・・。
今後、気をつけます!
> 関数program_changeの引数部分、複数の値を渡すには、
> それぞれをカンマで区切って別のデータとして渡すか、
> 配列にしておいて先頭ポインタを渡す必要があるかと
> 思います。
>
> ポインタのことは説明が少々面倒になりそうなので、
> ひとまず置いておいて、値を1個1個区切って渡す方向で
> 考えることにします。
>
> 5分くらいで、それっぽい処理を書いてみました。
>
有り難う御座います!!!!!
ひとまず、ここに書いたらいいかな?という感じでスケッチを完成させて動作させてみましたら、何故か音が鳴らなくなりました。
それと、ファンクションボタンも反応しなくなって・・・。
因みに以下のスケッチになります。
何卒宜しくお願い申し上げます。
/**********************************************/
/*** MIDI shield controller ***/
/*** for Arduino ***/
/*** version 0.2 ***/
/*** 2013. 9. 3 created by Nekosan ***/
/*** 2013. 9. 4 modified 0.2 by Nekosan ***/
/*** ***/
/**********************************************/
/* declare constant values */
const int velocity_pin = 0; // potentiometer
const int pitch_bend_pin = 2; // vertical
const int modulation_pin = 1; // horizontal (cc#1)
const int led = 13;
const int note[13] = {60,61,62,63,64,65,66,67,68,69,70,71,72};
// 24::120
const int key[14] = {2,18,3,17,4,5,10,6,11,7,12,8,9 ,19};
// c c# d d# e f f# g g# a a# b c func
/* declare global variables */
int now_notes[14] = {1,1,1,1,1,1,1,1,1,1,1,1,1, 1};
int old_notes[14] = {1,1,1,1,1,1,1,1,1,1,1,1,1, 1};
/* active low */
int now_velocity;
int now_pitch_bend;
int now_modulation;
int old_pitch_bend;
int old_modulation;
/****************/
/* sub routines */
/****************/
void key_input(){
int i;
/* input keys */
for (i=0;i<14;i++){
now_notes[i] = digitalRead(key[i]);
}
now_velocity = 127;
now_pitch_bend = 64;
now_modulation = 0;
if (now_modulation < 0) {
now_modulation = 0;
}
}
void check_and_output(){
int i;
for (i=0;i<13;i++){
if (now_notes[i] == old_notes[i]) {
/* note is not changed */
} else {
/* check note on or off */
if (now_notes[i] == LOW) { // active low
note_on(0 , note[i] , now_velocity);
} else {
note_off(0 , note[i]);
}
}
}
}
void store_old(){
int i;
for (i=0;i<13;i++) {
old_notes[i] = now_notes[i];
}
old_pitch_bend = now_pitch_bend;
old_modulation = now_modulation;
}
void note_on(int channel ,int note ,int velocity) {
note_out(0x90+channel , note , velocity);
}
void note_off(int channel ,int note) {
note_out(0x80+channel , note , 0);
}
void pitch_bend(int channel) {
if (now_pitch_bend != old_pitch_bend) {
pitch_bend_out(channel ,0 ,now_pitch_bend);
}
}
void modulation(int channel) {
if (now_modulation != old_modulation) {
control_change(channel ,1 ,now_modulation);
//1 = modulation (MSB only)
}
}
void funtion_key(){
int i;
if (now_notes[13] == LOW) { // funcyion
program_change(0 ,109); //
}
}
/*****************************/
/* sub routines for midi-out */
/*****************************/
void note_out(int channel,int note,int velocity){
Serial.write(channel); // 0::15
Serial.write(note); // 24::120
Serial.write(velocity); //0:127
}
void pitch_bend_out(int channel,int LSB,int MSB){
Serial.write(0xe0 + channel);
Serial.write(LSB);
Serial.write(MSB);
}
void control_change(int CC,int LSB,int MSB){
Serial.write(0xb0 + CC);
Serial.write(LSB);
Serial.write(MSB);
}
// bank select test //
void bank_select(int ch, int msb, int lsb) {
Serial.write(0xB0 + ch);
Serial.write(0x00); //msb
Serial.write(msb);
Serial.write(0xB0 + ch);
Serial.write(0x20); //lsb
Serial.write(lsb);
}
void program_change(int ch, int pgm) {
Serial.write(0xC0 + ch);
Serial.write(pgm);
}
/**************/
/* main logic */
/**************/
void setup() {
int i;
Serial.begin(31250); // connect to MIDI synth
//Serial.begin(38400); //connect to PC
for (i=0;i<14;i++){
pinMode(key[i], INPUT);
}
pinMode(led, OUTPUT);
old_pitch_bend = analogRead(pitch_bend_pin) / 8;
old_modulation = analogRead(modulation_pin) / 4 - 128;
if (old_modulation < 0) {
old_modulation = 0;
}
}
void loop(){
/* input from keys and pots */
key_input();
/* function key */
funtion_key();
/* check push or release / output note on/off */
check_and_output();
/* pitch bend */
pitch_bend(0);
/* modulation */
modulation(0);
/* store "old notes" */
store_old();
bank_select(0, 0x3F, 0x01); // ch:0 msb:63 lsb:1
program_change(0, 109); // ch:0 pgm:109
for(;;){}
}
|
|