LESSON 6: Reading Data from Arduino Serial Monitor
Résumé
TLDRArduino lesson number six teaches users how to receive input from a user for controlling LED behavior through serial communication. Paul emphasizes coding by hand, debugging skills, and explains how to configure inputs for LED blinking parameters via the serial monitor. The lesson includes details on setting up user prompts, waiting for input, and saving those inputs in variables for future use. By the end, viewers can modify their programs to make them more interactive and user-friendly for controlling LED lights without needing to change the underlying code directly.
A retenir
- 🖥️ Coding by hand enhances learning and debugging skills.
- 📜 Use serial communication for user input in Arduino.
- 🔍 Debugging involves finding and correcting code mistakes.
- ⚙️ Prompt users with clear messages on the serial monitor.
- 📈 Important to classify variables before use without initializing them right away.
- ⏳ Use while loops to wait for user input before proceeding.
- 🔄 Decide where to place user input code based on whether it needs to run multiple times.
- 💡 Experiment with your own projects and code modifications for better understanding.
Chronologie
- 00:00:00 - 00:05:00
Paul McArter introduces Arduino lesson number six, focusing on user input integration into Arduino circuits. He highlights the importance of typing code directly for effective learning and debugging, warning against copying and pasting code from sources.
- 00:05:00 - 00:10:00
The lesson continues with a brief recap of previous lessons, detailing the setup of necessary variables for LED blinking. The importance of the serial monitor for user interaction and feedback is also emphasized, showcasing how to compile and download code to the Arduino.
- 00:10:00 - 00:15:00
In this session, Paul explains the need for user input on blinks for red and yellow LEDs. He discusses the limitations of manually changing code and introduces methods to prompt and collect user data via the serial monitor, including using `Serial.println()` for prompts.
- 00:15:00 - 00:20:00
The foundational process of reading inputs includes declaring variables without assigning them values initially. Paul explains how to utilize `serial.available()` to wait for user input before proceeding in the code loop.
- 00:20:00 - 00:25:00
Next, he elaborates on using `Serial.parseInt()` to capture user input into specified variables. Paul clarifies the mechanics of input reading, emphasizing the need for the command to match the variable type, and explains different methods of input capture.
- 00:25:00 - 00:34:37
Finally, Paul modifies where user input prompts occur—shifting them to the setup section for single-time prompts or keeping them in the loop for continuous prompts. He assigns a task to the viewers for further practice, encouraging them to extend functionality by adding more user input parameters.
Carte mentale
Vidéo Q&R
What is the main topic of this lesson?
The lesson focuses on getting user input into an Arduino program.
Why is it important to type the code in rather than copying it?
Typing the code helps you learn to debug and find mistakes.
How do you get user input in Arduino?
You use serial communication to prompt and read data from the user.
What function is used to read an integer from the serial port?
You use serial.parseInt() to read an integer value.
Where should you place the code for getting user input if you only want to ask once?
You should place it in the void setup.
What should you do for your assignment?
Modify the program to ask for more parameters through user input.
Voir plus de résumés vidéo
FIQIH NIKAH | Pendidikan Pra Nikah | Buya Yahya | 1 Ramadhan 1445 H / 12 Maret 2024 M
How to talk about impact in your design projects (and what to do if you don't have it)
Hakikat Pendidikan Kewarganegaraan pt 2
KENAPA KITA HARUS INTROSPEKSI DIRI? | TAUSIYAH USTADZ HILMAN FAUZI
The Future of Healthcare is Retail | Dan Stanek - WD Partners | HealthSpaces
No Contact Is Weak...Do This Instead & Watch Her Crumble! | Jordan Peterson
- 00:00:00hi guys I'm Paul mcarter and I'm here
- 00:00:02with Arduino lesson number six uh in
- 00:00:06this lesson we're going to be learning
- 00:00:07how to get input from a user into the
- 00:00:10Arduino and so this is a really
- 00:00:12important lesson hope you guys enjoy it
- 00:00:14hope you learn a lot from it now we are
- 00:00:16going to continue to use this circuit
- 00:00:18here if you need help in hooking this
- 00:00:21circuit up that was in lesson number
- 00:00:23three also visit my website to
- 00:00:27toptechboy do.com that's toptechboy
- 00:00:30tocom you can get the link down in the
- 00:00:32description of this video and uh on that
- 00:00:36site if you will go to lesson number
- 00:00:37three you can get sort of a step by step
- 00:00:40of hooking the circuit up or if you're
- 00:00:43more technically Adept already you can
- 00:00:45just see the schematic in this lesson
- 00:00:47which is lesson number six and so uh go
- 00:00:49to toptech boy.com and uh on lesson
- 00:00:53number six you can see the schematic of
- 00:00:55this and also this code that we've been
- 00:00:58developing in the first few lessons you
- 00:01:00can get uh in the first five lessons you
- 00:01:03can get that code at toptechboy do.com
- 00:01:06all right on uh this I've said this
- 00:01:08before and I'll say it again it's really
- 00:01:10important for you to be putting this
- 00:01:11code in yourself I step you through it
- 00:01:14step by step in this video you really
- 00:01:17need to be typing the code in yourself
- 00:01:19if you go to my site and you just copy
- 00:01:21it and paste it by the end of this
- 00:01:23series of videos what you have learned
- 00:01:25to do is copy and paste what you need to
- 00:01:28learn to do is develop your own code and
- 00:01:31the way that you learn to develop your
- 00:01:32own code is by coding typing things in
- 00:01:35now to start with your code might look a
- 00:01:37whole lot like mine but it's important
- 00:01:39for you to be typing it in because when
- 00:01:41you type it in you will make mistakes
- 00:01:44and when you make mistakes you then have
- 00:01:46to find your mistakes and correct them
- 00:01:49and that is more important than anything
- 00:01:51else you will learn in these videos is
- 00:01:53how to make mistakes how to find the
- 00:01:56mistakes and how to correct the mistakes
- 00:01:58because that's what you spend your life
- 00:01:59doing as a coder or as a programmer
- 00:02:01making mistakes and then finding them
- 00:02:03and correcting them and that's called
- 00:02:05debugging and nobody writes perfect code
- 00:02:08and so what's really important is to
- 00:02:10start now early in your programming life
- 00:02:13to learn how to uh debug uh debug your
- 00:02:16code so please please please you're
- 00:02:18wasting your time if you simply just go
- 00:02:20in and copy and paste my code now I do
- 00:02:22know that some of you might be coming in
- 00:02:24the middle of these series and you don't
- 00:02:26want to go back and take the first five
- 00:02:28lessons you just want to jump into this
- 00:02:30lesson well for you you can go to my
- 00:02:32website toptech boy.com lesson six you
- 00:02:35can get this code so that you're at
- 00:02:37least starting where we are uh where
- 00:02:39where where we're starting here
- 00:02:41basically you can see what this code
- 00:02:43does is we set up a bunch of variables
- 00:02:45up at the top of the program and those
- 00:02:47variables are things like let's
- 00:02:49particularly look at num yellow blink
- 00:02:51and num red blink we're saying that we
- 00:02:53want num red blinks to be five and num
- 00:02:56yellow blinks to be five and then
- 00:02:58basically the red LED is blinking five
- 00:03:01times if you look down
- 00:03:03here and if you look at the yellow it
- 00:03:05then blinks five five times we also in
- 00:03:08the last lesson learned how to start
- 00:03:11working with the serial monitor so that
- 00:03:13we can uh so that we can actually put
- 00:03:17information out on our screen that the
- 00:03:20user can look at just to recap the way
- 00:03:22you get that is uh let's just say let's
- 00:03:24just start real here real quick here
- 00:03:26with a review to get this code down into
- 00:03:29the arduin we click this button you can
- 00:03:31see the green bar starts going across as
- 00:03:33it's compiling and then uh everything's
- 00:03:36white and so everything's good and now
- 00:03:39to see the things that we're printing
- 00:03:40from the Arduino we need to open the
- 00:03:42serial monitor the way we open the
- 00:03:44serial monitor is clicking there and
- 00:03:46then you can see that I start getting
- 00:03:48the information that's being printed by
- 00:03:50the Arduino quick recap of the program
- 00:03:53uh we set the parameters up for the
- 00:03:55blink like the the red on time the red
- 00:03:57off time yellow on time yellow off time
- 00:03:59the parameters of the blink and the
- 00:04:01numbers of
- 00:04:02blinks we set up up here at the top of
- 00:04:05the code and our void setup we put the
- 00:04:08things in the void setup that we want to
- 00:04:10do one time and so things like opening
- 00:04:13the serial Port we do that with a
- 00:04:15serial.
- 00:04:16begin 115,200 is the B rate you can set
- 00:04:21that to a lot of different things but
- 00:04:23what's critical is this number has to be
- 00:04:25the same as this number that if those
- 00:04:27two don't match it ain't going going to
- 00:04:31work okay what else do we do in the void
- 00:04:34setup we do our pin modes and the pin
- 00:04:37mode is basically just telling the
- 00:04:39Arduino whether we're going to be
- 00:04:40outputting to a pen or inputting to a
- 00:04:43pen on the Arduino right now the pins
- 00:04:47that we're using are primarily these
- 00:04:49which are our digital pins pin 0 through
- 00:04:5113 those can be outputs or those can be
- 00:04:55inputs so far we've only dealt with them
- 00:04:57as outputs and for a little while longer
- 00:04:58we'll be dealing with them as outputs
- 00:05:00but basically anytime you use a digital
- 00:05:02pen you need to set your PIN mode so we
- 00:05:04so say so we say pin mode red LED pin
- 00:05:08which if we look up here was pin n so
- 00:05:10we're saying we want pin nine to be an
- 00:05:11output and yellow LED pin is pin 10 we
- 00:05:14want the yellow yellow LED pin to be an
- 00:05:17output as well Okay so we've got things
- 00:05:19set up now in the void Loop are the
- 00:05:21things that happen over and over and
- 00:05:23over and so inside of that void Loop we
- 00:05:26have two four Loops the four Loops are
- 00:05:28the loops that we make ourselves I think
- 00:05:30that was probably video four that we
- 00:05:32talked about uh talked about for loops
- 00:05:34and what you can see here is is that uh
- 00:05:37in the loop uh in the void Loop we
- 00:05:40basically set up a four Loop for
- 00:05:42blinking the LED how many times do we
- 00:05:45blink the LED we blink it num red blinks
- 00:05:48time and then we have another for Loop
- 00:05:50for blinking the yellow LED and it
- 00:05:53blinks yellow uh numb yellow blinks and
- 00:05:56so we come through and blink the red one
- 00:05:58numb red blink BLS and then we come to
- 00:06:00the next Loop and we blink the yellow
- 00:06:02one num yellow blinks and then last time
- 00:06:05we learned how to print and so you see
- 00:06:07that we have some print statements to do
- 00:06:09this printing over here so as I'm
- 00:06:11sitting and watching I think it's kind
- 00:06:13of cool because this is telling me
- 00:06:15exactly what's happening in here and so
- 00:06:17I've got the circuit uh working with the
- 00:06:19program working with what you see all uh
- 00:06:22all working together so all of a sudden
- 00:06:24you know we're getting some pretty we're
- 00:06:26getting some pretty neat stuff to happen
- 00:06:27here okay now let's move on to what
- 00:06:30we're going to really be talking about
- 00:06:31in today's lesson and this is this is
- 00:06:34neat stuff that we're doing but uh the
- 00:06:36one thing that's not so great is is that
- 00:06:39you can come in and like you can change
- 00:06:41things like let's say we want to have 10
- 00:06:43blinks I'll say I want that I'm going to
- 00:06:45make it I'm going to make it 15 blinks
- 00:06:48uh I'm going to make it one blink on the
- 00:06:50yellow and I'm going to make it 15
- 00:06:52blinks on the red okay so I can control
- 00:06:55that parameter and I can come in here
- 00:06:57and I can uh uh change those values and
- 00:07:02I did not get it I've got to replace
- 00:07:05this serial cable it's too long this USB
- 00:07:07cable is too long and sometimes the
- 00:07:09program is not happy and I have to click
- 00:07:11it a couple of times third time maybe is
- 00:07:14going to be a charm and uh let's see if
- 00:07:18we got it down in
- 00:07:23there yeah okay it downloaded so you see
- 00:07:26now we are blinking 13 14 15 times and
- 00:07:29the uh yellow blinks one it goes 15
- 00:07:36times and it blinks once you see we're
- 00:07:38mainly blinking the red one and the
- 00:07:40yellow one so we can go uh we could go
- 00:07:42the other way let's say that we went 15
- 00:07:44on the yellow and let's say we went one
- 00:07:47on the red by changing our code there
- 00:07:51okay and let's see if we can bring up
- 00:07:53the serial monitor all right and then
- 00:07:56you see we blink the red one and then
- 00:07:58the yellow is going to blink 15 so you
- 00:08:00see we can make it now do anything that
- 00:08:02we want and as we looked at it last time
- 00:08:04we could also come in here and we could
- 00:08:06change these other things like how long
- 00:08:08for a given blink how long is it we
- 00:08:11could make longer blinks shorter blinks
- 00:08:13or we could have uneven blinks where
- 00:08:15it's on most of the time and off not so
- 00:08:17much of the time so we've got a lot of
- 00:08:18flexibility here but guys this is the
- 00:08:21pro problem at some point you're going
- 00:08:24to be writing programs that you that
- 00:08:26you're going to let other people use and
- 00:08:28so you need a better way of getting
- 00:08:30these numbers in you can't just hand a
- 00:08:33person off the street your program and
- 00:08:35have them come in and start modifying
- 00:08:36the code that would be a disaster right
- 00:08:39you got to give away for a person to
- 00:08:41interact a person off the street to
- 00:08:43interact with your program and you can't
- 00:08:45let them come in and be changing your
- 00:08:47code and so we need a way to get input
- 00:08:50from the user that doesn't involve uh uh
- 00:08:54changing the changing the code and so
- 00:08:56the way we do that is we do that by
- 00:08:58using the the serial Monitor and so
- 00:09:00we're going to do that by by figuring
- 00:09:03out how to put a number in here and then
- 00:09:05having that number end up over here and
- 00:09:08so there's a couple of things that we're
- 00:09:09going to have to do in order to allow
- 00:09:11that to happen and the first thing is
- 00:09:14I'm not going to change all of these
- 00:09:16because I'm going to give you examples
- 00:09:18and then you as your assignment need to
- 00:09:20go in and get all of these different
- 00:09:22numbers or get some subset of these
- 00:09:24numbers from the user as user as user
- 00:09:27input okay so I'm not going to do every
- 00:09:28single one for you I'm going to show you
- 00:09:30an example and then you can go in and
- 00:09:32you can do the rest of them okay but the
- 00:09:35two that let's do is the number of
- 00:09:37blinks and so instead of assigning your
- 00:09:40variables here of num red blinks and num
- 00:09:43yellow blinks what we're going to do is
- 00:09:44we're going to get those two numbers
- 00:09:46from the user all right so the first
- 00:09:48thing we need to do is we need to not
- 00:09:51assign a value to them here because we
- 00:09:55are going to assign those values over
- 00:09:59the serial Port okay now anytime you're
- 00:10:02going to use a variable you still have
- 00:10:04to declare it okay and so I am still
- 00:10:08declaring these variables because I am
- 00:10:09still using these variables these
- 00:10:11variables are still the buckets and
- 00:10:13they're the buckets that I'm going to
- 00:10:15put the values into it's just I'm not
- 00:10:17going to put the values into them now
- 00:10:19but because I know I'm going to use them
- 00:10:21I still have to declare them so what I
- 00:10:23need you to see is is that there's two
- 00:10:25things that we are doing like here on
- 00:10:27yellow off time I'm declaring the
- 00:10:30variable as an integer that's creating
- 00:10:33the bucket and putting the label on it
- 00:10:35which is yellow off time and then I'm
- 00:10:37also putting a number in it the number
- 00:10:39that I'm putting in it is 250 those are
- 00:10:42two things that I'm doing in this one
- 00:10:44line of code but you can only do one if
- 00:10:47you want and that is what I'm doing here
- 00:10:49I'm only declaring the variable I'm only
- 00:10:51creating the bucket putting the label on
- 00:10:53it it'll be later that I actually put a
- 00:10:55number into it okay but you always have
- 00:10:58to in Arduino you always have to declare
- 00:11:01your variables so here we've declared
- 00:11:03them but there's no value associated
- 00:11:05with them and so now how do we get a
- 00:11:07value well we're going to get the value
- 00:11:09from the serial Port okay and to get a
- 00:11:13value from a Serial Port there's
- 00:11:15basically three things that you have to
- 00:11:17do and you've got to do all three of
- 00:11:19them okay the first thing is you have to
- 00:11:22prompt the user for uh that you want
- 00:11:26data from him and the way you do that is
- 00:11:28is that you can do that again just with
- 00:11:31a simple serial print line because
- 00:11:33basically you're going to send a message
- 00:11:35to that serial Port telling the person
- 00:11:38that you want them to input data and so
- 00:11:40here and I'm going to do this at the top
- 00:11:42of the Void Loop to start with so right
- 00:11:45when I come into this void loop we're
- 00:11:46going to ask the user how many times do
- 00:11:49you want to Blink the red LED and how
- 00:11:51many times do you want to Blink the
- 00:11:53yellow LED okay but there's three things
- 00:11:55to get that input that you have to do
- 00:11:57and the first is you have to promp promp
- 00:11:59the user that you're expecting input
- 00:12:01from him we would do that with a print
- 00:12:03line statement so I'm going to go
- 00:12:06serial.print Ln why do we do print Ln
- 00:12:09because we want it to advance to the
- 00:12:11next line and not just start going
- 00:12:12across the across the page if you
- 00:12:14remember uh video five we kind of talked
- 00:12:17about that so what is it that we want to
- 00:12:19print well I'm going to print a string
- 00:12:21and I'm just going to directly put the
- 00:12:22string in here which is going to be the
- 00:12:24prompt and I'm going to say
- 00:12:26please uh let's see about just we we we
- 00:12:30say how many times
- 00:12:37do you want to how many times do you
- 00:12:42want the red L2
- 00:12:46blink question mark and then I always
- 00:12:49like to put a space after it just so
- 00:12:51that I don't run things together and
- 00:12:53then I need to close the quotes I need
- 00:12:56to close the parentheses and I need
- 00:12:59semicolon okay and so when this runs
- 00:13:02this is going to go out to that serial
- 00:13:04Monitor and it's going to put this
- 00:13:05statement how many times do you want to
- 00:13:08uh the red LED to Blink that's a
- 00:13:11string okay that is a string and we are
- 00:13:16putting that string out on uh the serial
- 00:13:19port and we can do that because we turn
- 00:13:20the serial port on in our void setup so
- 00:13:24we prompt now this is this is the part
- 00:13:28that people really kind of mess up on
- 00:13:30and it's something that You' just got to
- 00:13:32understand you've got to understand that
- 00:13:34a program can run thousands and
- 00:13:36thousands of times faster than a person
- 00:13:39can do things and so when you tell a
- 00:13:42person to do something when the when you
- 00:13:44your program or the computer or the
- 00:13:46microcontroller the Arduino tells the
- 00:13:49person to do something you have to stop
- 00:13:52and wait okay now you don't want to put
- 00:13:55a delay in because you don't know one
- 00:13:57person might be pretty fast and might
- 00:13:59answer it in a second another person
- 00:14:01might be distracted and might not answer
- 00:14:03for a couple of minutes so after you ask
- 00:14:06the person for input you have to wait
- 00:14:10wait how long wait until they put the
- 00:14:14input in and how do you do that well how
- 00:14:16do you know if they put the input in
- 00:14:19there is something that is called serial
- 00:14:21do available and serial dot available
- 00:14:25will be zero if they haven't put
- 00:14:27anything in it will be serial dot
- 00:14:30available will be one if they have put
- 00:14:33something in so what we need to do is
- 00:14:35just sort of test that serial dot
- 00:14:38available and if it's zero we need to
- 00:14:42continue to wait if it's one we need to
- 00:14:45move on to the next step but what we got
- 00:14:47to do is we've got to wait here right
- 00:14:51here before we come and start doing this
- 00:14:54other stuff right here we have got to
- 00:14:57wait until until that person has put the
- 00:15:00number in so how do we wait how long do
- 00:15:02we wait well we do something new called
- 00:15:04a while loop we wait while okay and
- 00:15:10we're going to have some condition in
- 00:15:12the while loop and the while loop is
- 00:15:14like a for Loop okay it's got a clause
- 00:15:18okay and a clause starts with a a curly
- 00:15:22bracket and it ends with a curly bracket
- 00:15:26okay a while loop starts with a curly
- 00:15:29bracket and ends with a curly bracket a
- 00:15:32while loop will continue and let me put
- 00:15:34an extra blank line in here okay a while
- 00:15:37loop will continue to do all the
- 00:15:40commands that are between the curly
- 00:15:42brackets as long as the condition that
- 00:15:47you put between those parentheses is
- 00:15:49true so I need to put a I need to put a
- 00:15:52condition here so like whatever
- 00:15:54condition I put here this while loop is
- 00:15:56going to continue to Loop and do
- 00:15:59everything between those curly brackets
- 00:16:01as long as that condition is true well
- 00:16:05what is the condition that we're
- 00:16:06interested in the condition that we're
- 00:16:09interested in is serial. a
- 00:16:13available open close with nothing in it
- 00:16:16okay as long as that is equal equal to
- 00:16:20zero which
- 00:16:21means while serial. available is equal
- 00:16:25equal Z equal equal is just a a uh
- 00:16:29conditional test so if you want to test
- 00:16:32if something is something you use equal
- 00:16:35equal so if you said serial do available
- 00:16:38was equal to zero that's like you're
- 00:16:39trying to set it equal to zero if you
- 00:16:42want to test to see if it's zero you use
- 00:16:45equal equal and this is something as I
- 00:16:47help students and as as I work with
- 00:16:49students this is one of the most common
- 00:16:51reasons that there's a mistaken code is
- 00:16:53they do a conditional a test and they
- 00:16:55just use one equal and that throws
- 00:16:57everything up so if you're asking does
- 00:17:00serial available equal 0 it's equal
- 00:17:02equal does serial doav available equal
- 00:17:05equal Z it's a question okay so if you
- 00:17:08put two of them there it's like a
- 00:17:09question so what does this
- 00:17:12do if if serial. available equal equal
- 00:17:16zero then that means the person hasn't
- 00:17:18put the number in yet what do you want
- 00:17:20to do go through the loop and go through
- 00:17:22it again now we look has the person
- 00:17:24entered the number yet if it's equal to
- 00:17:26zero no they haven't so this thing is
- 00:17:29going to sit and basically Loop until
- 00:17:32the person puts a number in once the
- 00:17:34person puts a number in then serial dot
- 00:17:36available would be one this condition
- 00:17:39would be false and it's going to break
- 00:17:42out and then come right here and do
- 00:17:45whatever the next command is that's
- 00:17:47after the while loop now the reason I
- 00:17:50know that this is a little confusing is
- 00:17:52normally in a while loop you would be
- 00:17:53doing something right I mean I would be
- 00:17:55doing something here in this while loop
- 00:17:57but this is an empty while loop it's an
- 00:18:00empty while loop because the purpose of
- 00:18:01it is to just hang the program at this
- 00:18:04point waiting for that person to put the
- 00:18:06number in so I guess I could put a
- 00:18:08comment in here because the comment
- 00:18:09doesn't do anything and just say
- 00:18:12twiddle
- 00:18:13[Music]
- 00:18:14your
- 00:18:16thumbs
- 00:18:18until user inputs data okay now
- 00:18:23understand with a comment the Arduino
- 00:18:25doesn't read it the Arduino doesn't do
- 00:18:27anything but just if you read the code
- 00:18:29you see that you're just looping looping
- 00:18:31looping looping looping looping here
- 00:18:33until the person puts the number in when
- 00:18:35the person puts the number in serial.
- 00:18:37available would become one this
- 00:18:40statement would no longer be true you
- 00:18:42would drop out and you would come here
- 00:18:44okay I hope you understand it all right
- 00:18:47but if you don't the bottom line is
- 00:18:51serial uh while serial. available equal
- 00:18:54equal 0 open curly bracket close curly
- 00:18:57bracket I'm going to take this out now
- 00:18:59okay I'm going to take that out and in
- 00:19:02fact I'm just going to go ahead and just
- 00:19:04put the empty Clause right here you see
- 00:19:06there's uh there is nothing in that
- 00:19:08clause and so this just makes the code a
- 00:19:11little neater while serial do available
- 00:19:14is equal to zero while no data has been
- 00:19:17entered do nothing and then it just sits
- 00:19:21at this line so if you if you if you're
- 00:19:23not following what I'm saying I'm sorry
- 00:19:25but just take my word for it this line
- 00:19:28of code will make the program wait here
- 00:19:31until the data has been entered once the
- 00:19:34DAT so now we prompt the user to put the
- 00:19:37data in we wait for him to put it in he
- 00:19:40puts it in what do we have to do we've
- 00:19:42got to read it it's like we say throw me
- 00:19:44the ball he throws us the ball we got to
- 00:19:47catch it okay so we said send me the
- 00:19:50data he sends the data we got to read it
- 00:19:54okay and how do you read it well you got
- 00:19:56to say you got to put it somewhere first
- 00:19:59well where are we going to put it what
- 00:20:00are we trying to read here am I even
- 00:20:03yeah what are we trying to read right
- 00:20:05here okay what are we trying to read the
- 00:20:08number of times that the red L that we
- 00:20:10want the red LED to Blink well where is
- 00:20:13that variable that variable is num red
- 00:20:16blinks so what we want to do is we want
- 00:20:18to say num red blinks blinks b e l i n
- 00:20:24KS yeah I got to get everything just
- 00:20:26right number Red blinks is equal to what
- 00:20:29we're going to go do is we're going to
- 00:20:30go read that number off of the serial
- 00:20:32Port now so we're going to say serial do
- 00:20:37parse in this is new stuff this is new
- 00:20:40stuff okay serial. parent and then we
- 00:20:43put our colon all right what is this
- 00:20:46doing this is saying go out to the
- 00:20:48serial Port can we do that yeah we turn
- 00:20:50the serial port on up here so it knows
- 00:20:52about the serial Port read a number from
- 00:20:56the serial Port is what this is is
- 00:20:58saying and what's really important here
- 00:21:00is is that you need to make this command
- 00:21:03match the type of number that you're
- 00:21:05expecting we are expecting what type of
- 00:21:08number numb red blinks is an INT so we
- 00:21:11need to tell it to read an INT how does
- 00:21:14it read an INT serial. parse
- 00:21:18int okay and that puts the number then
- 00:21:22into num red blinks the variable so
- 00:21:24remember we create the bucket up here we
- 00:21:27create the bucket up here the bucket is
- 00:21:29empty it's got the label on the bucket
- 00:21:31num red blinks and then down here I say
- 00:21:34I'm going to put something in the numb
- 00:21:35red blinks bucket go to the serial part
- 00:21:38and read a what read an INT an integer
- 00:21:43okay if we had made this up here if we
- 00:21:47had made this up here a float we would
- 00:21:49need to do a parse float okay so your
- 00:21:52read statement needs to match the type
- 00:21:54of variable that you're trying to read
- 00:21:56all right
- 00:21:59you could do if you're reading an INT
- 00:22:02you do a serial. parse int if you're
- 00:22:05going to read a float you do a serial.
- 00:22:07parse float
- 00:22:10okay reading a string is a little bit
- 00:22:12differently if you're going to read a
- 00:22:14string like if we wanted to read this
- 00:22:15message we would do a serial. read
- 00:22:18string okay serial. read string now
- 00:22:22there's a hundred different ways to do
- 00:22:24this and a lot of people go in and read
- 00:22:26things one character at a time and do
- 00:22:28all this crazy stuff and that's probably
- 00:22:29a better way of doing it but I want you
- 00:22:31to just learn something that's Rock
- 00:22:33Solid that's simple to understand and it
- 00:22:36will work for anything that you want to
- 00:22:38do if you want to read an INT do serial.
- 00:22:41parse int if you want to read a float do
- 00:22:44serial. pars float if you want to read a
- 00:22:46string do serial. read string so parse
- 00:22:51in parse
- 00:22:53float or read string okay those are the
- 00:22:57three different ways that you can input
- 00:22:59something okay so let's let's look at
- 00:23:01what we've done here we've done serial.
- 00:23:04print line how many times you want the
- 00:23:06red to Blink then we wait and then we
- 00:23:10read the number that they put in into
- 00:23:13numb red blinks and then we're ready to
- 00:23:15loot down here num red blinks now we
- 00:23:18need to do that because remember we also
- 00:23:21took o I think that should not be like
- 00:23:23that
- 00:23:26okay I think I yeah need to be more
- 00:23:29careful in my code here okay so
- 00:23:33basically uh what we are doing here is
- 00:23:36we
- 00:23:38are oh no I'm sorry ignore that never
- 00:23:41mind I was on the wrong line okay uh
- 00:23:45never mind all right here num yellow
- 00:23:48blinks num red blinks we declare and
- 00:23:51then down here what we do is we read the
- 00:23:56number of red blinks so we prompt the
- 00:23:59user for input we wait for him to input
- 00:24:02it and then we read it in now also on
- 00:24:06num yellow blinks we've declared that
- 00:24:08but we haven't we haven't assigned a
- 00:24:09value to it so we need to get that value
- 00:24:12as well and so what we're going to do is
- 00:24:14we are going to copy this and then we're
- 00:24:17going to paste it because we want to do
- 00:24:20the same thing for yellow but we want to
- 00:24:21say how many times do you want the this
- 00:24:25time it's going to be
- 00:24:26yellow okay led to Blink and then we
- 00:24:30wait again and then this time this is
- 00:24:33going to
- 00:24:35be num yellow blinks so how many times
- 00:24:40do you want it to Blink we prompt the
- 00:24:42user then we wait and then when he
- 00:24:46enters it we read it into numb yellow
- 00:24:49blinks and so at this time at this point
- 00:24:52with this code we now know how many
- 00:24:55times the user wants to Blink red and
- 00:24:57how many times s the user wants to Blink
- 00:24:59yellow
- 00:25:01okay will it work I hope so let's go
- 00:25:05ahead and take a look
- 00:25:10right green green everybody's happy good
- 00:25:13deal so now let's look at this serial
- 00:25:15Monitor and what uh where is my serial
- 00:25:18monitor come on maybe I got to pop it up
- 00:25:22down here okay so look at this we have a
- 00:25:25message waiting on the serial monitor
- 00:25:27how many times do you want the red LED
- 00:25:30to Blink I think I would like it to
- 00:25:31Blink 10 times okay how many
- 00:25:37uh I look at that I put that in the
- 00:25:40wrong spot because that should not have
- 00:25:41been there let's go back and look at
- 00:25:42this code okay uh look at that I sent
- 00:25:46this red message and I should not have
- 00:25:48done that so let's take this cut you see
- 00:25:52that was that line that was causing the
- 00:25:53problem and that should be down here
- 00:25:56like this
- 00:25:58okay let me kind of put some blank spots
- 00:26:00here so basically and let's go in and
- 00:26:03put some comments here I got a little
- 00:26:04sloppy there didn't I okay and so what
- 00:26:08are we doing here okay we are prompt
- 00:26:12user for input right what are we doing
- 00:26:16here we're putting our comments in wait
- 00:26:19for
- 00:26:21user input and then after we get it what
- 00:26:25do we
- 00:26:26do read
- 00:26:29user input okay and then here what are
- 00:26:33we doing we
- 00:26:36are
- 00:26:38prompt user for
- 00:26:41input what is this step four we wait for
- 00:26:47input and
- 00:26:49then this time we read user input okay
- 00:26:56now do you see when I got kind of sloppy
- 00:26:58and going too fast and I wasn't putting
- 00:27:00my comments in I ended up with this red
- 00:27:02message
- 00:27:05here okay up there where it should not
- 00:27:08have been and so uh now this should work
- 00:27:12so we're going to serial print line and
- 00:27:14get the number of red blinks and then
- 00:27:16we're going to get the number of yellow
- 00:27:17blinks and so now let's go in and see
- 00:27:19see if this
- 00:27:21works yes I do make
- 00:27:24mistakes all right so let's see if we
- 00:27:26can get our serial monitor
- 00:27:28pop it up
- 00:27:31here okay so it's waiting for data how
- 00:27:34many times do you want the red uh led to
- 00:27:37Blink you got to put the number in here
- 00:27:38we're going to say 10 now you could
- 00:27:40either hit send or you could click enter
- 00:27:43on your keyboard let's go ahead and just
- 00:27:45click Send here see how that does how
- 00:27:47many times do you want the yellow LED to
- 00:27:49Blink let's say one time okay and now
- 00:27:53when I send this you be watching over
- 00:27:56here and we should see this This Blink
- 00:27:5810 times and one time so send okay and
- 00:28:02look that's blinking Seven 8 n 10 and
- 00:28:06then one okay let's try that again it
- 00:28:11says how many times do you want the red
- 00:28:12LED to Blink okay now why is it doing
- 00:28:14that because where we put this code in
- 00:28:16here we put it at the top of the Void
- 00:28:18Loop so it's going to do the red blinks
- 00:28:21it's going to do the yellow blinks and
- 00:28:22then it's going to come back up here and
- 00:28:24then it's going to prompt us again and
- 00:28:25so this time let's do 10
- 00:28:28on the red okay how many times you want
- 00:28:30the red to Blink
- 00:28:3210 okay how many times you want the
- 00:28:34yellow to Blink let's do 10 on that okay
- 00:28:38and then send
- 00:28:40that okay and here we go three four five
- 00:28:436 7 8 nine 10 one 2 3 4 five six 7 8
- 00:28:50nine 10 okay and then it comes back over
- 00:28:53here and asks me again so this time
- 00:28:56let's say five
- 00:28:58okay and let's say five we come over
- 00:29:01here one two 3 four five one two three
- 00:29:06four five okay so you see it's working
- 00:29:09and every time through it's going to ask
- 00:29:11me how many times do I want the red to
- 00:29:13Blink and how many times do I want the
- 00:29:15yellow to Blink all right and then it'll
- 00:29:17stop every time and so I can control it
- 00:29:19why does it stop every time and let's
- 00:29:21talk about that a little bit well where
- 00:29:23we put this we put it uh inside of our
- 00:29:25void Loop so here is our I mean we put
- 00:29:28it inside of our void Loop so at the top
- 00:29:30of the Void Loop the first hot thing
- 00:29:32it's going to do is ask for the number
- 00:29:33of red blinks and it's going to ask for
- 00:29:35the number of yellow blinks then it's
- 00:29:37going to Blink that many times red and
- 00:29:39then it's going to Blink that many times
- 00:29:41yellow and then it's going to print a
- 00:29:42little blank line to make our formatting
- 00:29:44nice and then this is the end of the
- 00:29:46Void Loop right here and the void Loop
- 00:29:49is a loop so then what is that going to
- 00:29:52do that is going to come back up here to
- 00:29:54the top of the Void Loop and it's going
- 00:29:55to do this again what if we just wanted
- 00:29:58to set this up and we just wanted to ask
- 00:30:01the user one time for his input for like
- 00:30:05the number of red blinks and the number
- 00:30:06of yellow blinks and then just want it
- 00:30:08to sit and keep doing that over and over
- 00:30:10and over and not ask every single time
- 00:30:12so if we wanted to just ask for the user
- 00:30:14input one time where would we put things
- 00:30:17that we wanted to just do one time well
- 00:30:19we could put it up here in the void
- 00:30:20setup and so what if we got all this
- 00:30:22data here and we come up and let's just
- 00:30:25say we cut it because we don't want it
- 00:30:27to do it inside the void Loop anymore we
- 00:30:30want it to do it inside of the Void
- 00:30:33setup and
- 00:30:35uh I do like to try
- 00:30:37to keep my uh indenting kind of neat
- 00:30:41here so that I see all this indented
- 00:30:43stuff is the setup
- 00:30:49uh okay and I also like to put kind of
- 00:30:52spaces in my program so I can kind of
- 00:30:54see things that are uh so I can sort of
- 00:30:57see how things do you see how like I
- 00:31:00indent this so that it's natural for me
- 00:31:02to see this int indentation is all the
- 00:31:04void setup and then the void loop I
- 00:31:06probably should go in and try to do
- 00:31:08indentation on uh on that as well to
- 00:31:11kind of keep things
- 00:31:13uh easy to read now it doesn't really
- 00:31:16matter the white spaces Arduino doesn't
- 00:31:18look at if you're writing in python
- 00:31:20python actually does commands based on
- 00:31:22the white spaces so the indention is
- 00:31:25very important in Python uh it's not
- 00:31:27important in Arduino but I do think it's
- 00:31:29part of writing good and readable code
- 00:31:31and code that's easy to uh easy to debug
- 00:31:34so anyway now we've moved those prompts
- 00:31:36up to the void setup the void setup you
- 00:31:38do one time so I think this time what it
- 00:31:41should do is it should ask us one time
- 00:31:43for blinks and one time for the yellow
- 00:31:45blinks and then it should just sit and
- 00:31:46blink back and forth so let's let's take
- 00:31:48a look at this and see what
- 00:31:54happens okay it looks like it downloaded
- 00:31:56uh with everybody happy and so let's see
- 00:32:00how many times do you want to Brink
- 00:32:01blink the red LED I'm going to say five
- 00:32:05okay how many times do you want to Blink
- 00:32:07the yellow LED I'm going to say five and
- 00:32:10then let's look over here and see if it
- 00:32:12does it one two 3 four five one two
- 00:32:15three four five one two three four five
- 00:32:19yellow yellow yellow yellow yellow y red
- 00:32:22red red red okay so you see it's going
- 00:32:25back and forth and also you can see that
- 00:32:26it's synced up nicely here with our uh
- 00:32:29earlier print statements that are
- 00:32:30showing which uh which led uh which led
- 00:32:32is blinking so that's pretty cool I mean
- 00:32:35you can see that where you put stuff in
- 00:32:37the code starts affecting what happens
- 00:32:39and and you can see that if we in the
- 00:32:41void setup you're kind of asking for a
- 00:32:43onetime deal and then you do that over
- 00:32:45and over and over but also what you can
- 00:32:48do here is you can put it down here if
- 00:32:50you would want it to do you could put it
- 00:32:52down here in the void Loop if you would
- 00:32:54like it to do it separate every time
- 00:32:56okay okay so your assignment is to play
- 00:32:58around with this and and basically what
- 00:33:00I'd like you to do is go in and take all
- 00:33:02of these uh I mean don't input for what
- 00:33:04the PIN numbers are because the pin
- 00:33:06number should not be something that the
- 00:33:09user inputs right you're the programmer
- 00:33:11you're the one that set this circuit up
- 00:33:13you have to tell it that red LED pen is
- 00:33:15nine and you have to tell it that red
- 00:33:17LED pin is 10 but these other things
- 00:33:20like the red on time the red off time
- 00:33:22yellow on time yellow off time those
- 00:33:24would be things that you should enter
- 00:33:26from the uh that you should enter from
- 00:33:29the uh from the serial port and so what
- 00:33:31I want you to go in is is modify the
- 00:33:33program where you ask the user for all
- 00:33:36of the information maybe you would ask
- 00:33:38for some of it in the setup maybe these
- 00:33:40blink parameters how long it's on and
- 00:33:43how long it's off you maybe only do that
- 00:33:45one time do it in the void setup but
- 00:33:47then maybe do the number of blinks every
- 00:33:49time and so you would do that in the
- 00:33:51void Loop okay so play around with this
- 00:33:53and what I'm going to do for a grade is
- 00:33:55I'm going to see that you've gone and
- 00:33:56you youve put put more parameters in
- 00:33:58there and also just make it do something
- 00:34:00else I mean just think hey this is what
- 00:34:01I want it to do and kind of come up with
- 00:34:03your own goals and strategies and then
- 00:34:05sort of develop some of your own code so
- 00:34:07at a minimum I want you to do what I've
- 00:34:10done and change uh everything except the
- 00:34:12PIN numbers into things that you input
- 00:34:14that's the minimum but what I'd like you
- 00:34:16to do is kind of go beyond and say well
- 00:34:17what I would like my LEDs to do is I
- 00:34:19would like them to do this and then this
- 00:34:21is the way my program looks so you can
- 00:34:23sort of start taking this uh your own
- 00:34:25Direction okay I appreciate it this is
- 00:34:27six lessons down and I will see you
- 00:34:30again very shortly in Arduino lesson
- 00:34:33number seven take it easy
- 00:34:36guys
- Arduino
- Programming
- User Input
- Serial Monitor
- Code Debugging
- LED Blinking
- Input Parameters
- Functionality
- Microcontroller
- User Interaction