Using Serial.parseInt() with Arduino

Поділитися
Вставка
  • Опубліковано 1 лип 2021
  • 🤩 FREE Arduino Crash Course 👇👇
    bit.ly/get_Arduino_skills
    Want to learn more? Check out our courses!
    bit.ly/3xodeuB
    **Get the code, transcript, challenges, etc for this lesson on our website**
    bit.ly/3gnR71M
    We designed this circuit board for beginners!
    Kit-On-A-Shield: amzn.to/3lfWClU
    SHOP OUR FAVORITE STUFF! (affiliate links)
    ---------------------------------------------------
    Get your Free Trial of Altium PCB design Software
    www.altium.com/yt/programming...
    We use Rev Captions for our subtitles
    bit.ly/39trLeB
    Arduino UNO R3:
    Amazon: amzn.to/37eP4ra
    Newegg: bit.ly/3fahas8
    Budget Arduino Kits:
    Amazon:amzn.to/3C0VqsH
    Newegg:bit.ly/3j4tISX
    Multimeter Options:
    Amazon: amzn.to/3rRo3E0
    Newegg: bit.ly/3rJoekA
    Helping Hands:
    Amazon: amzn.to/3C8IYXZ
    Newegg: bit.ly/3fb03X1
    Soldering Stations:
    Amazon: amzn.to/2VawmP4
    Newegg: bit.ly/3BZ6oio
    AFFILIATES & REFERRALS
    ---------------------------------------------------
    ►Audible Plus Free trial: amzn.to/3j5IGrV
    ►Join Honey- Save Money bit.ly/3xmj7rH
    ►Download Glasswire for Free:bit.ly/3iv1fql
    FOLLOW US ELSEWHERE
    ---------------------------------------------------
    Facebook: / programmingelectronics...
    Twitter: / progelecacademy
    Website: www.programmingelectronics.com/
    Are you trying to send integers over serial using parseInt with Arduino?
    Maybe you’re struggling to understand the difference between Serial.read() and Serial.parseInt(), and how they can work for you? Do you just need to know how to convert multiple digit char to integers?
    Then you’re in luck! In this lesson you will learn exactly how to use parseInt from the Arduino Serial library to convert multiple characters to a single integer. Get ready!
    OVERVIEW
    Let’s do a quick overview of what we’ll cover.
    A quick overview of Serial Communication
    Serial.read() vs Serial.parseInt()
    Code for converting chars to integers with parseInt()
    parseInt() details, like setTimeOut(), Lookahead Mode, and ignore
    Quick Serial Communication Review
    If you watched our lesson on using serial.read(), then you already know how to take serial input and convert the chars to integers by putting all the incoming bytes into a char array. If you want to learn that method, make sure to check out the lesson on Serial.read().
    That code worked great, but it was somewhat lengthy. In this lesson we are going to talk about using a function from the Serial library called parseInt().
    Let’s do a quick review of how serial communication works with Arduino.
    You Arduino has some hardware on it called a USART/UART that will allow you to receive data serially from a computer. It will store that data in a buffer called the serial receive buffer.
    WHAT’S A BUFFER?
    Now if the word buffer throws you off - don’t sweat it. You can think of a buffer like a bunch of horse stalls in a stable. Some stables are big and have a bunch of stalls - you can house a bunch of horses there, other stables are quite small, and you only have room for so many horses - you can’t take in more horses than you have stalls!
    A buffer is like a stable, but instead of having horse stalls, it has spaces in memory where data can be stored.
    Generally speaking, a buffer is a transient place for data storage in a program. Usually, you are receiving data into a buffer, and then reading it out pretty quickly - because more data will be coming in and you need to make space for it. This is not some hard and fast rule though.
    The serial receive buffer has room for 64 bytes. When data comes to your Arduino over Serial, each byte will end up in the serial receive buffer.
    It’s your job as the programmer to read the data out of the serial receive buffer and do something with the data.
    But how do you do that? Turns out there are quite a few ways!

