0
Online
Tesla Member
Karma: 82
Posts: 8195
:(){:|:&};:
|
 |
« Reply #15 on: April 27, 2012, 02:30:47 am » |
la Midi_Send non dovrebbe usare write e non println?
non puoi mettere "il char", poichè sono 2 char che vanno uniti assieme che rappresentano il tuo dato. il problema potrebbe essere che byte a ha il segno! quindi usa un int a, e poi fai return (byte)a
byte strConv(String str) { //Serial.println(str); String cmd=str; char stringa[5]; String mystr=cmd.substring(0,5); mystr.toCharArray(stringa,5);
diventa
byte strConv(String str) { char stringa[5]; str.toCharArray(stringa,5);
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #16 on: April 27, 2012, 03:02:02 am » |
Si Midi_Send usa write ma per le prove uso print per vedere il dato.
Ora provo con la modifica che mi hai consigliato
Grazie
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #17 on: April 27, 2012, 03:11:10 am » |
Grazie lesto per la pazienza e l'impegno che stai dimostrando  Ho provato ma non funziona. Qualsiasi valore che entro mi ritorna 10111111 cioè 191 cioè BF che nella tabella esadecimale corrispondono La conversione sembra funzionare ma risulta indifferente al dato che invio byte b1; byte b2; byte b3;
void setup() { Serial.begin(9600); }
void loop() {
String b = "0xB0"; String c = "0x02"; String d = "0x7F"; while (Serial.available()>0) { Serial.read(); b1=(strConv(b)); b2=(strConv(c)); b3=(strConv(d));
Serial.println(b3,BIN); Serial.println(b3,DEC); Serial.println(b3,HEX);
// Midi_Send(176,01,127); } } void Midi_Send(byte cmd, byte data1, byte data2) { Serial.write(cmd); Serial.write(data1); Serial.write(data2); }
byte strConv(String str) { char stringa[5]; str.toCharArray(stringa,5);
Serial.println(stringa[0]); Serial.println(stringa[1]); Serial.println(stringa[2]); Serial.println(stringa[3]); Serial.println(stringa[4]); Serial.println(stringa[5]);
byte a =0; if (stringa[3] >= '0' && stringa[3] <= '9'){ //estraggo 'B' a = stringa[3] - '0'; }else{ a = stringa[3] - 'A'; } a = a << 4; //muovo il valore di 4 bit a sinistra if (stringa[4] >= '0' && stringa[4] <= '9'){ //estraggo '1' a = stringa[4] - '0'; }else{ a = stringa[4] - 'A'; } return a; }
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #18 on: April 27, 2012, 03:15:00 am » |
B0 dovrebbe convertirlo in 176 cioè 10110000
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 82
Posts: 8195
:(){:|:&};:
|
 |
« Reply #19 on: April 27, 2012, 03:24:04 am » |
ci sono vari errori byte strConv(String str) { char stringa[4]; str.toCharArray(stringa,4);
Serial.println("0: "); Serial.println(stringa[0]); Serial.println("1: "); Serial.println(stringa[1]); Serial.println("2: "); Serial.println(stringa[2]); Serial.println("3: "); Serial.println(stringa[3]); Serial.println("4: "); Serial.println(stringa[4]);
byte a =0; if (stringa[2] >= '0' && stringa[2] <= '9'){ //estraggo 'B' a = stringa[2] - '0'; }else{ a = stringa[2] - 'A'; } Serial.println(a, BIN); a = a << 4; //muovo il valore di 4 bit a sinistra Serial.println(a, BIN); if (stringa[3] >= '0' && stringa[3] <= '9'){ //estraggo '1' a += stringa[3] - '0'; }else{ a += stringa[3] - 'A'; } Serial.println(a, BIN); return a; } correzioni: indici di array manca il += nella seconda if output migliorato allega l'output di una o più conversioni, grazie.
|
|
|
|
|
Logged
|
|
|
|
|
Cagliari
Offline
Faraday Member
Karma: 49
Posts: 3001
|
 |
« Reply #20 on: April 27, 2012, 03:24:11 am » |
|
|
|
|
|
Logged
|
IDE 1.0.4 - Arduino UNO R3 - Ethernet Shield R3 - DCF77 Module - Adafruit Data Loggin Shield - LCD 16x2 RGB positive backlight - i2c LCD backpack - RTC 1307 Board - Saleae Compatibile Logic Analyzer 8ch 24Mhz - ATtiny2313 ProtoBoard - Xino Board - ATtiny 4313 - Arduino 2009 - Arduino USB Serial Ligth - DS18B20 - LM35DZ - ShiftRegister - TL431 - ATtiny 85 Ultimi acquisti: CodeBender: http://codebender.cc/register/?referrer=PaoloP
|
|
|
|
0
Online
Tesla Member
Karma: 82
Posts: 8195
:(){:|:&};:
|
 |
« Reply #21 on: April 27, 2012, 03:25:47 am » |
ma così ogni char è un byte, mentre per trasformare da HEX a byte devi unire due char, poichè F = 1111
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #22 on: April 27, 2012, 03:36:45 am » |
con 0xB0 mi esce: 0:0 1:x 2:B 3: 4:U accentato 11001111 207 CF
Con 0x02 0:0 1:x 2:0 3: 4:U accentato 10111111 191 BF
Con 0x7F 0:0 1:x 2:7 3: 4:U accentato 101111 47 2F
|
|
|
|
|
Logged
|
|
|
|
|
Cagliari
Offline
Faraday Member
Karma: 49
Posts: 3001
|
 |
« Reply #23 on: April 27, 2012, 04:09:06 am » |
Nella stdlib.h c'è la funzione strtol /** The strtol() function converts the string in nptr to a long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace()) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).
The remainder of the string is converted to a long value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either upper or lower case represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.)
If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtol() stores the original value of nptr in *endptr. (Thus, if *nptr is not '\\0' but **endptr is '\\0' on return, the entire string was valid.)
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If no conversion could be performed, 0 is returned. If an overflow or underflow occurs, errno is set to avr_errno "ERANGE" and the function return value is clamped to LONG_MIN or LONG_MAX, respectively. */ extern long strtol(const char *__nptr, char **__endptr, int __base);
|
|
|
|
|
Logged
|
IDE 1.0.4 - Arduino UNO R3 - Ethernet Shield R3 - DCF77 Module - Adafruit Data Loggin Shield - LCD 16x2 RGB positive backlight - i2c LCD backpack - RTC 1307 Board - Saleae Compatibile Logic Analyzer 8ch 24Mhz - ATtiny2313 ProtoBoard - Xino Board - ATtiny 4313 - Arduino 2009 - Arduino USB Serial Ligth - DS18B20 - LM35DZ - ShiftRegister - TL431 - ATtiny 85 Ultimi acquisti: CodeBender: http://codebender.cc/register/?referrer=PaoloP
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #24 on: April 27, 2012, 04:15:34 am » |
Minchia Paolo mi hai aperto un mondo ............. che non capisco per niente 
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 82
Posts: 8195
:(){:|:&};:
|
 |
« Reply #25 on: April 27, 2012, 04:45:42 am » |
l'errore è nella trasformazione della String in array di char: 0:0 1:x 2:B 3: 4:U accentato che versione di IDE usi? per risolvere puoi provare ad eliminare char stringa[4]; str.toCharArray(stringa,4); e invece che usare stringa[numero] usare str.charAt(numero); poi, io ho fatto stampare a video 3 volte a in formato binario, dal tuo ouput invece vedo: 10111111 191 BF il primo valore sarà pure in binario, ma gli altri 2 no... che codice stai usando?
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #26 on: April 27, 2012, 05:28:08 am » |
Usavo ide 0023 ...... ora uso 1.0
|
|
|
|
« Last Edit: April 27, 2012, 05:31:19 am by m.alverman »
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #27 on: April 27, 2012, 05:42:04 am » |
Niente da fare  Ora uso ide 1.0 Se entro con 0xB0 ottengo: CHAR 2: B CHAR 3: 0 16 Questo è il Serial.print 10000 Questo è il Serial.print BIN 16 Questo è il Serial.print DEC 10 Questo è il Serial.print HEX #include <stdlib.h>
byte b1; byte b2; byte b3;
void setup() { Serial.begin(9600); }
void loop() {
String b = "0xB0"; String c = "0x02"; String d = "0x7F"; while (Serial.available()>0) { Serial.read(); b1=(strConv(b)); // b2=(strConv(c)); // b3=(strConv(d));
Serial.println(b1); Serial.println(b1,BIN); Serial.println(b1,DEC); Serial.println(b1,HEX);
// Midi_Send(176,01,127); } } void Midi_Send(byte cmd, byte data1, byte data2) { Serial.write(cmd); Serial.write(data1); Serial.write(data2); }
byte strConv(String str) { byte a =0;
Serial.print("CHAR 2: "); Serial.println(str.charAt(2)); Serial.print("CHAR 3: "); Serial.println(str.charAt(3));
if (str.charAt(2) >= '0' && str.charAt(2) <= '9'){ //estraggo 'B' a = str.charAt(2) - '0'; }else{ a = str.charAt(2) - 'A'; } a = a << 4; //muovo il valore di 4 bit a sinistra if (str.charAt(3) >= '0' && str.charAt(3) <= '9'){ //estraggo '1' a += str.charAt(3) - '0'; }else{ a += str.charAt(3) - 'A'; } return a; }
|
|
|
|
|
Logged
|
|
|
|
|
0
Online
Tesla Member
Karma: 82
Posts: 8195
:(){:|:&};:
|
 |
« Reply #28 on: April 27, 2012, 05:49:54 am » |
ok ci siamo quasi, mi sono dimenticato di aggiungere il 10:
nel primo if (ramo else) a = str.charAt(2) - 'A'; diventa a = str.charAt(2) - 'A' + 10;
e nel seocndo if a += str.charAt(2) - 'A'; diventa a += str.charAt(2) - 'A' + 10;
FATTO!
|
|
|
|
|
Logged
|
|
|
|
|
Cremona
Offline
Full Member
Karma: 2
Posts: 146
Arduino rocks
|
 |
« Reply #29 on: April 27, 2012, 06:51:25 am » |
lesto
MI TTI COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO !!!!!!!!
Non sai quanto sono felice cazzo E' da domenica che rompo le palle a destra e a sinistra e tu mi hai risolto un grandissimo problema. Per molti non sarà niente ma per me è tantissimo.
Non sai da quanto tempo volevo mettere quel [RISOLTO]
grazie per il tuo tempo prezioso speso per me
Alberto
|
|
|
|
|
Logged
|
|
|
|
|
|