Monday, November 12, 2018

Serial input and monitoring - Who's at the door?

// Wei's demo of if-else-if statement.

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT); //The led is in PIN 13
Serial.println("who are you?");
}

char who=0;

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()>0){
who=Serial.read();
}
if(who=='a') {
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
Serial.println("person a wants to enter the building");
}

else if(who=='b') {
digitalWrite(13,HIGH);
delay(250);
digitalWrite(13,LOW);
delay(250);
Serial.println("person b wants to enter the building");
}

}

No comments:

Post a Comment