КОМЕНТАРІ • 67

  • @electrucs219
    @electrucs219 Рік тому +6

    The way you explained the function is of a professional teacher. Thank you for this hard work.

  • @mateuswaikamp3699
    @mateuswaikamp3699 Рік тому +2

    Sorry about my bad english, but I need to coment one thing. Here in Brazil a lot of peoples say "If you want good quality content, you will need to research in english." Today I started to search in english, and you with your content prove this fact to me, this content is perfect. I wish learn a lot with you (maybe with your courses), thank you so much.

  • @kezhang64
    @kezhang64 Рік тому +1

    It's a big help! thank you for your high-quality videos!

  • @jon_raymond
    @jon_raymond 3 роки тому +3

    This is really helpful and very well explained. Thank you.

  • @melodicachan
    @melodicachan 2 роки тому +2

    Que buenos videos! ❤️ Se nota que sabe lo que hace, Juntare dinero para hacerme con uno de sus cursos!

  • @DanTran-if2jt
    @DanTran-if2jt Рік тому

    Thank you, this is very specific!

  • @Richie_
    @Richie_ 3 роки тому +2

    I'm signing up on Saturday.. Really looking forward to it all.

  • @bhoomikshetty7688
    @bhoomikshetty7688 2 роки тому +2

    This helped me a lot sir,
    Thankyou!

  • @krischlapek6939
    @krischlapek6939 Рік тому

    Great info! Would be great if you could explain how to convert say 8 bytes received from serial to a single array os something that could be further used? Cheers

  • @ipadize
    @ipadize 2 роки тому

    does it matter if the numbers are being transmitted as numbers or as text? (i want to send for example b200 from Nextion to arduino and 200 being a number)

  • @Europayacht
    @Europayacht 2 роки тому +1

    That was really something useful !!!
    .

  • @RataJC91
    @RataJC91 Рік тому +1

    this video is gold

  • @freytube1
    @freytube1 Рік тому +1

    Well done videos. Prefect voice to follow also

  • @vikky452
    @vikky452 2 роки тому +1

    very good tutorial sir, thankyou

  • @chrismolloy131
    @chrismolloy131 2 роки тому +1

    How do you receive a zero entry?

  • @digitalzoul57
    @digitalzoul57 2 роки тому

    What if we have many int values in the string ?

  • @robertherzog2087
    @robertherzog2087 Рік тому +1

    How can you see what functions are in a class?

  • @HOKMAN03
    @HOKMAN03 10 місяців тому

    Perfect

  • @joshuapitong899
    @joshuapitong899 2 роки тому

    A lot of thanks.🙌

  • @rZERO_game
    @rZERO_game 4 місяці тому

    where we can find serial.cpp in arduino ide 2.3.2?

  • @DodgyBrothersEngineering
    @DodgyBrothersEngineering 3 роки тому +2

    Another great video. What does the : do in Serial.print(:dataIn); ?

    • @michaelcheich2481
      @michaelcheich2481 3 роки тому +2

      Thanks! That ":" was an error in the typing - sorry about that :(
      I didn't think anybody would spot it!

  • @ToanLe-cm1zg
    @ToanLe-cm1zg 2 роки тому +1

    so helpful

  • @godotology
    @godotology Рік тому +1

    thank you

  • @mradulbhardwaj3147
    @mradulbhardwaj3147 2 роки тому

    9:25 how to fix this 1000ms delay ?

  • @anujajacob9487
    @anujajacob9487 3 роки тому +2

    If I type in "Write 20 34" on the serial monitor, I need to store 20 and 34 in two separate locations, if I use Serial.parseInt() then it will retrieve 20, discard "Write " in the serial receive buffer, now only " 34" remains in the serial buffer, is there a way to retreive this value 34 and get it stored?

  • @mtalhakhalid1679
    @mtalhakhalid1679 2 роки тому

    There is another fubctuon itoa in arduino to convert to String like atoi convert string or char to int

  • @SPARKELECTRONICS
    @SPARKELECTRONICS 2 роки тому

    "SKIP_NONE' was not declared in this scope" i am getting this error

  • @ahealthylife3116
    @ahealthylife3116 8 місяців тому

    Gerçekten süper anlatım olmuş türkçe alt yazı olması çok hoşuma gitti.sana çok teşekkür ederim

  • @AzureSupremuZ
    @AzureSupremuZ 3 роки тому +2

    Since parseInt() will ignore some non numbers like the " tacos" after extracting "314" from example you gave, I assume clearing whatever is left in the serial receive buffer would be important? If so how would you do that?
    Edit: Great video btw, I really appreciate the tutorials.

    • @programmingelectronics
      @programmingelectronics  3 роки тому +2

      That is such a great question!
      To answer your question, you can use serial.read() with while loop to clear the rest of the buffer - you read out byte until serial.available() is equal to 0.
      Side note: It may be the case that the "other stuff" in the buffer is important, and you may want to read that in as well - which is why I am guessing this function does not just clear it for you.
      Hope this helps some!

    • @AzureSupremuZ
      @AzureSupremuZ 3 роки тому

      @@programmingelectronics thanks!

  • @DodgyBrothersEngineering
    @DodgyBrothersEngineering 3 роки тому +2

    Any chance you can explain the difference between while (Serial.avalable() > 0) and if (Serial.available())

    • @programmingelectronics
      @programmingelectronics  3 роки тому +5

      Great question! Here is my take...
      The "while" will keep you in it's block until the serial buffer is empty, where as the "if" (assuming something is in the serial buffer) will enter and then exit , regardless of how many bytes are left in the buffer.

    • @DodgyBrothersEngineering
      @DodgyBrothersEngineering 3 роки тому +1

      @@programmingelectronics of course, I had both in my code but forgot why I used both methods. Thanks for the reminder.

  • @terrygould3230
    @terrygould3230 2 роки тому +1

    If I wanted to send multiple values separated by commas e.g. 1243,9876,345,123 and have the Arduino save these in 4 variables e.g. data1, data2, data3, data4 how would this be done?

    • @programmingelectronics
      @programmingelectronics  2 роки тому

      Great question! Many ways you could do this, but I think Serial.parseInt() would work paired with an array. You would want to run a loop perhaps to store these values in an array, and every time through the loop, you increment the array to save the next value. If you know there will only be some many comma separated values coming in, you could then copy those saved array values over to your variables. Hope this makes some sense!

    • @nicolaslac1013
      @nicolaslac1013 2 роки тому

      Hi, Many thanks for these useful videos. But this answer doesn't make sense to me. I've called again the parseInt inside the Loop but it keeps only the last value entered, then if there is 2 values, 10 and 50 to add each other , the result is 10+10 or 50+50. But not 10+50. Any video any idea to solve this ? Thanks a lot

    • @vikaspanse2105
      @vikaspanse2105 3 місяці тому

      ​@@programmingelectronics Thanks a lot. I was about to ask this.

  • @maxpolaris99
    @maxpolaris99 11 місяців тому

    carriage return is ascii 13, if your data is 13 how does it know the difference in skip whitespace?

    • @astrabajtyt
      @astrabajtyt Місяць тому

      if you type 13 it will be 31 and 33 in ascii as those numbers are represented by other numbers ;) a bit confussing isn't it? ;)

  • @warrenscorner
    @warrenscorner 2 роки тому

    Great video! This really helped me understand parseInt better. I’m hoping that you will answer my question. I’m sending data from Python as a string. The string would be 12,27,54
    . My Arduino code is
    Int fn=Serial.parseInt();
    Int sn=Serial.parseInt();
    Int tn=Serial.parseInt();
    lcd.print(fn); lcd.print(“,”);
    lcd.print(sn); lcd.print(“,”);
    lcd.print(th);
    So my display now shows 12,27,54 but after repeating this exactly 9 times my display will be 12,27,5412! Then it will start to display 27,54,12.
    I’m thinking that I’m leaving something in the buffer and it’s picking it up after 9 iterations.
    How do I fix this?
    Thank you!

    • @agar.iodestiny8677
      @agar.iodestiny8677 2 роки тому

      what was it

    • @warrenscorner
      @warrenscorner 2 роки тому

      @@agar.iodestiny8677 Just what I thought. I cleared the buffer by doing a serial read and the problem went away.

    • @naasikhendricks1501
      @naasikhendricks1501 Рік тому

      @@warrenscorner serial.flush(); and serial.availabletowrite should advise.

  • @oqibidipo
    @oqibidipo 2 роки тому +1

    10:25 Incorrect. The timeout is between characters, not for the whole duration of parseInt().

  • @user-oy7vn6mk1t
    @user-oy7vn6mk1t Рік тому

    Is there anyone who know library or function that can count an example like: " 2.8+(4-1)*(6/10) " ?

    • @0x8badbeef
      @0x8badbeef Рік тому

      OneNote. Put a "=" after that and press the "space bar" 2.8+(4-1)*(6/10)=4.6.

    • @0x8badbeef
      @0x8badbeef Рік тому

      Also OneNote, functions like log, sin, cos ... works as well.

  • @ahmadjaradat3011
    @ahmadjaradat3011 4 місяці тому

    The way you explained the function is of a professional teacher. Thank you for this hard work.