Hello, I am new to Arduino and was encountering issues with this program. This program is supposed to be able to poll data whenever b, h, or t is pressed. My program does compile but whenever I send these commands it just outputs 2 blank lines. What could be causing these issues?
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9 // Lower resolution
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
DHT dht(DHTPIN, DHTTYPE);
int numberOfDevices;
void setup() {
Serial.begin(9600);
Serial.println("Temp Probe");
sensors.begin();
dht.begin();
numberOfDevices = sensors.getDeviceCount();
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
for (int i = 0; i < numberOfDevices; i++)
{
if (sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}
else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void printTemp (DeviceAddress = 0xA6)// Potential addresses 0x28 '0x5A' '0x4C' '0x05' '0x0D' '0x00' '0x00' '0xA6')
{
Serial.print("Metal temp probe: ");
float tempC = sensors.getTempC(0x28);
if (tempC == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
return;
}
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC));
return;
}
void humidityAndTemp() {
sensors.requestTemperatures();
for (int i = 0; i < numberOfDevices; i++)
{
if (sensors.getAddress(tempDeviceAddress, i))
{
DeviceAddress(tempDeviceAddress);
}
}
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Temp + humidity sensor: ");
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.println(F("°F"));
Serial.println("///////////////////////////////////////////////////////////////////////////");
return;
}
void loop()
{
if (Serial.available() > 0)
{
char ComReading;
ComReading = Serial.read();
Serial.print(char(ComReading));
Serial.print(" ");
Serial.println();
if (ComReading == '1')
{
// if char recieved do this
switch (ComReading)
{
case 'b':
//Do what you need when b is pressed
Serial.println(" b pressed");
humidityAndTemp();
break;
case 'h':
//Do what you need when h is pressed
Serial.println(" h pressed");
printTemp();
break;
case 't':
//Do what you need when t is pressed
Serial.println(" t pressed");
humidityAndTemp();
printTemp();
break;
default:
// Do something when it is not one of the above three characters
Serial.println(" another char pressed");
break;
}
}
if (ComReading == '0')
{
// if no char recieved do this
Serial.println("No Char recieved");
}
delay(1);
}
}