I'm building a midi controller with Arduino. In order to send messages I use commands like Serial.print(char(0x01));
As you can see, although the message itself is a hex value, it has to be sent in the char format.
With toggle switches that only know two states it was easy to do (only 0x01 or 0x7F - two values), but I haven't figured out how I can convert an analog value that I've read into a int variable with a potentiometer (127 different values possible).
I can do "Serial.print(hex(pot_value))", but it won't give me the result I need as it's in the wrong format.
I also tried "Serial.print(char(hex(pot_value)))" which didn't work either.
How do I cast a int value that I have into pure characters, so that I get it to do Serial.print(char(0xPot_hex_value_as_char))?
The character will never contain a hex value. Decimal, hex, and octal are only skinware representations of binary values. If you want to display the binary value of the lower byte in hex format:
If I had a switch that would just switch between the max/min value, I'd just send another Serial.print(char(...)) with the corresponding value. (I have switches on my midi controller as well, and they're working fine with this method)
Now since I'm using a potentiometer, I have to take the value that I get from there, use "map" to get a value between 0 and 127 and write this value in hex. (0x00 - 0x7F)
That sounds pretty simple. The point where I'm struggling is that if I use Serial.print(midiValue,HEX) the value is written as HEX, not as a char containing the HEX value as plain text (yes, I do realize that in the end it's all zero's & one's). So when I look at the serial monitor on my computer, I can see that the working Serial.print(char(...)) messages just get me a "°" where the print of the hex will return a value (00-7F).
I do need that value, but in the same format as the other stuff.
I tried it with lowByte, but my MIDI Monitor application that I use to debug the MIDI messages I'm sending shows me all kinds of weird information. I double checked with a commercial hardware midi controller with the same potentiometer and I'm getting correct data.
I also get correct values in my Arduino test program / serial console.
So I assume that I'm still not sending them in the correct format.
You explained that hexValue 0xFF and decimalValue 255 both have the same binary value 11111111.
But what's stored if I do 'char charValue = "0xFF" ' ?
I would assume that the binary string behind the char field is much longer. And that's what I'm sending out to my MIDI device. It works for the switches, so I assume that it will also work for the potentiometer.
So if I'm not completely on the wrong path - there's another cast / conversion that has to be done.
I just don't know how.
If I understood Tim correctly, then you should be able to put this program on your Arduino, run it and the serial console would be showing "°" only. Every second a new one...
But it doesn't. It alternates between "°" and "177". The 177 is produced by the lowByte print.
The "correct" way to send data if I want my MIDI device to understand what I'm doing produces the "°".
How do I get serial print to send the value in the correct format?