LESSON 6: Reading Data from Arduino Serial Monitor

00:34:37
https://www.youtube.com/watch?v=-3Xg8x2ibeY

摘要

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.

心得

  • 🖥️ 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.

时间轴

  • 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.

显示更多

思维导图

视频问答

  • 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.

查看更多视频摘要

即时访问由人工智能支持的免费 YouTube 视频摘要!
字幕
en
自动滚动:
  • 00:00:00
    hi guys I'm Paul mcarter and I'm here
  • 00:00:02
    with Arduino lesson number six uh in
  • 00:00:06
    this lesson we're going to be learning
  • 00:00:07
    how to get input from a user into the
  • 00:00:10
    Arduino and so this is a really
  • 00:00:12
    important lesson hope you guys enjoy it
  • 00:00:14
    hope you learn a lot from it now we are
  • 00:00:16
    going to continue to use this circuit
  • 00:00:18
    here if you need help in hooking this
  • 00:00:21
    circuit up that was in lesson number
  • 00:00:23
    three also visit my website to
  • 00:00:27
    toptechboy do.com that's toptechboy
  • 00:00:30
    tocom you can get the link down in the
  • 00:00:32
    description of this video and uh on that
  • 00:00:36
    site if you will go to lesson number
  • 00:00:37
    three you can get sort of a step by step
  • 00:00:40
    of hooking the circuit up or if you're
  • 00:00:43
    more technically Adept already you can
  • 00:00:45
    just see the schematic in this lesson
  • 00:00:47
    which is lesson number six and so uh go
  • 00:00:49
    to toptech boy.com and uh on lesson
  • 00:00:53
    number six you can see the schematic of
  • 00:00:55
    this and also this code that we've been
  • 00:00:58
    developing in the first few lessons you
  • 00:01:00
    can get uh in the first five lessons you
  • 00:01:03
    can get that code at toptechboy do.com
  • 00:01:06
    all right on uh this I've said this
  • 00:01:08
    before and I'll say it again it's really
  • 00:01:10
    important for you to be putting this
  • 00:01:11
    code in yourself I step you through it
  • 00:01:14
    step by step in this video you really
  • 00:01:17
    need to be typing the code in yourself
  • 00:01:19
    if you go to my site and you just copy
  • 00:01:21
    it and paste it by the end of this
  • 00:01:23
    series of videos what you have learned
  • 00:01:25
    to do is copy and paste what you need to
  • 00:01:28
    learn to do is develop your own code and
  • 00:01:31
    the way that you learn to develop your
  • 00:01:32
    own code is by coding typing things in
  • 00:01:35
    now to start with your code might look a
  • 00:01:37
    whole lot like mine but it's important
  • 00:01:39
    for you to be typing it in because when
  • 00:01:41
    you type it in you will make mistakes
  • 00:01:44
    and when you make mistakes you then have
  • 00:01:46
    to find your mistakes and correct them
  • 00:01:49
    and that is more important than anything
  • 00:01:51
    else you will learn in these videos is
  • 00:01:53
    how to make mistakes how to find the
  • 00:01:56
    mistakes and how to correct the mistakes
  • 00:01:58
    because that's what you spend your life
  • 00:01:59
    doing as a coder or as a programmer
  • 00:02:01
    making mistakes and then finding them
  • 00:02:03
    and correcting them and that's called
  • 00:02:05
    debugging and nobody writes perfect code
  • 00:02:08
    and so what's really important is to
  • 00:02:10
    start now early in your programming life
  • 00:02:13
    to learn how to uh debug uh debug your
  • 00:02:16
    code so please please please you're
  • 00:02:18
    wasting your time if you simply just go
  • 00:02:20
    in and copy and paste my code now I do
  • 00:02:22
    know that some of you might be coming in
  • 00:02:24
    the middle of these series and you don't
  • 00:02:26
    want to go back and take the first five
  • 00:02:28
    lessons you just want to jump into this
  • 00:02:30
    lesson well for you you can go to my
  • 00:02:32
    website toptech boy.com lesson six you
  • 00:02:35
    can get this code so that you're at
  • 00:02:37
    least starting where we are uh where
  • 00:02:39
    where where we're starting here
  • 00:02:41
    basically you can see what this code
  • 00:02:43
    does is we set up a bunch of variables
  • 00:02:45
    up at the top of the program and those
  • 00:02:47
    variables are things like let's
  • 00:02:49
    particularly look at num yellow blink
  • 00:02:51
    and num red blink we're saying that we
  • 00:02:53
    want num red blinks to be five and num
  • 00:02:56
    yellow blinks to be five and then
  • 00:02:58
    basically the red LED is blinking five
  • 00:03:01
    times if you look down
  • 00:03:03
    here and if you look at the yellow it
  • 00:03:05
    then blinks five five times we also in
  • 00:03:08
    the last lesson learned how to start
  • 00:03:11
    working with the serial monitor so that
  • 00:03:13
    we can uh so that we can actually put
  • 00:03:17
    information out on our screen that the
  • 00:03:20
    user can look at just to recap the way
  • 00:03:22
    you get that is uh let's just say let's
  • 00:03:24
    just start real here real quick here
  • 00:03:26
    with a review to get this code down into
  • 00:03:29
    the arduin we click this button you can
  • 00:03:31
    see the green bar starts going across as
  • 00:03:33
    it's compiling and then uh everything's
  • 00:03:36
    white and so everything's good and now
  • 00:03:39
    to see the things that we're printing
  • 00:03:40
    from the Arduino we need to open the
  • 00:03:42
    serial monitor the way we open the
  • 00:03:44
    serial monitor is clicking there and
  • 00:03:46
    then you can see that I start getting
  • 00:03:48
    the information that's being printed by
  • 00:03:50
    the Arduino quick recap of the program
  • 00:03:53
    uh we set the parameters up for the
  • 00:03:55
    blink like the the red on time the red
  • 00:03:57
    off time yellow on time yellow off time
  • 00:03:59
    the parameters of the blink and the
  • 00:04:01
    numbers of
  • 00:04:02
    blinks we set up up here at the top of
  • 00:04:05
    the code and our void setup we put the
  • 00:04:08
    things in the void setup that we want to
  • 00:04:10
    do one time and so things like opening
  • 00:04:13
    the serial Port we do that with a
  • 00:04:15
    serial.
  • 00:04:16
    begin 115,200 is the B rate you can set
  • 00:04:21
    that to a lot of different things but
  • 00:04:23
    what's critical is this number has to be
  • 00:04:25
    the same as this number that if those
  • 00:04:27
    two don't match it ain't going going to
  • 00:04:31
    work okay what else do we do in the void
  • 00:04:34
    setup we do our pin modes and the pin
  • 00:04:37
    mode is basically just telling the
  • 00:04:39
    Arduino whether we're going to be
  • 00:04:40
    outputting to a pen or inputting to a
  • 00:04:43
    pen on the Arduino right now the pins
  • 00:04:47
    that we're using are primarily these
  • 00:04:49
    which are our digital pins pin 0 through
  • 00:04:51
    13 those can be outputs or those can be
  • 00:04:55
    inputs so far we've only dealt with them
  • 00:04:57
    as outputs and for a little while longer
  • 00:04:58
    we'll be dealing with them as outputs
  • 00:05:00
    but basically anytime you use a digital
  • 00:05:02
    pen you need to set your PIN mode so we
  • 00:05:04
    so say so we say pin mode red LED pin
  • 00:05:08
    which if we look up here was pin n so
  • 00:05:10
    we're saying we want pin nine to be an
  • 00:05:11
    output and yellow LED pin is pin 10 we
  • 00:05:14
    want the yellow yellow LED pin to be an
  • 00:05:17
    output as well Okay so we've got things
  • 00:05:19
    set up now in the void Loop are the
  • 00:05:21
    things that happen over and over and
  • 00:05:23
    over and so inside of that void Loop we
  • 00:05:26
    have two four Loops the four Loops are
  • 00:05:28
    the loops that we make ourselves I think
  • 00:05:30
    that was probably video four that we
  • 00:05:32
    talked about uh talked about for loops
  • 00:05:34
    and what you can see here is is that uh
  • 00:05:37
    in the loop uh in the void Loop we
  • 00:05:40
    basically set up a four Loop for
  • 00:05:42
    blinking the LED how many times do we
  • 00:05:45
    blink the LED we blink it num red blinks
  • 00:05:48
    time and then we have another for Loop
  • 00:05:50
    for blinking the yellow LED and it
  • 00:05:53
    blinks yellow uh numb yellow blinks and
  • 00:05:56
    so we come through and blink the red one
  • 00:05:58
    numb red blink BLS and then we come to
  • 00:06:00
    the next Loop and we blink the yellow
  • 00:06:02
    one num yellow blinks and then last time
  • 00:06:05
    we learned how to print and so you see
  • 00:06:07
    that we have some print statements to do
  • 00:06:09
    this printing over here so as I'm
  • 00:06:11
    sitting and watching I think it's kind
  • 00:06:13
    of cool because this is telling me
  • 00:06:15
    exactly what's happening in here and so
  • 00:06:17
    I've got the circuit uh working with the
  • 00:06:19
    program working with what you see all uh
  • 00:06:22
    all working together so all of a sudden
  • 00:06:24
    you know we're getting some pretty we're
  • 00:06:26
    getting some pretty neat stuff to happen
  • 00:06:27
    here okay now let's move on to what
  • 00:06:30
    we're going to really be talking about
  • 00:06:31
    in today's lesson and this is this is
  • 00:06:34
    neat stuff that we're doing but uh the
  • 00:06:36
    one thing that's not so great is is that
  • 00:06:39
    you can come in and like you can change
  • 00:06:41
    things like let's say we want to have 10
  • 00:06:43
    blinks I'll say I want that I'm going to
  • 00:06:45
    make it I'm going to make it 15 blinks
  • 00:06:48
    uh I'm going to make it one blink on the
  • 00:06:50
    yellow and I'm going to make it 15
  • 00:06:52
    blinks on the red okay so I can control
  • 00:06:55
    that parameter and I can come in here
  • 00:06:57
    and I can uh uh change those values and
  • 00:07:02
    I did not get it I've got to replace
  • 00:07:05
    this serial cable it's too long this USB
  • 00:07:07
    cable is too long and sometimes the
  • 00:07:09
    program is not happy and I have to click
  • 00:07:11
    it a couple of times third time maybe is
  • 00:07:14
    going to be a charm and uh let's see if
  • 00:07:18
    we got it down in
  • 00:07:23
    there yeah okay it downloaded so you see
  • 00:07:26
    now we are blinking 13 14 15 times and
  • 00:07:29
    the uh yellow blinks one it goes 15
  • 00:07:36
    times and it blinks once you see we're
  • 00:07:38
    mainly blinking the red one and the
  • 00:07:40
    yellow one so we can go uh we could go
  • 00:07:42
    the other way let's say that we went 15
  • 00:07:44
    on the yellow and let's say we went one
  • 00:07:47
    on the red by changing our code there
  • 00:07:51
    okay and let's see if we can bring up
  • 00:07:53
    the serial monitor all right and then
  • 00:07:56
    you see we blink the red one and then
  • 00:07:58
    the yellow is going to blink 15 so you
  • 00:08:00
    see we can make it now do anything that
  • 00:08:02
    we want and as we looked at it last time
  • 00:08:04
    we could also come in here and we could
  • 00:08:06
    change these other things like how long
  • 00:08:08
    for a given blink how long is it we
  • 00:08:11
    could make longer blinks shorter blinks
  • 00:08:13
    or we could have uneven blinks where
  • 00:08:15
    it's on most of the time and off not so
  • 00:08:17
    much of the time so we've got a lot of
  • 00:08:18
    flexibility here but guys this is the
  • 00:08:21
    pro problem at some point you're going
  • 00:08:24
    to be writing programs that you that
  • 00:08:26
    you're going to let other people use and
  • 00:08:28
    so you need a better way of getting
  • 00:08:30
    these numbers in you can't just hand a
  • 00:08:33
    person off the street your program and
  • 00:08:35
    have them come in and start modifying
  • 00:08:36
    the code that would be a disaster right
  • 00:08:39
    you got to give away for a person to
  • 00:08:41
    interact a person off the street to
  • 00:08:43
    interact with your program and you can't
  • 00:08:45
    let them come in and be changing your
  • 00:08:47
    code and so we need a way to get input
  • 00:08:50
    from the user that doesn't involve uh uh
  • 00:08:54
    changing the changing the code and so
  • 00:08:56
    the way we do that is we do that by
  • 00:08:58
    using the the serial Monitor and so
  • 00:09:00
    we're going to do that by by figuring
  • 00:09:03
    out how to put a number in here and then
  • 00:09:05
    having that number end up over here and
  • 00:09:08
    so there's a couple of things that we're
  • 00:09:09
    going to have to do in order to allow
  • 00:09:11
    that to happen and the first thing is
  • 00:09:14
    I'm not going to change all of these
  • 00:09:16
    because I'm going to give you examples
  • 00:09:18
    and then you as your assignment need to
  • 00:09:20
    go in and get all of these different
  • 00:09:22
    numbers or get some subset of these
  • 00:09:24
    numbers from the user as user as user
  • 00:09:27
    input okay so I'm not going to do every
  • 00:09:28
    single one for you I'm going to show you
  • 00:09:30
    an example and then you can go in and
  • 00:09:32
    you can do the rest of them okay but the
  • 00:09:35
    two that let's do is the number of
  • 00:09:37
    blinks and so instead of assigning your
  • 00:09:40
    variables here of num red blinks and num
  • 00:09:43
    yellow blinks what we're going to do is
  • 00:09:44
    we're going to get those two numbers
  • 00:09:46
    from the user all right so the first
  • 00:09:48
    thing we need to do is we need to not
  • 00:09:51
    assign a value to them here because we
  • 00:09:55
    are going to assign those values over
  • 00:09:59
    the serial Port okay now anytime you're
  • 00:10:02
    going to use a variable you still have
  • 00:10:04
    to declare it okay and so I am still
  • 00:10:08
    declaring these variables because I am
  • 00:10:09
    still using these variables these
  • 00:10:11
    variables are still the buckets and
  • 00:10:13
    they're the buckets that I'm going to
  • 00:10:15
    put the values into it's just I'm not
  • 00:10:17
    going to put the values into them now
  • 00:10:19
    but because I know I'm going to use them
  • 00:10:21
    I still have to declare them so what I
  • 00:10:23
    need you to see is is that there's two
  • 00:10:25
    things that we are doing like here on
  • 00:10:27
    yellow off time I'm declaring the
  • 00:10:30
    variable as an integer that's creating
  • 00:10:33
    the bucket and putting the label on it
  • 00:10:35
    which is yellow off time and then I'm
  • 00:10:37
    also putting a number in it the number
  • 00:10:39
    that I'm putting in it is 250 those are
  • 00:10:42
    two things that I'm doing in this one
  • 00:10:44
    line of code but you can only do one if
  • 00:10:47
    you want and that is what I'm doing here
  • 00:10:49
    I'm only declaring the variable I'm only
  • 00:10:51
    creating the bucket putting the label on
  • 00:10:53
    it it'll be later that I actually put a
  • 00:10:55
    number into it okay but you always have
  • 00:10:58
    to in Arduino you always have to declare
  • 00:11:01
    your variables so here we've declared
  • 00:11:03
    them but there's no value associated
  • 00:11:05
    with them and so now how do we get a
  • 00:11:07
    value well we're going to get the value
  • 00:11:09
    from the serial Port okay and to get a
  • 00:11:13
    value from a Serial Port there's
  • 00:11:15
    basically three things that you have to
  • 00:11:17
    do and you've got to do all three of
  • 00:11:19
    them okay the first thing is you have to
  • 00:11:22
    prompt the user for uh that you want
  • 00:11:26
    data from him and the way you do that is
  • 00:11:28
    is that you can do that again just with
  • 00:11:31
    a simple serial print line because
  • 00:11:33
    basically you're going to send a message
  • 00:11:35
    to that serial Port telling the person
  • 00:11:38
    that you want them to input data and so
  • 00:11:40
    here and I'm going to do this at the top
  • 00:11:42
    of the Void Loop to start with so right
  • 00:11:45
    when I come into this void loop we're
  • 00:11:46
    going to ask the user how many times do
  • 00:11:49
    you want to Blink the red LED and how
  • 00:11:51
    many times do you want to Blink the
  • 00:11:53
    yellow LED okay but there's three things
  • 00:11:55
    to get that input that you have to do
  • 00:11:57
    and the first is you have to promp promp
  • 00:11:59
    the user that you're expecting input
  • 00:12:01
    from him we would do that with a print
  • 00:12:03
    line statement so I'm going to go
  • 00:12:06
    serial.print Ln why do we do print Ln
  • 00:12:09
    because we want it to advance to the
  • 00:12:11
    next line and not just start going
  • 00:12:12
    across the across the page if you
  • 00:12:14
    remember uh video five we kind of talked
  • 00:12:17
    about that so what is it that we want to
  • 00:12:19
    print well I'm going to print a string
  • 00:12:21
    and I'm just going to directly put the
  • 00:12:22
    string in here which is going to be the
  • 00:12:24
    prompt and I'm going to say
  • 00:12:26
    please uh let's see about just we we we
  • 00:12:30
    say how many times
  • 00:12:37
    do you want to how many times do you
  • 00:12:42
    want the red L2
  • 00:12:46
    blink question mark and then I always
  • 00:12:49
    like to put a space after it just so
  • 00:12:51
    that I don't run things together and
  • 00:12:53
    then I need to close the quotes I need
  • 00:12:56
    to close the parentheses and I need
  • 00:12:59
    semicolon okay and so when this runs
  • 00:13:02
    this is going to go out to that serial
  • 00:13:04
    Monitor and it's going to put this
  • 00:13:05
    statement how many times do you want to
  • 00:13:08
    uh the red LED to Blink that's a
  • 00:13:11
    string okay that is a string and we are
  • 00:13:16
    putting that string out on uh the serial
  • 00:13:19
    port and we can do that because we turn
  • 00:13:20
    the serial port on in our void setup so
  • 00:13:24
    we prompt now this is this is the part
  • 00:13:28
    that people really kind of mess up on
  • 00:13:30
    and it's something that You' just got to
  • 00:13:32
    understand you've got to understand that
  • 00:13:34
    a program can run thousands and
  • 00:13:36
    thousands of times faster than a person
  • 00:13:39
    can do things and so when you tell a
  • 00:13:42
    person to do something when the when you
  • 00:13:44
    your program or the computer or the
  • 00:13:46
    microcontroller the Arduino tells the
  • 00:13:49
    person to do something you have to stop
  • 00:13:52
    and wait okay now you don't want to put
  • 00:13:55
    a delay in because you don't know one
  • 00:13:57
    person might be pretty fast and might
  • 00:13:59
    answer it in a second another person
  • 00:14:01
    might be distracted and might not answer
  • 00:14:03
    for a couple of minutes so after you ask
  • 00:14:06
    the person for input you have to wait
  • 00:14:10
    wait how long wait until they put the
  • 00:14:14
    input in and how do you do that well how
  • 00:14:16
    do you know if they put the input in
  • 00:14:19
    there is something that is called serial
  • 00:14:21
    do available and serial dot available
  • 00:14:25
    will be zero if they haven't put
  • 00:14:27
    anything in it will be serial dot
  • 00:14:30
    available will be one if they have put
  • 00:14:33
    something in so what we need to do is
  • 00:14:35
    just sort of test that serial dot
  • 00:14:38
    available and if it's zero we need to
  • 00:14:42
    continue to wait if it's one we need to
  • 00:14:45
    move on to the next step but what we got
  • 00:14:47
    to do is we've got to wait here right
  • 00:14:51
    here before we come and start doing this
  • 00:14:54
    other stuff right here we have got to
  • 00:14:57
    wait until until that person has put the
  • 00:15:00
    number in so how do we wait how long do
  • 00:15:02
    we wait well we do something new called
  • 00:15:04
    a while loop we wait while okay and
  • 00:15:10
    we're going to have some condition in
  • 00:15:12
    the while loop and the while loop is
  • 00:15:14
    like a for Loop okay it's got a clause
  • 00:15:18
    okay and a clause starts with a a curly
  • 00:15:22
    bracket and it ends with a curly bracket
  • 00:15:26
    okay a while loop starts with a curly
  • 00:15:29
    bracket and ends with a curly bracket a
  • 00:15:32
    while loop will continue and let me put
  • 00:15:34
    an extra blank line in here okay a while
  • 00:15:37
    loop will continue to do all the
  • 00:15:40
    commands that are between the curly
  • 00:15:42
    brackets as long as the condition that
  • 00:15:47
    you put between those parentheses is
  • 00:15:49
    true so I need to put a I need to put a
  • 00:15:52
    condition here so like whatever
  • 00:15:54
    condition I put here this while loop is
  • 00:15:56
    going to continue to Loop and do
  • 00:15:59
    everything between those curly brackets
  • 00:16:01
    as long as that condition is true well
  • 00:16:05
    what is the condition that we're
  • 00:16:06
    interested in the condition that we're
  • 00:16:09
    interested in is serial. a
  • 00:16:13
    available open close with nothing in it
  • 00:16:16
    okay as long as that is equal equal to
  • 00:16:20
    zero which
  • 00:16:21
    means while serial. available is equal
  • 00:16:25
    equal Z equal equal is just a a uh
  • 00:16:29
    conditional test so if you want to test
  • 00:16:32
    if something is something you use equal
  • 00:16:35
    equal so if you said serial do available
  • 00:16:38
    was equal to zero that's like you're
  • 00:16:39
    trying to set it equal to zero if you
  • 00:16:42
    want to test to see if it's zero you use
  • 00:16:45
    equal equal and this is something as I
  • 00:16:47
    help students and as as I work with
  • 00:16:49
    students this is one of the most common
  • 00:16:51
    reasons that there's a mistaken code is
  • 00:16:53
    they do a conditional a test and they
  • 00:16:55
    just use one equal and that throws
  • 00:16:57
    everything up so if you're asking does
  • 00:17:00
    serial available equal 0 it's equal
  • 00:17:02
    equal does serial doav available equal
  • 00:17:05
    equal Z it's a question okay so if you
  • 00:17:08
    put two of them there it's like a
  • 00:17:09
    question so what does this
  • 00:17:12
    do if if serial. available equal equal
  • 00:17:16
    zero then that means the person hasn't
  • 00:17:18
    put the number in yet what do you want
  • 00:17:20
    to do go through the loop and go through
  • 00:17:22
    it again now we look has the person
  • 00:17:24
    entered the number yet if it's equal to
  • 00:17:26
    zero no they haven't so this thing is
  • 00:17:29
    going to sit and basically Loop until
  • 00:17:32
    the person puts a number in once the
  • 00:17:34
    person puts a number in then serial dot
  • 00:17:36
    available would be one this condition
  • 00:17:39
    would be false and it's going to break
  • 00:17:42
    out and then come right here and do
  • 00:17:45
    whatever the next command is that's
  • 00:17:47
    after the while loop now the reason I
  • 00:17:50
    know that this is a little confusing is
  • 00:17:52
    normally in a while loop you would be
  • 00:17:53
    doing something right I mean I would be
  • 00:17:55
    doing something here in this while loop
  • 00:17:57
    but this is an empty while loop it's an
  • 00:18:00
    empty while loop because the purpose of
  • 00:18:01
    it is to just hang the program at this
  • 00:18:04
    point waiting for that person to put the
  • 00:18:06
    number in so I guess I could put a
  • 00:18:08
    comment in here because the comment
  • 00:18:09
    doesn't do anything and just say
  • 00:18:12
    twiddle
  • 00:18:13
    [Music]
  • 00:18:14
    your
  • 00:18:16
    thumbs
  • 00:18:18
    until user inputs data okay now
  • 00:18:23
    understand with a comment the Arduino
  • 00:18:25
    doesn't read it the Arduino doesn't do
  • 00:18:27
    anything but just if you read the code
  • 00:18:29
    you see that you're just looping looping
  • 00:18:31
    looping looping looping looping here
  • 00:18:33
    until the person puts the number in when
  • 00:18:35
    the person puts the number in serial.
  • 00:18:37
    available would become one this
  • 00:18:40
    statement would no longer be true you
  • 00:18:42
    would drop out and you would come here
  • 00:18:44
    okay I hope you understand it all right
  • 00:18:47
    but if you don't the bottom line is
  • 00:18:51
    serial uh while serial. available equal
  • 00:18:54
    equal 0 open curly bracket close curly
  • 00:18:57
    bracket I'm going to take this out now
  • 00:18:59
    okay I'm going to take that out and in
  • 00:19:02
    fact I'm just going to go ahead and just
  • 00:19:04
    put the empty Clause right here you see
  • 00:19:06
    there's uh there is nothing in that
  • 00:19:08
    clause and so this just makes the code a
  • 00:19:11
    little neater while serial do available
  • 00:19:14
    is equal to zero while no data has been
  • 00:19:17
    entered do nothing and then it just sits
  • 00:19:21
    at this line so if you if you if you're
  • 00:19:23
    not following what I'm saying I'm sorry
  • 00:19:25
    but just take my word for it this line
  • 00:19:28
    of code will make the program wait here
  • 00:19:31
    until the data has been entered once the
  • 00:19:34
    DAT so now we prompt the user to put the
  • 00:19:37
    data in we wait for him to put it in he
  • 00:19:40
    puts it in what do we have to do we've
  • 00:19:42
    got to read it it's like we say throw me
  • 00:19:44
    the ball he throws us the ball we got to
  • 00:19:47
    catch it okay so we said send me the
  • 00:19:50
    data he sends the data we got to read it
  • 00:19:54
    okay and how do you read it well you got
  • 00:19:56
    to say you got to put it somewhere first
  • 00:19:59
    well where are we going to put it what
  • 00:20:00
    are we trying to read here am I even
  • 00:20:03
    yeah what are we trying to read right
  • 00:20:05
    here okay what are we trying to read the
  • 00:20:08
    number of times that the red L that we
  • 00:20:10
    want the red LED to Blink well where is
  • 00:20:13
    that variable that variable is num red
  • 00:20:16
    blinks so what we want to do is we want
  • 00:20:18
    to say num red blinks blinks b e l i n
  • 00:20:24
    KS yeah I got to get everything just
  • 00:20:26
    right number Red blinks is equal to what
  • 00:20:29
    we're going to go do is we're going to
  • 00:20:30
    go read that number off of the serial
  • 00:20:32
    Port now so we're going to say serial do
  • 00:20:37
    parse in this is new stuff this is new
  • 00:20:40
    stuff okay serial. parent and then we
  • 00:20:43
    put our colon all right what is this
  • 00:20:46
    doing this is saying go out to the
  • 00:20:48
    serial Port can we do that yeah we turn
  • 00:20:50
    the serial port on up here so it knows
  • 00:20:52
    about the serial Port read a number from
  • 00:20:56
    the serial Port is what this is is
  • 00:20:58
    saying and what's really important here
  • 00:21:00
    is is that you need to make this command
  • 00:21:03
    match the type of number that you're
  • 00:21:05
    expecting we are expecting what type of
  • 00:21:08
    number numb red blinks is an INT so we
  • 00:21:11
    need to tell it to read an INT how does
  • 00:21:14
    it read an INT serial. parse
  • 00:21:18
    int okay and that puts the number then
  • 00:21:22
    into num red blinks the variable so
  • 00:21:24
    remember we create the bucket up here we
  • 00:21:27
    create the bucket up here the bucket is
  • 00:21:29
    empty it's got the label on the bucket
  • 00:21:31
    num red blinks and then down here I say
  • 00:21:34
    I'm going to put something in the numb
  • 00:21:35
    red blinks bucket go to the serial part
  • 00:21:38
    and read a what read an INT an integer
  • 00:21:43
    okay if we had made this up here if we
  • 00:21:47
    had made this up here a float we would
  • 00:21:49
    need to do a parse float okay so your
  • 00:21:52
    read statement needs to match the type
  • 00:21:54
    of variable that you're trying to read
  • 00:21:56
    all right
  • 00:21:59
    you could do if you're reading an INT
  • 00:22:02
    you do a serial. parse int if you're
  • 00:22:05
    going to read a float you do a serial.
  • 00:22:07
    parse float
  • 00:22:10
    okay reading a string is a little bit
  • 00:22:12
    differently if you're going to read a
  • 00:22:14
    string like if we wanted to read this
  • 00:22:15
    message we would do a serial. read
  • 00:22:18
    string okay serial. read string now
  • 00:22:22
    there's a hundred different ways to do
  • 00:22:24
    this and a lot of people go in and read
  • 00:22:26
    things one character at a time and do
  • 00:22:28
    all this crazy stuff and that's probably
  • 00:22:29
    a better way of doing it but I want you
  • 00:22:31
    to just learn something that's Rock
  • 00:22:33
    Solid that's simple to understand and it
  • 00:22:36
    will work for anything that you want to
  • 00:22:38
    do if you want to read an INT do serial.
  • 00:22:41
    parse int if you want to read a float do
  • 00:22:44
    serial. pars float if you want to read a
  • 00:22:46
    string do serial. read string so parse
  • 00:22:51
    in parse
  • 00:22:53
    float or read string okay those are the
  • 00:22:57
    three different ways that you can input
  • 00:22:59
    something okay so let's let's look at
  • 00:23:01
    what we've done here we've done serial.
  • 00:23:04
    print line how many times you want the
  • 00:23:06
    red to Blink then we wait and then we
  • 00:23:10
    read the number that they put in into
  • 00:23:13
    numb red blinks and then we're ready to
  • 00:23:15
    loot down here num red blinks now we
  • 00:23:18
    need to do that because remember we also
  • 00:23:21
    took o I think that should not be like
  • 00:23:23
    that
  • 00:23:26
    okay I think I yeah need to be more
  • 00:23:29
    careful in my code here okay so
  • 00:23:33
    basically uh what we are doing here is
  • 00:23:36
    we
  • 00:23:38
    are oh no I'm sorry ignore that never
  • 00:23:41
    mind I was on the wrong line okay uh
  • 00:23:45
    never mind all right here num yellow
  • 00:23:48
    blinks num red blinks we declare and
  • 00:23:51
    then down here what we do is we read the
  • 00:23:56
    number of red blinks so we prompt the
  • 00:23:59
    user for input we wait for him to input
  • 00:24:02
    it and then we read it in now also on
  • 00:24:06
    num yellow blinks we've declared that
  • 00:24:08
    but we haven't we haven't assigned a
  • 00:24:09
    value to it so we need to get that value
  • 00:24:12
    as well and so what we're going to do is
  • 00:24:14
    we are going to copy this and then we're
  • 00:24:17
    going to paste it because we want to do
  • 00:24:20
    the same thing for yellow but we want to
  • 00:24:21
    say how many times do you want the this
  • 00:24:25
    time it's going to be
  • 00:24:26
    yellow okay led to Blink and then we
  • 00:24:30
    wait again and then this time this is
  • 00:24:33
    going to
  • 00:24:35
    be num yellow blinks so how many times
  • 00:24:40
    do you want it to Blink we prompt the
  • 00:24:42
    user then we wait and then when he
  • 00:24:46
    enters it we read it into numb yellow
  • 00:24:49
    blinks and so at this time at this point
  • 00:24:52
    with this code we now know how many
  • 00:24:55
    times the user wants to Blink red and
  • 00:24:57
    how many times s the user wants to Blink
  • 00:24:59
    yellow
  • 00:25:01
    okay will it work I hope so let's go
  • 00:25:05
    ahead and take a look
  • 00:25:10
    right green green everybody's happy good
  • 00:25:13
    deal so now let's look at this serial
  • 00:25:15
    Monitor and what uh where is my serial
  • 00:25:18
    monitor come on maybe I got to pop it up
  • 00:25:22
    down here okay so look at this we have a
  • 00:25:25
    message waiting on the serial monitor
  • 00:25:27
    how many times do you want the red LED
  • 00:25:30
    to Blink I think I would like it to
  • 00:25:31
    Blink 10 times okay how many
  • 00:25:37
    uh I look at that I put that in the
  • 00:25:40
    wrong spot because that should not have
  • 00:25:41
    been there let's go back and look at
  • 00:25:42
    this code okay uh look at that I sent
  • 00:25:46
    this red message and I should not have
  • 00:25:48
    done that so let's take this cut you see
  • 00:25:52
    that was that line that was causing the
  • 00:25:53
    problem and that should be down here
  • 00:25:56
    like this
  • 00:25:58
    okay let me kind of put some blank spots
  • 00:26:00
    here so basically and let's go in and
  • 00:26:03
    put some comments here I got a little
  • 00:26:04
    sloppy there didn't I okay and so what
  • 00:26:08
    are we doing here okay we are prompt
  • 00:26:12
    user for input right what are we doing
  • 00:26:16
    here we're putting our comments in wait
  • 00:26:19
    for
  • 00:26:21
    user input and then after we get it what
  • 00:26:25
    do we
  • 00:26:26
    do read
  • 00:26:29
    user input okay and then here what are
  • 00:26:33
    we doing we
  • 00:26:36
    are
  • 00:26:38
    prompt user for
  • 00:26:41
    input what is this step four we wait for
  • 00:26:47
    input and
  • 00:26:49
    then this time we read user input okay
  • 00:26:56
    now do you see when I got kind of sloppy
  • 00:26:58
    and going too fast and I wasn't putting
  • 00:27:00
    my comments in I ended up with this red
  • 00:27:02
    message
  • 00:27:05
    here okay up there where it should not
  • 00:27:08
    have been and so uh now this should work
  • 00:27:12
    so we're going to serial print line and
  • 00:27:14
    get the number of red blinks and then
  • 00:27:16
    we're going to get the number of yellow
  • 00:27:17
    blinks and so now let's go in and see
  • 00:27:19
    see if this
  • 00:27:21
    works yes I do make
  • 00:27:24
    mistakes all right so let's see if we
  • 00:27:26
    can get our serial monitor
  • 00:27:28
    pop it up
  • 00:27:31
    here okay so it's waiting for data how
  • 00:27:34
    many times do you want the red uh led to
  • 00:27:37
    Blink you got to put the number in here
  • 00:27:38
    we're going to say 10 now you could
  • 00:27:40
    either hit send or you could click enter
  • 00:27:43
    on your keyboard let's go ahead and just
  • 00:27:45
    click Send here see how that does how
  • 00:27:47
    many times do you want the yellow LED to
  • 00:27:49
    Blink let's say one time okay and now
  • 00:27:53
    when I send this you be watching over
  • 00:27:56
    here and we should see this This Blink
  • 00:27:58
    10 times and one time so send okay and
  • 00:28:02
    look that's blinking Seven 8 n 10 and
  • 00:28:06
    then one okay let's try that again it
  • 00:28:11
    says how many times do you want the red
  • 00:28:12
    LED to Blink okay now why is it doing
  • 00:28:14
    that because where we put this code in
  • 00:28:16
    here we put it at the top of the Void
  • 00:28:18
    Loop so it's going to do the red blinks
  • 00:28:21
    it's going to do the yellow blinks and
  • 00:28:22
    then it's going to come back up here and
  • 00:28:24
    then it's going to prompt us again and
  • 00:28:25
    so this time let's do 10
  • 00:28:28
    on the red okay how many times you want
  • 00:28:30
    the red to Blink
  • 00:28:32
    10 okay how many times you want the
  • 00:28:34
    yellow to Blink let's do 10 on that okay
  • 00:28:38
    and then send
  • 00:28:40
    that okay and here we go three four five
  • 00:28:43
    6 7 8 nine 10 one 2 3 4 five six 7 8
  • 00:28:50
    nine 10 okay and then it comes back over
  • 00:28:53
    here and asks me again so this time
  • 00:28:56
    let's say five
  • 00:28:58
    okay and let's say five we come over
  • 00:29:01
    here one two 3 four five one two three
  • 00:29:06
    four five okay so you see it's working
  • 00:29:09
    and every time through it's going to ask
  • 00:29:11
    me how many times do I want the red to
  • 00:29:13
    Blink and how many times do I want the
  • 00:29:15
    yellow to Blink all right and then it'll
  • 00:29:17
    stop every time and so I can control it
  • 00:29:19
    why does it stop every time and let's
  • 00:29:21
    talk about that a little bit well where
  • 00:29:23
    we put this we put it uh inside of our
  • 00:29:25
    void Loop so here is our I mean we put
  • 00:29:28
    it inside of our void Loop so at the top
  • 00:29:30
    of the Void Loop the first hot thing
  • 00:29:32
    it's going to do is ask for the number
  • 00:29:33
    of red blinks and it's going to ask for
  • 00:29:35
    the number of yellow blinks then it's
  • 00:29:37
    going to Blink that many times red and
  • 00:29:39
    then it's going to Blink that many times
  • 00:29:41
    yellow and then it's going to print a
  • 00:29:42
    little blank line to make our formatting
  • 00:29:44
    nice and then this is the end of the
  • 00:29:46
    Void Loop right here and the void Loop
  • 00:29:49
    is a loop so then what is that going to
  • 00:29:52
    do that is going to come back up here to
  • 00:29:54
    the top of the Void Loop and it's going
  • 00:29:55
    to do this again what if we just wanted
  • 00:29:58
    to set this up and we just wanted to ask
  • 00:30:01
    the user one time for his input for like
  • 00:30:05
    the number of red blinks and the number
  • 00:30:06
    of yellow blinks and then just want it
  • 00:30:08
    to sit and keep doing that over and over
  • 00:30:10
    and over and not ask every single time
  • 00:30:12
    so if we wanted to just ask for the user
  • 00:30:14
    input one time where would we put things
  • 00:30:17
    that we wanted to just do one time well
  • 00:30:19
    we could put it up here in the void
  • 00:30:20
    setup and so what if we got all this
  • 00:30:22
    data here and we come up and let's just
  • 00:30:25
    say we cut it because we don't want it
  • 00:30:27
    to do it inside the void Loop anymore we
  • 00:30:30
    want it to do it inside of the Void
  • 00:30:33
    setup and
  • 00:30:35
    uh I do like to try
  • 00:30:37
    to keep my uh indenting kind of neat
  • 00:30:41
    here so that I see all this indented
  • 00:30:43
    stuff is the setup
  • 00:30:49
    uh okay and I also like to put kind of
  • 00:30:52
    spaces in my program so I can kind of
  • 00:30:54
    see things that are uh so I can sort of
  • 00:30:57
    see how things do you see how like I
  • 00:31:00
    indent this so that it's natural for me
  • 00:31:02
    to see this int indentation is all the
  • 00:31:04
    void setup and then the void loop I
  • 00:31:06
    probably should go in and try to do
  • 00:31:08
    indentation on uh on that as well to
  • 00:31:11
    kind of keep things
  • 00:31:13
    uh easy to read now it doesn't really
  • 00:31:16
    matter the white spaces Arduino doesn't
  • 00:31:18
    look at if you're writing in python
  • 00:31:20
    python actually does commands based on
  • 00:31:22
    the white spaces so the indention is
  • 00:31:25
    very important in Python uh it's not
  • 00:31:27
    important in Arduino but I do think it's
  • 00:31:29
    part of writing good and readable code
  • 00:31:31
    and code that's easy to uh easy to debug
  • 00:31:34
    so anyway now we've moved those prompts
  • 00:31:36
    up to the void setup the void setup you
  • 00:31:38
    do one time so I think this time what it
  • 00:31:41
    should do is it should ask us one time
  • 00:31:43
    for blinks and one time for the yellow
  • 00:31:45
    blinks and then it should just sit and
  • 00:31:46
    blink back and forth so let's let's take
  • 00:31:48
    a look at this and see what
  • 00:31:54
    happens okay it looks like it downloaded
  • 00:31:56
    uh with everybody happy and so let's see
  • 00:32:00
    how many times do you want to Brink
  • 00:32:01
    blink the red LED I'm going to say five
  • 00:32:05
    okay how many times do you want to Blink
  • 00:32:07
    the yellow LED I'm going to say five and
  • 00:32:10
    then let's look over here and see if it
  • 00:32:12
    does it one two 3 four five one two
  • 00:32:15
    three four five one two three four five
  • 00:32:19
    yellow yellow yellow yellow yellow y red
  • 00:32:22
    red red red okay so you see it's going
  • 00:32:25
    back and forth and also you can see that
  • 00:32:26
    it's synced up nicely here with our uh
  • 00:32:29
    earlier print statements that are
  • 00:32:30
    showing which uh which led uh which led
  • 00:32:32
    is blinking so that's pretty cool I mean
  • 00:32:35
    you can see that where you put stuff in
  • 00:32:37
    the code starts affecting what happens
  • 00:32:39
    and and you can see that if we in the
  • 00:32:41
    void setup you're kind of asking for a
  • 00:32:43
    onetime deal and then you do that over
  • 00:32:45
    and over and over but also what you can
  • 00:32:48
    do here is you can put it down here if
  • 00:32:50
    you would want it to do you could put it
  • 00:32:52
    down here in the void Loop if you would
  • 00:32:54
    like it to do it separate every time
  • 00:32:56
    okay okay so your assignment is to play
  • 00:32:58
    around with this and and basically what
  • 00:33:00
    I'd like you to do is go in and take all
  • 00:33:02
    of these uh I mean don't input for what
  • 00:33:04
    the PIN numbers are because the pin
  • 00:33:06
    number should not be something that the
  • 00:33:09
    user inputs right you're the programmer
  • 00:33:11
    you're the one that set this circuit up
  • 00:33:13
    you have to tell it that red LED pen is
  • 00:33:15
    nine and you have to tell it that red
  • 00:33:17
    LED pin is 10 but these other things
  • 00:33:20
    like the red on time the red off time
  • 00:33:22
    yellow on time yellow off time those
  • 00:33:24
    would be things that you should enter
  • 00:33:26
    from the uh that you should enter from
  • 00:33:29
    the uh from the serial port and so what
  • 00:33:31
    I want you to go in is is modify the
  • 00:33:33
    program where you ask the user for all
  • 00:33:36
    of the information maybe you would ask
  • 00:33:38
    for some of it in the setup maybe these
  • 00:33:40
    blink parameters how long it's on and
  • 00:33:43
    how long it's off you maybe only do that
  • 00:33:45
    one time do it in the void setup but
  • 00:33:47
    then maybe do the number of blinks every
  • 00:33:49
    time and so you would do that in the
  • 00:33:51
    void Loop okay so play around with this
  • 00:33:53
    and what I'm going to do for a grade is
  • 00:33:55
    I'm going to see that you've gone and
  • 00:33:56
    you youve put put more parameters in
  • 00:33:58
    there and also just make it do something
  • 00:34:00
    else I mean just think hey this is what
  • 00:34:01
    I want it to do and kind of come up with
  • 00:34:03
    your own goals and strategies and then
  • 00:34:05
    sort of develop some of your own code so
  • 00:34:07
    at a minimum I want you to do what I've
  • 00:34:10
    done and change uh everything except the
  • 00:34:12
    PIN numbers into things that you input
  • 00:34:14
    that's the minimum but what I'd like you
  • 00:34:16
    to do is kind of go beyond and say well
  • 00:34:17
    what I would like my LEDs to do is I
  • 00:34:19
    would like them to do this and then this
  • 00:34:21
    is the way my program looks so you can
  • 00:34:23
    sort of start taking this uh your own
  • 00:34:25
    Direction okay I appreciate it this is
  • 00:34:27
    six lessons down and I will see you
  • 00:34:30
    again very shortly in Arduino lesson
  • 00:34:33
    number seven take it easy
  • 00:34:36
    guys
标签
  • Arduino
  • Programming
  • User Input
  • Serial Monitor
  • Code Debugging
  • LED Blinking
  • Input Parameters
  • Functionality
  • Microcontroller
  • User Interaction