Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« on: April 22, 2012, 09:36:01 am » |
Buon giorno. In base ad una selezione creo il comando da inviare alla funzione ma questo mi da un'errore in fase di compilazione SendCCMidi was not declared in this scope Mi date una mano ? Grazie .......
switch (string[0]) { case 'CC': int cmd1 = (int)(string[1]); int cmd2 = (int)(string[2]); int cmd3 = (int)(string[3]); SendCCMidi(cmd1,cmd2,cmd3); break; } void SendCCMidi(int cmd, int data1, int data2) { Serial.write(cmd); Serial.write(data1); Serial.write(data2); }
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Online
Brattain Member
Karma: 219
Posts: 16538
Don't know what I do
|
 |
« Reply #1 on: April 22, 2012, 09:55:53 am » |
Senza il resto dello sketch non si può dire se hai scritto giusto oppure no, dov'è questa funzione che chiami rispetto a setup() e loop() ecc...
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #2 on: April 22, 2012, 10:11:53 am » |
questo è tutto /*=========================== TITLE: Serial Echo Code v1.1 AUTHOR: Fayaz Kadir DESCRIPTION: If you send a string starting with "!" and ending with "*", the Arduino responds back with "$Y" for valid data string, or $N for invalid data string. You can modify this code for your use. COMPATIBLE BOARD: Arduino Mega (but any other Arduino should work fine) ============================*/
char string[25]; int i=0; int string_len; int inByte; int last_inByte; int counter=0;
void setup() { Serial.begin(9600); //INTIALISING THE MIDI SERIAL PORT Serial1.begin(9600); //INTIALISING THE CONTROLLER SERIAL PORT }
// La stringa in arrivo deve avere un ! per ogni lettera e deve terminare con un * // All'inizio va raddoppiato !! altrimenti perdo la prima lettera // Esempio !!P!R!O!V!A*
void loop() // LOOP FUNCTION { inByte = Serial1.read(); string_len=0; if (inByte == '!') read_serial(); // If Start of line ("!") is found, call read_serial() function }
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE { Serial.println ("SOL FOUND and reading"); // THE SOL (Start of Line) found notice. Serial1.print("READ : !\n"); //Saying that it has read "!" on the serial port while (inByte!= '*') // As long as EOL not found, keep reading if (Serial1.available() > 0) // if new data is available { inByte = Serial1.read(); // Read new byte Serial.print("READ : "); // Display the new byte string[string_len] = inByte; // Save the data in a character array Serial.println(string[string_len]); // Print the characters that was recieved string_len++; } else if (Serial1.available() == 0) { Serial.println("EOL not available, data string invalid"); // If EOL not in the string Serial1.print("$N"); //FAIL SIGNATURE break; }
if (inByte == '*') { Serial1.println ("$Y"); //SUCCESS SIGNATURE for (int i=0; i <= (string_len-2); i++){ Serial.print (string[i]); }
Serial.print ("\n"); switch (string[0]) { case 'CC': int cmd1 = (int)(string[1]); int cmd2 = (int)(string[2]); int cmd3 = (int)(string[3]); SendCCMidi(cmd1,cmd2,cmd3); break; } } void SendCCMidi(int cmd, int data1, int data2) { Serial.write(cmd); Serial.write(data1); Serial.write(data2); } }
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #3 on: April 22, 2012, 10:16:38 am » |
Praticamente ho un'arduino mega con montato lo shield midi. Sulla seriale1 ricevo i dati dal pc e in base a quello che mando creo il comando midi da unviare sulla seriale0
Poi perchè se tolgo tutti i Serial.print, non i Serial1, la stringa non si compone ? Devo per forza stampare su Serial per storare il dato da Serial.read ?
Grazie ancora Alberto
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Online
Brattain Member
Karma: 219
Posts: 16538
Don't know what I do
|
 |
« Reply #4 on: April 22, 2012, 10:30:24 am » |
Hai chiuso male le varie funzioni. In pratica dopo SendCCMidi ci sono 2 "}" mentre una di esse dovrebbe stare prima, perché la funzione SendCCMidi deve stare fuori da loop(). Ogni funzione è a sé stante, non puoi incapsularla all'interno di un'altra. Cioè così: /*=========================== TITLE: Serial Echo Code v1.1 AUTHOR: Fayaz Kadir DESCRIPTION: If you send a string starting with "!" and ending with "*", the Arduino responds back with "$Y" for valid data string, or $N for invalid data string. You can modify this code for your use. COMPATIBLE BOARD: Arduino Mega (but any other Arduino should work fine) ============================*/
char string[25]; int i=0; int string_len; int inByte; int last_inByte; int counter=0;
void setup() { Serial.begin(9600); //INTIALISING THE MIDI SERIAL PORT Serial1.begin(9600); //INTIALISING THE CONTROLLER SERIAL PORT }
// La stringa in arrivo deve avere un ! per ogni lettera e deve terminare con un * // All'inizio va raddoppiato !! altrimenti perdo la prima lettera // Esempio !!P!R!O!V!A*
void loop() // LOOP FUNCTION { inByte = Serial1.read(); string_len=0; if (inByte == '!') read_serial(); // If Start of line ("!") is found, call read_serial() function }
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE { Serial.println ("SOL FOUND and reading"); // THE SOL (Start of Line) found notice. Serial1.print("READ : !\n"); //Saying that it has read "!" on the serial port while (inByte!= '*') // As long as EOL not found, keep reading if (Serial1.available() > 0) // if new data is available { inByte = Serial1.read(); // Read new byte Serial.print("READ : "); // Display the new byte string[string_len] = inByte; // Save the data in a character array Serial.println(string[string_len]); // Print the characters that was recieved string_len++; } else if (Serial1.available() == 0) { Serial.println("EOL not available, data string invalid"); // If EOL not in the string Serial1.print("$N"); //FAIL SIGNATURE break; }
if (inByte == '*') { Serial1.println ("$Y"); //SUCCESS SIGNATURE for (int i=0; i <= (string_len-2); i++){ Serial.print (string[i]); }
Serial.print ("\n"); switch (string[0]) { case 'CC': int cmd1 = (int)(string[1]); int cmd2 = (int)(string[2]); int cmd3 = (int)(string[3]); SendCCMidi(cmd1,cmd2,cmd3); break; } } }
void SendCCMidi(int cmd, int data1, int data2) { Serial.write(cmd); Serial.write(data1); Serial.write(data2); }
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #5 on: April 22, 2012, 11:07:31 am » |
Grazie  Mi sapresti dire anche qualcosa sulla seconda domanda, che ho postato mentre rispondevi ? TNX; alberto
|
|
|
|
|
Logged
|
|
|
|
|
Forum Moderator
Italy
Online
Brattain Member
Karma: 219
Posts: 16538
Don't know what I do
|
 |
« Reply #6 on: April 22, 2012, 11:39:21 am » |
Non ho capito il tuo problema. Scusa.
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #7 on: April 23, 2012, 10:27:11 am » |
Help me, please Da VB invio una stringa così: "!!CC!0xB0!0x01!0x7F!*" Per il momento tralascio il primo CC lo usero più avati Alla fine in arduino la tasformo in tre Stringhe Comando Data1 Data2 Quando li stampo dal monitor seriale vedo perfettamente: 0xB0 0x01 0x7F Ora quello che non so fare è come inviare le 3 stringhe in 3 byte con serial.write nelle ultime 3 righe commentate. Mi date una mano ? Grazie, Albverman char string[25]; int i=0; int string_len; int inByte; int last_inByte; int counter=0;
String TipoComando; String Comando; String Data1; String Data2;
void setup() { Serial.begin(9600); //INTIALISING THE MIDI SERIAL PORT Serial1.begin(9600); //INTIALISING THE CONTROLLER SERIAL PORT }
void loop() // LOOP FUNCTION { inByte = Serial1.read(); string_len=0; if (inByte == '!') read_serial(); // If Start of line ("!") is found, call read_serial() function }
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE { Serial.println ("SOL FOUND and reading"); // THE SOL (Start of Line) found notice. Serial1.print("READ : !\n"); //Saying that it has read "!" on the serial port while (inByte!= '*') // As long as EOL not found, keep reading if (Serial1.available() > 0) // if new data is available { inByte = Serial1.read(); // Read new byte Serial.print("READ : "); // Display the new byte string[string_len] = (int)inByte; // Save the data in a character array Serial.println(string[string_len]); // Print the characters that was recieved string_len++; } else if (Serial1.available() == 0) { Serial.println("EOL not available, data string invalid"); // If EOL not in the string Serial1.print("$N"); //FAIL SIGNATURE break; }
if (inByte == '*') { Serial1.println ("$Y"); //SUCCESS SIGNATURE
Comando=((String)string[3]+(String)string[4]+(String)string[5]+(String)string[6]); Data1=((String)string[8]+(String)string[9]+(String)string[10]+(String)string[11]); Data2=((String)string[13]+(String)string[14]+(String)string[15]+(String)string[16]); Serial.println(Comando); Serial.println(Data1); Serial.println(Data2);
// Serial.write(Comando); // Serial.write(Data1); // Serial.write(Data2); } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 83
Posts: 8243
:(){:|:&};:
|
 |
« Reply #8 on: April 23, 2012, 10:33:49 am » |
Serial.print(Comando, HEX);
non è il valore in hex, ma la sua rapresentazione ascii (ovvwro leggibile da un umano). La write scrive il vero e proprio byte in 1 e 0, quindi perde di significato se dire binario, hex o altro
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #9 on: April 23, 2012, 10:36:40 am » |
Uso arduino ide 1.0 Mi da errore: no matching function for call to 'HardwareSerial::write(String&,int)'
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 83
Posts: 8243
:(){:|:&};:
|
 |
« Reply #10 on: April 23, 2012, 10:39:11 am » |
sei stato più veloce della mia edit, è print non write
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #11 on: April 23, 2012, 10:41:12 am » |
Ma io dovrei mandare il byte alla periferica midi quindi devo usare write. Sa fò ? 
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #12 on: April 23, 2012, 10:42:23 am » |
Io dovrei mandare l'equivalente:
Serial.wryte(0xB0); Serial.write(0x01); Serial.write(0x7F);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 83
Posts: 8243
:(){:|:&};:
|
 |
« Reply #13 on: April 23, 2012, 10:47:44 am » |
se non erro la write manda un byte solo, mentre stai cercando di inviare valori a 2 byte. quindi scomponili: int invio = 0X16; serial.write(invio<<  ; serial.write(invio); comunque cerca nel reference, c'è scritto tutto
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #14 on: April 23, 2012, 12:03:01 pm » |
Scusa per la domanda ma sono in confusione
se ho una stringa che contiene "0xB0" quanti byte sono ?
|
|
|
|
|
Logged
|
|
|
|
|
|