Serial Monitor communication

I am trying to make a temperature and humidity monitor and I wrote this code so far, But for some reason, even if I send a correct letter into the serial monitor, it doesn't do anything. Here is my code:

#include <dht.h>
#define dht11 A0
dht DHT;
int redPin= 9;// Red Color to pin 3 on the Arduino
int greenPin = 8;//Green Color to pin 5 on the Arduino
int bluePin = 7;
int buzzPin = 11;
char data;
float temp;
float humi;
void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(buzzPin, OUTPUT);
  setColor(225, 0, 0);
  Serial.begin(9200);
  Serial.println("Portable temp measuring unit");
  delay(100);
  Serial.println("Enter t for temp. Enter h for humidity.");
  Serial.println("Accesing sensor");
  delay(100);
  Serial.print(".");
  delay(1000);
  Serial.print(".");
  delay(1000);
  Serial.print(".");
  delay(100);
  analogWrite(buzzPin, 100);
  delay(250);
  analogWrite(buzzPin, 0);
  delay(250);
  analogWrite(buzzPin, 300);
  delay(250);
  analogWrite(buzzPin, 0);
  setColor(0, 255, 0);
  Serial.flush();
}
void loop(){
  DHT.read11(dht11);
  temp = DHT.temperature;
  humi = DHT.humidity; 
  if(Serial.available()>0){
      data = Serial.read();
  }  
  Temperature();
  Humidity();
  delay(3000);

}




void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
void Temperature(){
  
    if(data == "t"){
      analogWrite(buzzPin, 300);
      delay(250);
      analogWrite(buzzPin, 0);
      Serial.println("Temperature:");
      Serial.println(temp);
      Serial.print("*c");
      Serial.println(temp*1.8+32);
      Serial.print("*F");
    }
}
void Humidity(){
  if(data == "h"){
      analogWrite(buzzPin, 300);
      delay(250);
      analogWrite(buzzPin, 0);
      Serial.println("Humidity:");
      Serial.print(humi);
    }
}

This is an odd baudrate..... 9600 is a standard but I use 115200 and it works well.

Not the issue, but it’s an odd design to call each function(), then parse for the desired ‘letter’.

Most would o the parsing in loop(), or a dedicated command parser function.

To each his own.

Ehh?

My reading was that Serial.print does not work. Setup to 9200 would cut off Serial Monitor.

Oops

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.