SALES INQUIRIES: 1 (888) 767-9864

Arduino Issue: LED flickers (on then off) when using switchcase and serial monitor input via the keyboard

Are you using a switch statement that is triggered by input from the serial monitor window?

For example, when you type a letter into the serial monitor, you want that letter to trigger a case in your switchcase() statement, but what you’re finding is that the switchcase() doesn’t seem to be working right?

What’s going on?

Well, in this lesson we’ll explore reasons why that might be the case.

Lesson Overview

  • Code example and troubleshooting
  • ASCII control characters \n and \r
  • Troubleshooting Arduino with a dinosaur

 

 

Hardware you will need:

  • Arduino board, an Arduino Uno is used in the example[1]
  • Kit-on-a-shield, you can find more info on them here
  • Alternatively, if you don’t have a Kit-on-a-shield, you will need
    • Solderless breadboard [1]
    • Jumper wire [2]
    • 220 ohm resistor [2]
    • LED [2]
  • A tech savvy Velociraptor [1]

Examining the code

We want this program to accept input from the serial monitor window. When we type something in here, like an “a”, and we press “enter” or we hit “send”, we want it to do a different action.

In our case, we’ll want an LED to light up if an “a” is sent, and we’ll want a different LED to light up if a “b” is sent. If any other letters are sent, then all the LEDs turn off.

Here’s the code for what that will look like:

 

Let’s look at the setup(). We’ve got a Serial.begin(9600) which begins serial communication. We also set the modes of two pins, 13 and 12, to OUTPUT.

This is where the LEDs will be attached. If you have a Kit-on-a-shield, LED 7 will correspond to digital pin 13, and LED 6 will correspond to digital pin 12.

This is what makes the shield so handy, no wires or resistors are required to do this simple example.

 

Arduino Shield from PEA

Arduino Shield from PEA

 

If you don’t have the shield, not to worry, you can easily make the simple circuit as seen in this diagram.

 

Arduino wiring diagram

Alternative wiring diagram if you don’t have the shield

 

The first thing we want to do is create an “if statement” and check if there’s any data available in the serial buffer.

If the value is greater than 0, we’ve determined there is data available and we can continue.

Next we’ll read the sensor and whatever data is read, we’ll assign it to the integer “inByte”. Now we jump into our switch case function. If “inByte” equals “a”, then we want pin 13 to go to HIGH.

If “inByte” equals “b”, then we want pin 12 to go to HIGH. In all other cases, we want pin 12 and 13 to go low, so we use “default” . So far so good.

So if we test this, we would expect when “a” is typed, LED 7 turns on, and when “b” is typed, LED 6 turns on.

Anything else will turn both LEDs off.

If we were to type “afb” what should happen? LED 7 turns on for a split second, then is turned off by “f”, and then LED 6 turns on and remains on.

Testing the code

But let’s see what is actually happening. Type an “a” and only an “a” into the serial monitor.

 

Arduino Shield and Serial Monitor

LED’s on shield and Serial Monitor

 

We would expect LED 7 to turn on and remain on. What happens?

It flickered but then it went right back off. Let’s try “b”. LED 6 comes on momentarily and then goes off again.

What’s going on? What’s happening is a control character is being added to the end of this line when you hit “enter” or click “send”.

The reason for this is because “carriage return” is selected on the drop down next to the baud rate.

 

Arduino Serial Monitor, carriage return vs Newline

Arduino Serial Monitor, Carriage Return (CR) vs Newline (NL)

 

This dropdown allows us to select which ASCII control character gets added to the end of each of the statements when we click “send” or press “enter”. There are 4 options:

  • No line ending
  • Newline
  • Carriage Return
  • Both NL & CR (Both Newline & Carriage Return)

Select “No line ending”. This means no ASCII control characters will be added at all when you click send. Only the data, in our case “a”. Let’s try this option. LED 7 comes on and stays on.

Try typing “b”. LED 6 comes on and stays on. Try typing any other character. Both LEDs go out. It now works as advertised! But let’s delve a little deeper into this.

ASCII Control Characters

Control characters are ASCII commands and there’s actually quite a few of them. We’ve put the full table below, but do not worry about learning all of them.

Right now we’re just concerned with “\n” which is “New line”, and “\r”, which is “Carriage return”.

What’s the point of this “\n” in this “\r”?

What are they actually doing?

 

ASCII table

ASCII table

 

These commands are saying to the receiving end, “Hey, start a new line of text”. That’s really all it’s doing and they’re doing the same thing.

Why do we have two options? Some systems are looking for different things. Some systems are looking for that “\n”, others are looking for that “\r”, and some are looking for both the “\n” and “\r”.

In our case, we don’t want either, hence when we selected “No line ending”, it fixed our issue.

Testing the theory

Lets try a little example. Let’s change case “a” to “/n” and case “b” to “/r”.

 

Changes to the Arduino sketch code

Changes to the Arduino sketch code

 

Upload the program, open up the serial monitor, and make sure “No line ending” is selected.

Do not type anything, just hit “send”. Nothing happens, the LEDs stay off. Now select “New Line” and without typing anything, hit “send”. LED 7 comes on.

Now select “Carriage return” and click “send”. LED 6 now turns on. Next select “No line ending”, type any character, and click “send”.

Everything turns off.

Lastly, select “Both NL & CR” and click “send”. Both LED 7 and 6 turn on.

In Review

Let’s review why it didn’t work correctly.

Remember, the LEDs would just flicker and then go off when you typed “a” or “b”.

This is because when you type “a” in the serial monitor and click “send”, what was actually transmitted was “a\n” if you had “New line” selected.

The “a” meets the case and turns on the LED, but immediately after that it is replaced by “\n”, which meets the default case of anything other than “a” or “b”, and thus turns off all LEDs.

This is what caused the flicker.

To sum it all up, if you’re having issues sending data via the serial monitor and you’re getting outcomes you didn’t expect, make sure to check if you’re sending erroneous “\n” or “\r” ASCII characters when you didn’t intend to.

 

Arduino dinosaur

Leave a Comment