Multiple Sensors to Print to Serial Monitor

Hey everyone,

I would really appreciate some help getting to print all of my sensors to the serial monitor. I have individually tested all of them and had them print to the serial monitor.

Now that I have combined most of them, I am having trouble getting the code to write all the values to the serial monitor.

The sensors to be read on the serial monitor are the HIH 6130 Temp&Humidity Sensor, the value of the pressure sensor (pressureValue), and the value of the rotary encoder (encoderValue) if possible.

Any guidance is helpful.

Here is my code:

#include <Servo.h>
#include <Wire.h> //I2C library

#define TRUE 1
#define FALSE 0
#define PRESSURE_PIN A1

Servo CRservo, INTAKEservo, EXHAUSTservo; //create servo object to control a servo

int pos=0; //variable to store the servo position
int encoderValue;
float pressureValue;
int ADCvalue;

byte fetch_humidity_temperature(unsigned int *p_Humidity, unsigned int *p_Temperature);
void print_float(float f, int num_digits);

void readEncoder()
{

encoderValue=analogRead(A0);
}

void readPressure()
{

ADCvalue=analogRead(PRESSURE_PIN);
pressureValue= 0.000145037738 * ((ADCvalue/1024.0)+0.095)/0.000009;
}

void setup()
{

CRservo.attach(9);
INTAKEservo.attach(11);
EXHAUSTservo.attach(6);

INTAKEservo.write(100);
delay(5000);
EXHAUSTservo.write(100);
delay(2000);

Wire.begin();
Serial.begin(9600);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH); // this turns on the HIH3610
delay(1000);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>"); // just to be sure things are working

}

void loop()
{
byte _status;
unsigned int H_dat, T_dat;
float RH, T_C;

while(1)
{
_status = fetch_humidity_temperature(&H_dat, &T_dat);

switch(_status)
{
case 0: Serial.println(“Normal.”);
break;
case 1: Serial.println(“Stale Data.”);
break;
case 2: Serial.println(“In command mode.”);
break;
default: Serial.println(“Diagnostic.”);
break;
}

RH = (float) H_dat * 6.10e-3;
T_C = (float) T_dat * 1.007e-2 - 40.0;

print_float(RH, 1);
Serial.print(" ");
print_float(T_C, 2);
Serial.println();
delay(1000);
}
}

byte fetch_humidity_temperature(unsigned int *p_H_dat, unsigned int *p_T_dat)
{
byte address, Hum_H, Hum_L, Temp_H, Temp_L, _status;
unsigned int H_dat, T_dat;
address = 0x27;;
Wire.beginTransmission(address);
Wire.endTransmission();
delay(100);

Wire.requestFrom((int)address, (int) 4);
Hum_H = Wire.read();
Hum_L = Wire.read();
Temp_H = Wire.read();
Temp_L = Wire.read();
Wire.endTransmission();

_status = (Hum_H >> 6) & 0x03;
Hum_H = Hum_H & 0x3f;
H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;
T_dat = (((unsigned int)Temp_H) << 8) | Temp_L;
T_dat = T_dat / 4;
*p_H_dat = H_dat;
*p_T_dat = T_dat;
return(_status);
}

void print_float(float f, int num_digits)
{
int f_int;
int pows_of_ten[4] = {1, 10, 100, 1000};
int multiplier, whole, fract, d, n;

multiplier = pows_of_ten[num_digits];
if (f < 0.0)
{
f = -f;
Serial.print("-");
}
whole = (int) f;
fract = (int) (multiplier * (f - (float)whole));

Serial.print(whole);
Serial.print(".");

for (n=num_digits-1; n>=0; n–) // print each digit with no leading zero suppression
{
d = fract / pows_of_ten[n];
Serial.print(d);
fract = fract % pows_of_ten[n];
}

// read input
readEncoder();

//processing
if (encoderValue < 50 )
{
//stop servo
CRservo.write(94);

}
else if (encoderValue < 512 )
{
//send 120 degrees to servo to get it moving counterclockwise
CRservo.write(120);
}
else if (encoderValue < 920) {

CRservo.write(60);
}
else {

CRservo.write(94);
}
//output
Serial.println(encoderValue);
Serial.print(pressureValue);
Serial.println();
delay(100); //delay in between reads for stability

}

This is what the Serial Monitor is reading to me:

Normal.
66.2371
0.00
20.08382
0.00

Normal.
66.5387
0.00
20.08387
0.00

I know the two numerical values are for the HIH 6130 Temp&Humidity sensor. I don’t know what the two zero’s are for.

Thanks for any help.

I was able to get my pressure sensor to send its value to the serial monitor, still trying to get the encoder to do the same. Here is what the serial monitor looks like now.

Normal. 64.4403 14.64 19.91403 14.64

The first number is humidity, below that is pressure in PSI, then temperature, and last is pressure again.

This is the change to the last portion of the code I implemented

 //output
  readEncoder();
  Serial.println(encoderValue);
  readPressure();
   Serial.println(pressureValue);
  
  delay(100);  //delay in between reads for stability
/code]