07) Basic Serial Connections

From the blinking light example, we might not be able to figure out where exactly the problem is. So one method of finding out what is happening in the Arduino is to use serial connections back to the computer. This is the serial monitor button in the Arduino environment. However, this button is not magic, it will not show what you want unless you tell it within the main code.

Before we use serial, we have to start it. We use a function called "Serial.begin()". This has a period in between the two words, don't forget that. As well as serial being capitalized, the word (as well as variables) are case sensitive. This function has one argument, it declares the speed of transfer that you would like to use. Those speeds are:
300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, and 115200
The normal speed is typically 9600, but can be changed to your desire if the speed is too slow. The speeds mentioned above are measured in Pulses Per Second. The fastest the Arduino can handle is 115.2 Kbps.

We will use 9600, a nice baud rate that is not to slow or fast for what we will do. So, continuing off the code from last lesson, we will get:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(5,HIGH);//Turn light on
  delay(1000);//Wait one second
  digitalWrite(5,LOW);//Turn light off
}

You will notice that I put in the non-working version. We will try to use an alternate method on how to fix our problem. So we will carry on with our printing commands. There are two commands to "print" to the computer (NOTE: print does not connect to the printer, it just displays whatever you tell through the code to the serial monitor). There is "Serial.print()" and "Serial.println". The last one has lower case L and N at the end, that is short for line. The only differance between the two is that the "println" funtion will make a "carriage return" which goes down a line.

So now the argument. This argument is special, it can be either a number(593), character('A'), or a string("Hello World")!
a number looks like: 5301
a character looks like: 's'
a string looks like: "This is a string!"

A character contains ONLY one letter, and is represented in ASCII! If you don't know what ASCII, don't worry, for now it is not important, but if you would like to know, click this link here. And a string contains a bunch of characters combined together.

void setup()
{
  Serial.begin(9600);
  Serial.println("Staring Code!");//A message to make sure that the setup was completed
}

void loop()
{
  digitalWrite(5,HIGH);//Turn light on
  Serial.println("ON");//Telling the computer that we turned the light on
  delay(1000);//Wait one second
  digitalWrite(5,LOW);//Turn light off
  Serial.println("OFF");//Telling the computer that we turned the light off
}

Now upload it the Arduino, but when it is done, make sure you press the "Serial Monitor" button on the right of the upload button.
You should now see that the serial monitor messages have "ON" written on the bottom of the window, showing that it does the "OFF" command to quick, almost as if it skipped it! With the our knowledge now, lets fix it with the messages still there.

void setup()
{
  Serial.begin(9600);
  Serial.println("Staring Code!");//A message to make sure that the setup was completed
}

void loop()
{
  digitalWrite(5,HIGH);//Turn light on
  Serial.println("ON");//Telling the computer that we turned the light on
  delay(1000);//Wait one second
  digitalWrite(5,LOW);//Turn light off
  Serial.println("OFF");//Telling the computer that we turned the light off
  delay(1000);//Wait one second
}

Now lets see if that fixed it. You should see that ON and OFF is alternating at a steady pace. So now its on to the next lesson.
Comments