Serial Print a number of int values

Hi there,

As I was working on the code for my latest project this morning, I have run into an obstacle; maybe it is really easy to fix and I just missed the solution or maybe it is complicated. Hopefully, you can tell me!

Basically, I have 7 integer values read from 7 different analog pins plus a temperature and humidity reading from a DHT-22.

Once the board reads the sensor values, I'll just make up some values for the purpose of demonstration, I want it to print to the serial port the following (note: the DHT-22 outputs an actual value for the temp and humidity so it would be like 15.4256 and I want it rounded to 2 decimal places)

15.43,27.52,1020,45,698,365,189,78,0

Where the first is temp, second is humidity, and the rest are variables.

//


#include <dht.h>

dht DHT;


#define DHT22_PIN 10  //digital 10, that is
// Constants that also define Pins
const int methanePin = A1;
const int propanePin = A2;
const int comonoxPin = A3;
const int nitdioxPin = A4;
const int ammoniaPin = A5;
const int codioxPin = A6;
const int ozonePin = A7;

// Variables to store sensor values in
int methane = 0;
int propane = 0;
int comonox = 0;
int nitdiox = 0;
int ammonia = 0;
int codiox = 0;
int ozone = 0;
int error = 0;
int timer = 100;
int errdelay = 2500;



void setup()
{
  Serial.begin(9600);
  
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);      
  }
}


void loop()
{
  // READ DATA
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
                error = 0;
                break;
    case DHTLIB_ERROR_CHECKSUM:
                error = 1;
                break;
    case DHTLIB_ERROR_TIMEOUT:
                error = 2;
                break;
    default:
                error = 3;
                break;
  }
  
  if (error = 0) {
     for (int thisPin = 2; thisPin < 8; thisPin++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 7; thisPin >= 2; thisPin--) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
  }
  
  else {
    if (error = 1) {
      
        digitalWrite(2, HIGH);
      digitalWrite(8,HIGH);
      delay(errdelay);
      digitalWrite(8, LOW);
      digitalWrite(2, LOW);
      delay(errdelay);
      }
      
      else {
        if (error = 2) {
          
            digitalWrite(2, HIGH);
            digitalWrite(5, HIGH);
            digitalWrite(8, HIGH);
            delay(errdelay);
            digitalWrite(2, LOW);
            digitalWrite(5, LOW);
            digitalWrite(8, LOW);
          }
          
          else {
            if (error = 3) {
             
                digitalWrite(2, HIGH);
                digitalWrite(4, HIGH);
                digitalWrite(6, HIGH);
                digitalWrite(8, HIGH);
                delay(errdelay);
                digitalWrite(2, LOW);
                digitalWrite(4, LOW);
                digitalWrite(6, LOW);
                digitalWrite(8, LOW);
                delay(errdelay);
              }}}}
 
 methane = analogRead(methanePin);
 propane = analogRead(propanePin);
 comonox = analogRead(comonoxPin);
 nitdiox = analogRead(nitdioxPin);
 ammonia = analogRead(ammoniaPin);
 codiox = analogRead(codioxPin);
 ozone = analogRead(ozonePin);
  
  
  // DISPLAY DATA
  
  
  
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(1000);
}

 

//
// END OF FILE
//

You will note that the serial print for the Temp and Humidity is found at the bottom of the code, but I want to print all of those int values with the temp/humidity all in one comma separated chunk.

How exactly would I do this?

Thanks in advance

Either one Serial,print() statement per variable, or use sprintf(). One print per variable is easier.

By the way, why are you assigning all those values to error inside if statements?

I'd get rid of your individual named variables, and use an array, naming the array indices instead.
And, as PaulS said, try == instead of = in your ifs.

So == will fix that?

Yes.

Also, could you refer me to an example of an array and array indices?

Beyond http://arduino.cc/en/Reference/Array?

I will eventually be feeding this serial stream into a LabJack for interpretation by DaqFactory if that helps with anything.

No. Serial output is serial output.

how do I read the value of each pin in the array (a variable)

Using a for loop.

