SALES INQUIRIES: 1 (888) 767-9864

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

Transcript:

Hello, I hope you’re doing fantastic. Now I’ve got a question. Are you using a switch statement that is triggered by input from the serial monitor window? You know like you type a letter and then that letter supposed to trigger a case in your switchcase() in your Arduino program, 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 a reason why that might be the case. Specifically, in this lesson, we’ll walk step by step through a code example and troubleshoot what might be going on. We’ll talk briefly about ASCII control characters and we’ll also learn how to use a dinosaur to troubleshoot Arduino. So let’s jump into the Arduino ide and take a look at a code example. So just as a reminder, we’re looking at a piece of code. We want this program to accept input from the serial monitor window and when we type something in here, like in a and we press enter or we hit send here, we want it to do a different action.

And in this case, I’ve set up a simple program that turns on an LED if an ‘a’ is sent and it turns on a different LED if ‘b’ is sent, and it turns off all the LEDs, if any other letter is sent. So that’s kind of what we’re after here. So to do that in setup(), we’ve got a couple of statements. The first thing is we’ve got a Serial.begin(9600). This begins serial communication and then we’ve set the mode of two pins as outputs pin 13 and 12. And this is where I have my LED circuits attached. Now I’m using a Kit-on-a-Shield for our Arduino. So it actually attaches the LEDs for me once I put it on top of the Arduino. But for all intents and purposes, you can just assume that these are two LEDs attached. Now down in the loop function, I’ve got a couple things going on.

The first thing I want to do is I want to check if there’s any data available in the serial buffer. So basically I want to check, hey, did anybody send over some characters through the serial monitor window? That’s the first thing I’m checking, is this value greater than zero? If that’s the case, then we jump in to this if statement and the first thing I want to do while I want to get the first item that was put inside the serial buffer, so if I was over here and I typed in a and s, e, f and I hit send, the first item is going to be an a. The second will be an s then an e then an f. This is going to take that first character and it’s going to store the value in this variable inByte. So we read that first character and now we’re jumping into the switchcase() and this is where the issue seems to be coming in the switchcase(), looking at that variable inByte and it is checking it against two cases.

It wants to see, hey, did we send an ‘a’ or did we send a ‘b’? If it’s an a, we’re going to use digital right to turn pin 13 high. That’ll turn on the LED and then same thing, if it’s ‘b’, we’re going to turn on that LED, but the one attached to pin 12 and then if it’s neither ‘a’ or ‘b’, we come to the default and that’s when we turned both of those pins low. Turning those LEDs. Well, let’s go ahead and check this out. What we should see when we send a is we should see the LED at pin 13. Come on and stay on. And then if we send a ‘b’, we should see that happen at pin 12. And if we send anything else, those LEDs should go out. But let’s see what is actually happening. So I’m going to type in a here and you can see I’m using my Kit-on-a-Shield for Arduino and I’ll send this and keep an eye on that pin

and it, uh, it flicked but then it went right back off. Coming on real quick. But then it’s going off. Let me try. The ‘b’ will send a ‘b’, okay. That LED is coming on and then flicking right off again. Okay. So it’s coming on and it’s going off. What’s going on? Well, what’s happening is a control character is being added to the end of this line and the reason that is is because down here in this dropdown, this is where we select which ASCII control character gets added to the end of each of the statements. When we click send. So if we click here, we’ve got four options, we’ve got no line ending, new line, carriage return, and both new line and carriage return. So what we want for this to work correctly is no line ending. That means the only thing that’s going to get sent is the ‘a’ that we put in here or whatever we might type in. Nothing else is going to get added onto the end. So let’s give this a shot. Let Me Send A. Okay, so I sent a and now the LED comes on bright. Let me send ‘b’. I’ll send that. Okay. And now that one comes out and let me send something off kilt

and now they go out. All right, looks like it’s working, right? Well, what exactly was happening here? Let’s talk a little bit about these control characters. So control characters are ASCII commands and there’s actually quite a few of them. The ones that we’re looking at here are ‘\n’ which is the new line, and then ‘\r’, which is the carriage return. So what’s the point of this ‘\n’ in this ‘\r’, what are they actually doing? Well, what these commands are doing is saying to the receiving end, hey, start a new line of text. That’s really all it’s doing and they’re both doing the same thing. Well, if new line and carriage return are doing essentially the same thing, why do we have two options? Well, the reason is 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 a ‘\n’ and ‘\r’, which would be the both new line and carriage return.

That’s why you got all those options, but in this case, again, we want no line ending. Now chances are you’re like, yeah, I really don’t believe you. So let’s try this as a little example. I’m going to go ahead in this first case and I’m going to go ahead and enter a new line here, so I’ll go back slash n and I’m going to enter a carriage return here. I’ll make this backslash are. I’ll go ahead and upload the program. I’ll open up the serial monitor and now I’ve got no line ending. I’m to get hit send and I should see nothing happened on my LEDs. Okay? Nothing happened. Now what I’m going to do, Oh look it even let me know . You just you press send and there’s nothing there guy. Now I’m going to select New Line and I’m going to hit send.

Now look at that. Now LED 13 came on. Well let me switch this to carriage return and I’ll hit send or enter and now that one comes on. All right, I’ll change it back to No Line Ending and all. Send something. Turn them both off and now I’m going to send it to new line and carriage return. So the first thing should be a new line and that should set off the first case and then the second should be a carriage return. That should have suck set off the second case, let me hit send and there they both turned on. So see I wasn’t making that up. Well maybe. But anyway. So the reason the LED is flickering when we have an option set here other than no line ending is because that additional byte is being sent over to the serial buffer when we hit send.

So if we don’t have No Line Ending instead we have new line, it actually looks something like this, it would be an a and then at the end you would see a new line. And so this gets sent to the serial buffer and the first character is going to get pulled in by Serial.read(). So ‘a’ is going to be an inByte, the switchcase() statement going to see ‘a’ and it’s going to go ahead and turn pin 13 High, which turns our LED on. But the next time through the loop it’s going to pull in that control character. So now inByte is going to be the control character and that doesn’t match ‘a’, sure doesn’t match ‘b’, therefore it’s going to go to the default and it’s gonna turn pin 13 and 12 Low, turning off the LEDs and that’s why we’re seeing those LEDs flicker. So if we don’t want that, again, we just set that to No Line Ending. So what’s the moral of the story? If you’re having an issue sending data over the serial monitor and getting your cases to work right, and it doesn’t even have to be a switchcase() some other times you might not be expecting some of these control characters. Just remember, double check, No Line Ending. Well, hey, that’s a wrap. I hope you found it helpful and I hope you have a great day. Bye.

Arduino Code: