Now that we know how to to make Arduino talk to us through serial communication channel TX/RX, our next step is to find a way to say something back to Arduino and build some intelligence so that Arduino can understand us. When I look back to the good old days of BBC MicroCommodore 64 and ZX Spectrum class of home computers, I have very fond memories of writing simple programs in Basic, but, I had a lot of fun doing so. It was an era when people wanted to be friends with their beloved computer. I figured why not do the same with Arduino. After all it is much more powerful compared to my ZX Spectrum.

So here is what we want to do. Arduino will talk to us and we will reply and that in turn will be understood by Arduino and we have a conversation. Think that you are just 8 to 10 years old and your only objective is to learn and interact with your new found friend Arduino.

Do not get taken aback by the long code posting. When we get to the bottom of this, you will understand the critical parts and the silly imaginations of the 10 year old. But, you will have one happy Arduino with a friendly personality. So let is first dive into the code.

// Arduino interacts with the user

// when we type something back to Arduino the text we type
// should be stored in a temporary buffer before we get a
// chance to use it
String userName = "";
// even thought Arduino cannot think by itself, it is very fast
// when we type the response back, we are relatively slow
// after all we humans cannot communicate in 1s and 0s
// this variable is here to tell Arduino when we have finished typing
boolean userEntryComplete = false;

// we want to initialize serial communications
void setup()
{
  // the buffer through which we communicate back to Arduino
  // needs to be allocated
  userName.reserve(50);

  // we will setup Arduino to communicate with the PC
  // at 9600 baud (9600 bits per second)
  Serial.begin(9600);

  // we will let Arduino introduce itself first
  Serial.println("Greetings! My name is Arduino.");
  // let Ardunino ask us our name next
  Serial.println("May I know with whom I am talking to?");
  // asking for the name more than once will be weird
  // so we are asking as part of the setup block and not inside the loop
}

void loop()
{
  // we do not want Arduino to process anything until we finished typing
  // so we will let Arduino to check to see whether we have done before
  // it starts to process
  if(userEntryComplete)
  {
    if(userName != "")
    {
      // user entered something
      // if the entry is yes or no, then Arduino needs to behave intelligently
      // think that these are commands that Arduino needs to process
      if (userName.equalsIgnoreCase("no"))
      {
        // Arduino needs to just wait to meet someone new
        // you guessed it, Arduino is sad
        Serial.println(":( ok. Bye for now.");
      }
      else if (userName.equalsIgnoreCase("yes"))
      {
        // this is great news for our Arduino, it gets to meet with someone new
        // Arduino is happy and eager to meet with a new friend
        Serial.println(":) Please introduce me to your friend. What is your friend's name?");
      }
      else
      {
        // time for Arduino to say Hello and find out whether there is a a posibility
        // of meeting another new friend
        Serial.println("Hello! " + userName + ". Welcome to the world of Arduino!");
        Serial.println("Nice to know you " + userName + ". Do you have a friend with you?");
      }
    }
    else
    {
      // May be our user does not understand English or perhaps out right mean
      // But, Arduino will be polite and will remind the user that it is expecting
      // the user to enter his/her name
      Serial.println("Hello! I am Arduino. Please enter your name.");
    }
    // we need to initialize these parameters so that Arduino will wait for
    // further user interaction
    userEntryComplete = false;
    userName = "";
  }

  // we are done, but, Ardunio does not know that
  // poor Arduino will loop through this block forever
}

// function is automatically called when serial input data is available
// but this availability of serial data is checked only after
// each processing of our loop code
void serialEvent()
{
  // we will try to read the serial input only when one or more
  // bytes of data is available for us to read
  // Serial.available() method returns the number of bytes available
  while (Serial.available() > 0)
  {
    // we can use the read() method to read the value
    // but, read() only returns the value of a byte (0 to 255)
    // we already know that user is typing through the keyboard
    // and the data will be in ASCII characters and so we will
    // convert the value we receive to a character
    char oneCharacter = (char)Serial.read();
    // unless we receive the special New Line character from serial monitor
    // as a signal for us to stop, we will add/concatenate the characters together
    // to form a name; most people have more than one alphabet unless we meet with
    // Q from Star Trek: Enterprise
    if (oneCharacter == '\n')
    {
      userEntryComplete = true;
    }
    else
    {
      userName += oneCharacter;
    }
  }
}

That was long, but, if you exclude the comments and silliness there is not much to it. I have been liberal with the comments so that it helps the reader to understand why the next code block is written and what it is expected to do.

Let us fire up the above program and see how Arduino behaves. I suggest that you follow this write up to understand how it works and perhaps experiment with your own variant of the above code (by typing it and not copy and paste the code).

After you compile and upload the code to your Arduino, open up Serial Monitor and you should see the following.

User_Interaction_1

Pay attention to the baud rate and Newline section. It is important that these selections are made correctly, as our code above assumes these settings.

That’s good. Arduino is waiting for us to type in our name. You can type in your name and press the send button.

User_Interaction_2

Cool! It works. I have another friend next to me and so I will type yes and press the send button.

User_Interaction_3

User_Interaction_4

I have Pooni next to me and so I will introduce her to Arduino.

User_Interaction_5

Unfortunately we have run out of company and so I will type no and press send.

User_Interaction_6

Bye Arduino!

Will Arduino slowly learn to become Mr.Data?

Why not?

Advertisements