byte somePins[] = { 5, 8, 10, 11, 13 };
const byte pinCount = sizeof(somePins) / sizeof(somePins[0]); // Count the pins
byte pinVals[pinCount];

void loop()
{
   for(byte p=0; p<pinCount; p++)
   {
      pinsVals[p] = digitalRead(somePins[p]);
   }
}

and subsequently print the values?

Not surprisingly, the same way.

Also, how do I get the DHT-22 values into the array since they are a part of the library?

The values are not part of the library. The values are outputs from library methodss, just like the values output by the digitalRead() function.

So your example code is for a digitalRead; will it work with analogRead?

With different pin numbers, yes. It was just an example of using an array.

Also, even though the DHT has a library, how do I incorporate that into the array?

Why would you? You only appear to have one DHT.

Lastly, how do I make sure that the values are truncated to a reasonable amount of decimal places on the DHT values? Normally it would be (DHT.temperature, 2);

Normally, it would be Serial.print(DHT.temperature, 2);. So, what's wrong with that?

I'm not sure about the advantage of an array when all I really want is serial.Print(DHT.temperature,DHT.humidity,a,b,c,d,e,f,g);

You may want to do that, but without any methods to do it, the array will probably produce the most compact code.

So what would be the proper syntax to serial.Print multiple variables?

There is no Serial method that prints an arbitrary number of variables.
A simple for loop and an array would do pretty much what you want.

I don't see any problem here - you may be over thinking the issue.

I want it all in one line of data

so, only call println after the last item.

The float values can be truncated by assignment to an integer data type, or rounded, by adding a half and then assigning to an integer.

I would be more open to doing that if I knew how to incorporate and truncate the two values from the DHT-22 into the array. So it would be analog pins 1-7 and then the two DHT values.

The array of analogRead() results will contain ints. You can't add floats to an int array, without losing accuracy.

What it the problem with Serial.print()ing the two floats and then the analogRead values from the array?

I seem to be leading myself in circles...

Yes.

The program I am sending this serial data to would behave better if I could send one block of data at every time interval.

I honestly can't see what is preventing you from doing this.

STS_MR2:
The program I am sending this serial data to would behave better if I could send one block of data at every time interval. And I would like it to be sent all at once, not one after another followed by the array.

So the receiving device can't cope with this:

Serial.print("a");
Serial.print("b");

and requires this instead:

Serial.print("ab");

?

Arduino can only print one item at a time.

So your code will look like

Serial.print( DHT.temp );
Serial.print( DHT.humidity );
Serial.print(a);
Serial.print(b);
Serial.print(c);
Serial.print(d);
Serial.println(e) ;

This should print all your items on one line.

Arduino can only print one item at a time.

So your code will look like

Serial.print( DHT.temp );
Serial.print( DHT.humidity );
Serial.print(a);
Serial.print(b);
Serial.print(c);
Serial.print(d);
Serial.println(e) ;

This should print all your items on one line.

With no separators to define where one value ends and the next begins. Keep that in mind when you go to parse the data on the other end.

The program I am sending this serial data to would behave better if I could send one block of data at every time interval. And I would like it to be sent all at once, not one after another followed by the array.

The device on the other end can not tell whether one Serial.print() occurred, or 27.

What you would like can be accomplished if you use sprintf() to create a string to send. In the end, though, it will make no difference with respect to the data put on, or pulled off, the serial port.

const char FIELD_SEP = ',';

Serial.print( DHT.temp );
Serial.print( FIELD_SEP );
Serial.print( DHT.humidity );
Serial.print( FIELD_SEP );
Serial.print(a);
Serial.print( FIELD_SEP );
Serial.print(b);
Serial.print( FIELD_SEP );
Serial.print(c);
Serial.print( FIELD_SEP );
Serial.print(d);
Serial.print( FIELD_SEP );
Serial.println(e) ;

Which, btw, screams for the use of an array :stuck_out_tongue_winking_eye:

Which, btw, screams for the use of an array :stuck_out_tongue_winking_eye:

See reply #2