0

I have a bit of code below, the problem is only the first 'if' statement will execute when called on. When I call on u, q, or any of the other if statements it will go straight to the else and not execute. Tried switching the following ifs to 'else if' but got the same result.

#include "Arduino.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include "DHT.h"

DHT dht1(3, DHT11);
DHT dht2(2, DHT11);

void setup() 
{
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  digitalWrite(6,LOW);
  digitalWrite(5,LOW);
  digitalWrite(4,LOW);
  Serial.begin(9600);
  dht1.begin();
  dht2.begin();
}

void loop() 
{
  float DHT11_t = dht1.readTemperature(true);
  float DHT21_t = dht2.readTemperature(true);

  if(Serial.read() == 'u')
  {
    Serial.println();
    Serial.print(DHT11_t,1);
    Serial.println();
    Serial.print(DHT21_t,1);
    Serial.println();
  }

  if(Serial.read() == 'c')
  {
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
    digitalWrite(8,LOW);
    digitalWrite(7,LOW);
    digitalWrite(6,LOW);
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
    Serial.print("NDX");
    Serial.println();
    Serial.print("u");
    Serial.println();
  }

  if(Serial.read() == 'q')
  {
    digitalWrite(13,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(10,HIGH);
    digitalWrite(9,HIGH);
    Serial.print("STX");
    Serial.println();
    Serial.print("u");
    delay(2000);
    Serial.print("u");
    delay(2000);
    Serial.print("u");
    delay(2000);
    Serial.print("u");
    delay(2000);
    Serial.print("u");
    delay(2000);
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
    Serial.print("NDX");
    Serial.println();
    Serial.print("u");
    Serial.println();
  }

  if(Serial.read() == 'w')
  {
    digitalWrite(8,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(6,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(4,HIGH);
  }

  else if(Serial.read() == 'e')
  {
    digitalWrite(13,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(10,HIGH);
    digitalWrite(9,HIGH);
  }

  if(Serial.read() == 'r')
  {
    digitalWrite(8,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(6,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(4,HIGH);
  }

  if(Serial.read() == 't')
  {
    digitalWrite(13,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(10,HIGH);
    digitalWrite(9,HIGH);
    delay(30000);
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
    delay(750);
    digitalWrite(8,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(6,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(4,HIGH);
    delay(30000);
    digitalWrite(8,LOW);
    digitalWrite(7,LOW);
    digitalWrite(6,LOW);
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
  }

  if(Serial.read() == 'y')
  {
    digitalWrite(13,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(11,HIGH);
    digitalWrite(10,HIGH);
    digitalWrite(9,HIGH);
    delay(30000);
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
    delay(750);
    digitalWrite(8,HIGH);
    digitalWrite(7,HIGH);
    digitalWrite(6,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(4,HIGH);
    delay(30000);
    digitalWrite(8,LOW);
    digitalWrite(7,LOW);
    digitalWrite(6,LOW);
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
  }

  else
  {
    digitalWrite(13,LOW);
    digitalWrite(12,LOW);
    digitalWrite(11,LOW);
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
    digitalWrite(8,LOW);
    digitalWrite(7,LOW);
    digitalWrite(6,LOW);
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
  }

}   
4

Serial.read() takes the first byte out of the serial buffer. Once it's executed, this byte is gone, so the Serial.read() in the next if statement will fetch the next byte (if available). What you can do is assign the result of Serial.read() to some variable before the first if statement, and then use that variable instead of Serial.read() in the if statements.

  • Can you post the fix? I tried defining the var but got 'not declared in this scope' – nuggetchris Oct 7 '18 at 21:13
  • @nuggetchris Please take a look at the example from arduino.cc/en/Serial/Read. It should work if you put your if statements after incomingByte = Serial.read(); (inside if (Serial.available() > 0) { }). – ex-punctis Oct 7 '18 at 22:10
  • And this is also switching Serial.read() == x within the if statement with incomingByte == x? – nuggetchris Oct 7 '18 at 22:13
  • @nuggetchris: Yes. Sorry, I forgot to mention it. – ex-punctis Oct 7 '18 at 22:16
  • Ok, it seems to work as long as I define a data type before incomingByte. Since it's only letters, I went with char. Only problem now is, the serial.printed letters the program produces, such as in the 'u' in the if statements, it doesn't seem to recognize. It only prints the 'u' but does not execute what the 'u' is stated to execute in the first if statement – nuggetchris Oct 7 '18 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.