Arduino LESSON 5: Working With Strings

Arduino Lesson 5: Working With Strings

Arduino LESSON 5: Working With Strings

In this lesson we will learn more about working with strings. A string is literally a string or a series of characters. The word "hello" is a string of characters. Strings allow your computer program to interact with the world in a way that the world understands. So it is important that we learn a little more about strings.

In this lesson we will use the same circuit that we developed in Arduino lesson 3. If you need help setting up the circuit, visit Lesson 3. The circuit schematic and code we worked with are listed below.

This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

This is the code that will run this circuit.

int redLEDPin=9; //Declare redLEDPin an int, and set to pin 9 
int yellowLEDPin=10; //Declare yellowLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int yellowOnTime=250; //Declare yellowOnTime an int, and set to 250
int yellowOffTime=250; //Declare yellowOffTime an int, and set to 250
int numYellowBlinks=5; //Number of times to blink yellow LED
int numRedBlinks=5;  //Number of times to blink red LED

void setup() {
  Serial.begin(115200);
  pinMode(redLEDPin, OUTPUT);  // Tell Arduino that redLEDPin is an output pin
  pinMode(yellowLEDPin, OUTPUT);  //Tell Arduino that yellowLEDPin is an output pin
}

void loop() {

Serial.println("The Red LED is Blinking");
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}
Serial.println(" ");
Serial.println("The Yellow Yellow is Blinking");
  for (int j=1; j<=numYellowBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on Blink #: ");
    Serial.println(j);
    digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
    delay(yellowOnTime);             //Leave on for yellowOnTime
    digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
    delay(yellowOffTime);            //Leave off for yellowOffTime
}
Serial.println(" ");
}

In the code above, notice that we are using stings in some of our print statements. For example the line of code above the for loop for blinking the red LED reads as:

Serial.println("The Red LED is Blinking");

The quoted text is your string, and in this case it is "The red LED is blinking". As you can see, a string is just a string. We can use strings to present meaningful words and thoughts to the user. It's not limited to using strings like we did before, but we can actually create variables that contain strings. For example, in the code above, one of the strings we are outputting to the user is "The red LED is blinking". We will be able to create a variable called redMessage and set redMessage to "The red LED is blinking". To do this, we would have to declare the redMessage variable as a string. Program before void setup, which would make it a global variable. The code would look like this:

String redMessage="The Red LED is Blinking";
String yellowMessage="The Yellow LED is Blinking";

With this code we declare two new variables of the string type, in addition to declaring the variables, we initialize them with their respective values. This allows us to change our print declarations in the original code to print the strings by printing the variables we have assigned to them.

Serial.println("The Red LED is Blinking");

can be replaced with

Serial.println(redMessage);

Notice when we print the variable, we do not use quotes around it. When we say to print redMessage, it will print the string that was assigned to redMessage.

By assigning strings to variables instead of using them directly, it is much easier to modify and use your code. Try to get used to assigning strings to variables.

When we have string variables, it is possible to concatenate, or combine stings together.

For example, lets say we declare and initialize two variables:

String st1="Hello"; //Create a Strign st1 and set to "Hello "
String st2="World"; //Create a String st2 and set to "World"
String st3;         //Declare a String st3 but dont assign value to it yet.

// Now In your void loop add this

st3=st1+st2;      //Combine st1 and st2 into a new variable st3
Serial.println(st3); // Print out st3

It is a good practice to try to assign strings to variables and then use the variables.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0