Serial communication with DAC through Max 6

Hello.

I am trying to use an Arduino board to monitor the voltage of two MCP 4725 DAC's with different values.

I input the value through a Max patcher (Max 6, from https://docs.cycling74.com/max5/tutorials/max-tut/communicationschapter02.html) and transmit it to the Arduino board.Then it translates the 0-255 we enter in Max into DAC values for the setvoltage(value) method (0-4095).

My first issue is that i can't assign a value in my loop. We could simply do (0 to 255) * 16,06 = (0 to 4095) obviously but i had to do that huge ugly switch case.

The second one is that i can't find a way to store 2 different values from Max.

Here is my code if it helps :

void loop(void) {

unsigned long currentMillis = millis();

if(Serial.available()) // check to see if there's serial data in the buffer
{ 
   serialvalue = Serial.read(); // read a byte of serial data
} if(currentMillis - previousMillis > interval) { 
    switch(serialvalue){ 
          case 255: 
            dac1.setVoltage(4095,false); 
            dac2.setVoltage(4095,false); 
         break; ... etc.

Our final idea is to be able to monitor 4 DAC's with Max at different voltages. I've beeen trying to go throng Serial.read() and Serial.readBytes() functions without success.

Thanks for your help.

Reading single bytes from Serial won't help in controlling multiple outputs. Define a command format, that includes a DAC number and value. Then read entire command lines, and decode the contained values. Then send the value to the given DAC.

Your mapping function is incorrect, the factor is exactly 4096/256=16.

Thank you for your help.

Our idea is to be able to send 0-4095 range values instead of 0-255 to at least one DAC (through the Max patcher from the link above). That would be easy to do from the IDE terminal but we need to do it through Max (i.e. Serial.read() ). I tried to send "40" then "95" with a boolean for example but it doesn't work.

How do you define a command format ? Do you mean declaring a function ?

I tried to google around but didn't find anything.

Regarding the mapping i got it working with map().

As an IT student you should know about the binary representation of information. Numbers can be transmitted e.g. in binary encoding (bytes), or as text (characters), where the lowest level of a serial protocol transmits a number of bits within a certain time frame (asynchronous), or in sync with a clock signal (synchronous, SPI, I2C...). The next protocol level defines the begin, length or end of a transmission, e.g. transmission of a line of text terminated by CR and/or LF. Then the sender converts the information into a sequence of bytes, and the receiver parses that stream and extracts the contained information.

For the transmission of numbers you can use e.g. Serial.print() and parseInt(), see the forum Reference on the Serial or Stream library.

I know that :)

I cannot use Serial.print() to send information. My "sender" is a box in the Max patcher. So far Serial.read() is able to read a number up to 255 but i'd like a higher resolution for the DAC.

I tried sending "4095" or a list ("4" "0" "9" "5") from Max and store it then convert it in Arduino without success.

Find a method to send strings from the Max.

That’s what i am trying at the moment, but it doesn’t return the correct value to the DAC when i send “4 0 9 5” to get 5V.

void loop(void) {

  if(Serial.available() >= 4){
    n1 = Serial.read();
    n2 = Serial.read();
    n3 = Serial.read();
    n4 = Serial.read();
  }
value = n1*1000+n2*100+n3*10+n4;
dac1.setVoltage(value,false);
}
value = n1*1000+n2*100+n3*10+n4;

Is not going to work.

Think about what each byte represents.