Arduino digitalRead to Max/msp with SerialReceive

Qbrick's icon

I have a DHT11 temp/humidity sensor connected to my arduino. I've have correct readout to my arduino serial monitor.

Now I want to route that data to Max/msp thru SerialReceive. I have connection to my arduino and it gives me a bang on the same time I get an update of the data read of my arduino. Only thing is I don't get the same data in Max/msp. How do I translate the data from arduino to the same data in Max/msp in a constant way.
Is there another way to achieve a more solid reading or is this Arduino sided problem.

code Arduino:

#include "dht.h" //library

dht DHT;

#define DHTPIN 2

int VTemperature = 2;
int VHumidity = 2;

void setup(){
Serial.begin(9600);
}

void loop()
{
int chk = DHT.read11(DHTPIN);
VTemperature = digitalRead(DHT.temperature);
Serial.println(DHT.temperature);
VHumidity = digitalRead(DHT.humidity);
Serial.println(DHT.humidity);

Serial.print("\r");
delay(1000);

}
Patch Max/Msp

Max Patch
Copy patch and select New From Clipboard in Max.

Scott Fitzgerald's icon

In your Arduino code, only send a Serial.println() when you're ready to read the data from the sensor in Max. You can use the newline character to identify the end of a packet.

#include "dht.h" //library

dht DHT;

#define DHTPIN 2

int VTemperature = 2;
int VHumidity = 2;

void setup(){
Serial.begin(9600);
}

Max Patch
Copy patch and select New From Clipboard in Max.

void loop()
{
int chk = DHT.read11(DHTPIN);
VTemperature = digitalRead(DHT.temperature);
Serial.print(DHT.temperature);
VHumidity = digitalRead(DHT.humidity);
Serial.println(DHT.humidity);
delay(1000);
}

Qbrick's icon

I'm not following scott.

hz37's icon

Hold on... this is not right:

VTemperature = digitalRead(DHT.temperature);

You do not do a digitalRead() from a member of the DHT struct. It is already read by then. And besides, digitalRead() only hands you a LOW or HIGH. You want an analog-ish value from your sensor.

int chk = DHT.read11(DHTPIN);

That already polls the sensor for both humidity and temperature. You can then just cast them to what you need:

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);

But if you want a more handy value for use in Max, use the Arduino map() function.
// Scale to 7 bits.
char output = map(DHT11.humidity, 0, maxReadHumidity, 0, 127);
// Output the number in 7 bit range.
Serial.print(output);

...where maxReadHumidity is the max you ever get from the humid sensor. First print to the serial monitor and see what you get, then map to [0..127]. You can do the same for the temp. Good luck and have fun!

Hens Zimmerman

Qbrick's icon

Ok, I went a little further and think I got what I want so far. I'm not sure if all the data is actually correct.

For the max side I have a new problem, I get my data but the unpacking is not correct, though it tries to keep it correct I guess.

whey you look at the pictures you see the data unpack differences (last one is the correct reading according to arduino's listing).

arduino code:

Max Patch
Copy patch and select New From Clipboard in Max.

//Libraries first
#include "dht.h"    //DHT11 library

dht DHT;

#define DHTPIN 2

const int VerticalRainSensor = 0;           // const int for millis delay for Rainsensor vertical
const int HorizontalRainSensor = 1;         // const int for millis delay for Rainsensor horizontal
const int MoistureSensor = 2;               // const int for millis delay for Moisture
const int HumidityTemperatureSensor = 2;    // const int for millis delay for Humidity
const int TemperatureHumiditySensor = 2;    // const int for millis delay for Temperature
const int DayNightSensor = 3;               // const int for millis delay for Day/Night 

void setup(){
  
  Serial.begin(9600);
  
  
}

void loop(){
  
  int chk = DHT.read11(DHTPIN);
  
  int ValVRS = analogRead(A0);
  int ValHRS = analogRead(A1);
  int ValMS  = analogRead(A2);
  int ValHTS = digitalRead(DHT.humidity);
  int ValDNS = digitalRead(DHT.temperature);
  
  
  
 Serial.print(DHT.humidity);
 Serial.print(" ");
 Serial.print(DHT.temperature);
 Serial.print(" ");
 Serial.print(ValVRS);
 Serial.print(" ");
 Serial.print(ValHRS);
 Serial.print(" ");
 Serial.print(ValMS);
 
 
 
  
  delay(2000);
  
  
}


max patch:


wrong11.png
png
wrong21.png
png
wrong31.png
png
Correctish11.png
png
drewmcm's icon

Hi There
Just wondering did you manage to get this resolved?

I am trying to do exactly the same thing. Would love to know

Thanks
Drew

Source Audio's icon

Why would one expect to get group of 5 ints out of zl group in Max
without sending println from arduino at the end of transmission ?
That is really basic stuff for arduino -> max data.
By chance in the above patch zl group was set to 78 which
gets very soon filled up with that clumsy serial report of data.
so after zl group gets emptied, next 78 received ints are collected,
and the first 5 get out of unpack, very funny