Array of strings to float

Hello!

I’m using an Arduino 1 board to communicate with SC. I had no issue since following some tutorials, but when I try to make a float out of a normalised value I encounter some issues using the .asFloat method.

(
~intArray= [ ];
Tdef(\readValues, {
	{
		~ascii = ~port.read;
		case
		
		{~ascii == nil} {nil}

		{~ascii.asAscii.isDecDigit }
		{~intArray = ~intArray.add(~ascii.asAscii.digit)}
		
		{~ascii.asAscii.isPunct}
		{~intArray = ~intArray.add(~ascii.asAscii)}

		{~ascii.asAscii.isAlpha}
		{
			~val = ~intArray;
			~intArray = [ ];
		}

		{true} {nil};
		}.loop;
}).play;
)

I get an array like [0, ., 0, 7] for example. I know the method .convertDigit would create a number if there was no point in it, I can’t find anything like that for float since .asFloat doesn’t convert it.
The code for Arduino sends the value (0.07) and then a character ‘a’ to be able to separate them here.

Any ideas on how to make this work?
Thanks

Cheers

One thing in your code is that you’re interested in the characters, but keep ~ascii as an integer. I’d just put it into char space if it’s not nil. Then you get String:asFloat for free.

(
~string = String.new;
Tdef(\readValues, {
	{
		~ascii = ~port.read;

		if(~ascii.notNil) {
			~ascii = ~ascii.asAscii;

			case

			{ ~ascii.isDecDigit or: { ~ascii.isPunct } } {
				~string = ~string.add(~ascii)
			}
		
			{ ~ascii.isAlpha } {
				~val = ~string.asFloat;
				~string = String.new;
			}

			// {true} {nil};  // redundant, just delete
		}
	}.loop;
}).play;
)

hjh

Hey, thanks! That was it!