Serial input to monitor perform multiple functions

I know this is probably a simple fix, but I can't seem to figure it out...
I want use the serial monitor to input a letter 'u' to perform a function OR
input a letter 'd' to perform a different function. This is what I have:

 if (Serial.available()> 0){
   up = Serial.read();
   }

 if (Serial.available()> 0){
  down = Serial.read();  
 }
 
  if (up == 'u'){
    Serial.print(up);
    Serial.println("Door Opening");
    doorOpening();}

  if (down == 'd'){
    Serial.print(down);
    Serial.println("Door Closing");
    doorClosing();
  }

This will allow the 'u' to call for the doorOpening() but the other does not work..... I assume it is because serial.read() is only putting the first character it receives in the up variable. I just dont know how to change it. Should I use a switch case some how??

Here is entire code if needed:

#include <RTClib.h> // for the RTC
#include <Wire.h>

// Instance of the class for RTC
RTC_DS3231 rtc;

char t[32];
char up;
char down;

int RelayUp = 4;
int RelayDown = 5;
int switchA = 6;
int switchB = 7;

const int OnHourUp = 6; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMinUp = 38;
const int OnHourDown = 21; //SET TIME TO OFF RELAY
const int OnMinDown = 8;
const int upHour;
const int upMin;
const int downHour;
const int downMin;  

int CurrentHour;
int CurrentMinute;


void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  if(rtc.lostPower()) {      // uncomment this line on the second time upload
   rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  }
  //If time is off comment out the if statement above and adjust the date in the line below
  //rtc.adjust(DateTime(2021,6,9,16,6,0));  
  //The time set above is June 8, 2021 6 16 AM uncomment it
  //Once the time has been set correctly comment out the line rtc.adjust(DateTime(2021,6,8,6,20,0)); line and then
  //uncomment the if statement above that you previously commented out to adjust the time

   pinMode(switchA, INPUT_PULLUP);
   pinMode(switchB, INPUT_PULLUP);
   pinMode(RelayUp, OUTPUT);
   pinMode(RelayDown, OUTPUT);  
   
   
}
void loop() {
 DateTime now = rtc.now();
   // Save check in time;
  sprintf(t, "%02d:%02d:%02d   %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year());  
   
  CurrentHour = now.hour();
  CurrentMinute = now.minute();

  Serial.print(F("Date/Time:   "));
  Serial.print(t);
 
  Serial.print("\tHour: ");
  Serial.print(CurrentHour);
  Serial.print("\tMinute: ");
  Serial.println(CurrentMinute);

 if (Serial.available()> 0){
   up = Serial.read();
   }

 if (Serial.available()> 0){
  down = Serial.read();  
 }
 
  if (up == 'u'){
    Serial.print(up);
    Serial.println("Door Opening");
    doorOpening();}

  if (down == 'd'){
    Serial.print(down);
    Serial.println("Door Closing");
    doorClosing();
  }
   if(CurrentHour == OnHourUp && CurrentMinute == OnMinUp){
    Serial.println("Door Opening");
    doorOpening();
    }
   
  if(CurrentHour == OnHourDown && CurrentMinute == OnMinDown){
     Serial.println("Door Closing");
      doorClosing();
    }

  if(digitalRead(switchA)==LOW){
     Serial.println("switchA was read");
     
  }

  if(digitalRead(switchB)==LOW){
     Serial.println("switchB was read");
  }


  if(CurrentHour == upHour && CurrentMinute == upMin){
    Serial.println("Door Opening");
    doorOpening();
    }
   
  if(CurrentHour == downHour && CurrentMinute == downMin){
     Serial.println("Door Closing");
      doorClosing();
     }
 
}

void doorOpening()
{
  while(digitalRead(switchA)== HIGH){
    digitalWrite(RelayUp,HIGH);
    digitalWrite(RelayDown,LOW);
  }
   
    if(digitalRead(switchA)== LOW){
    digitalWrite(RelayUp,LOW);
    digitalWrite(RelayDown,LOW);
    Serial.println("Door Opened");
    delay(60000);
    up = 0;
   
    }
}

void doorClosing()
{

 while(digitalRead(switchB)== HIGH){
   digitalWrite(RelayDown,HIGH);
    digitalWrite(RelayUp,LOW);
  }
   
    if(digitalRead(switchB)== LOW){
    digitalWrite(RelayDown,LOW);
    digitalWrite(RelayUp,LOW);
    Serial.println("Door Closed");
    delay(60000);
    down = 0;
   
   }
}

Think of it more like this

 if (Serial.available()> 0){
   direction = Serial.read();
   }

Ok. I think I am understanding, but direction turns orange like it is a function within the ide? Is this something special or is it just an arbitrary variable?

Thanks!

Ignore the syntax highlighting :wink:

1 Like

No, you don't know what the next character will be, so putting a 'd' into a variable called "up" makes zero sense.

1 Like

Thanks I think I have it figured out!

 if (Serial.available()> 0){
   direction = Serial.read();
   }

 if (direction == 'u') {
  Serial.print(up);
    Serial.println("Door Opening");
    doorOpening(); 
 }
 
  if (direction == 'd') {
  Serial.print(down);
    Serial.println("Door Closing");
    doorClosing(); 
 }