Float to Int for sending over serial

    May 16 2021 | 4:58 pm
    Hey, I've run into an issue in a project where I'm trying to send a list of floats from max to arduino. The data is a list of FFT bin amplitudes generated from the fft.list and fft.normalize objects in the fft tutorials https://cycling74.com/articles/fft-part-5.
    I've been able to send data from max to arduino over serial, but only when the data are integers, but the FFT bins are floats - and while I get some data into arduino, it doesn't correspond to the digits I see in max and I don't know how to make sense of it. I've been reading a few threads, which suggested instead to convert the data to ints in max before it's sent which seems like a sensible strategy. I can edit the fft.normalize object to convert the range to 0-100 for a sensible range for whole-number amplitude values, but I don't know how to convert the float list to an int list in max.
    Would anyone have any suggestions on how to solve this? or alternatively how to interpret float data sent from max over serial?
    thanks Jess

    • May 17 2021 | 12:28 pm
      Here one example using start / end markers to form data string
      I used < and > as Markers
      const byte numItems = 255; char receivedItems[numItems]; boolean newMessage = false;
      void setup() {Serial.begin(115200);} void loop() {stringWithMarks(); showNewMessage();}
      void stringWithMarks() {static boolean Receiving = false; static byte index = 0; char startMark = '<'; char endMark = '>'; char bag; while (Serial.available() > 0 && newMessage == false) {bag = Serial.read(); if (Receiving == true) {if (bag != endMark) {receivedItems[index] = bag; index++; if (index >= numItems) {index = numItems - 1;}} else {receivedItems[index] = '\0'; Receiving = false; index = 0; newMessage = true;}} else if (bag == startMark) {Receiving = true;}}}
      void showNewMessage() {if (newMessage == true) {Serial.println(receivedItems);newMessage = false;}}
      Int or float, no matter, you also don't have to reduce float precision Just change port to your board
    • May 17 2021 | 2:17 pm
      ah terrific thanks, that's worked. in case it's useful to anyone else, I added the t <> and atoi objects to the list coming out of fft.list before sending over serial and the numbers received into arduino make sense now.
      thanks again for your help, much appreciated
    • May 17 2021 | 2:53 pm
      just use the max patch I uploaded, there is t > l < and itoa inside