Тёмный

Using Serial.parseInt() with Arduino 

Programming Electronics Academy
Подписаться 227 тыс.
Просмотров 46 тыс.
50% 1

🤩 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!

Опубликовано:

 

1 июл 2021

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 67   
@electrucs219
@electrucs219 Год назад
The way you explained the function is of a professional teacher. Thank you for this hard work.
@programmingelectronics
@programmingelectronics Год назад
Glad it was helpful!!
@kezhang64
@kezhang64 Год назад
It's a big help! thank you for your high-quality videos!
@programmingelectronics
@programmingelectronics Год назад
Great to hear! Thanks for listening!
@jon_raymond
@jon_raymond 3 года назад
This is really helpful and very well explained. Thank you.
@programmingelectronics
@programmingelectronics 3 года назад
Thanks for watching!
@mateuswaikamp3699
@mateuswaikamp3699 Год назад
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.
@programmingelectronics
@programmingelectronics Год назад
Thanks so much Mateus!
@SotPSotPSotP
@SotPSotPSotP Год назад
The best is not in english its in american ☺
@melodicachan
@melodicachan 2 года назад
Que buenos videos! ❤️ Se nota que sabe lo que hace, Juntare dinero para hacerme con uno de sus cursos!
@Richie_
@Richie_ 2 года назад
I'm signing up on Saturday.. Really looking forward to it all.
@programmingelectronics
@programmingelectronics 2 года назад
Thanks Richie!
@DanTran-if2jt
@DanTran-if2jt Год назад
Thank you, this is very specific!
@bhoomikshetty7688
@bhoomikshetty7688 2 года назад
This helped me a lot sir, Thankyou!
@programmingelectronics
@programmingelectronics 2 года назад
Glad it helped! Anything specific you were trying to do?
@Europayacht
@Europayacht 2 года назад
That was really something useful !!! .
@programmingelectronics
@programmingelectronics 2 года назад
Glad it was helpful!
@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
@freytube1
@freytube1 11 месяцев назад
Well done videos. Prefect voice to follow also
@programmingelectronics
@programmingelectronics 11 месяцев назад
Thanks!
@vikky452
@vikky452 2 года назад
very good tutorial sir, thankyou
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for watching!!
@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)
@joshuapitong899
@joshuapitong899 2 года назад
A lot of thanks.🙌
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for all your comments Joshua!
@RataJC91
@RataJC91 Год назад
this video is gold
@programmingelectronics
@programmingelectronics Год назад
Thanks Julio!!
@DodgyBrothersEngineering
@DodgyBrothersEngineering 3 года назад
Another great video. What does the : do in Serial.print(:dataIn); ?
@michaelcheich2481
@michaelcheich2481 3 года назад
Thanks! That ":" was an error in the typing - sorry about that :( I didn't think anybody would spot it!
@ToanLe-cm1zg
@ToanLe-cm1zg 2 года назад
so helpful
@programmingelectronics
@programmingelectronics 2 года назад
Glad it helped! Thanks for the note!
@godotology
@godotology Год назад
thank you
@programmingelectronics
@programmingelectronics Год назад
Thanks for watching!
@anujajacob9487
@anujajacob9487 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?
@programmingelectronics
@programmingelectronics 2 года назад
You should be able to call parseInt() again to get out the remaining 34.
@digitalzoul57
@digitalzoul57 2 года назад
What if we have many int values in the string ?
@ahealthylife3116
@ahealthylife3116 7 месяцев назад
Gerçekten süper anlatım olmuş türkçe alt yazı olması çok hoşuma gitti.sana çok teşekkür ederim
@programmingelectronics
@programmingelectronics 7 месяцев назад
Thanks!
@AzureSupremuZ
@AzureSupremuZ 3 года назад
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 года назад
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!
@HOKMAN03
@HOKMAN03 9 месяцев назад
Perfect
@robertherzog2087
@robertherzog2087 Год назад
How can you see what functions are in a class?
@rZERO_game
@rZERO_game 4 месяца назад
where we can find serial.cpp in arduino ide 2.3.2?
@DodgyBrothersEngineering
@DodgyBrothersEngineering 3 года назад
Any chance you can explain the difference between while (Serial.avalable() > 0) and if (Serial.available())
@programmingelectronics
@programmingelectronics 3 года назад
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 года назад
@@programmingelectronics of course, I had both in my code but forgot why I used both methods. Thanks for the reminder.
@chrismolloy131
@chrismolloy131 2 года назад
How do you receive a zero entry?
@terrygould3230
@terrygould3230 2 года назад
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 2 месяца назад
​@@programmingelectronics Thanks a lot. I was about to ask this.
@SPARKELECTRONICS
@SPARKELECTRONICS 2 года назад
"SKIP_NONE' was not declared in this scope" i am getting this error
@mtalhakhalid1679
@mtalhakhalid1679 2 года назад
There is another fubctuon itoa in arduino to convert to String like atoi convert string or char to int
@maxpolaris99
@maxpolaris99 10 месяцев назад
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? ;)
@mradulbhardwaj3147
@mradulbhardwaj3147 2 года назад
9:25 how to fix this 1000ms delay ?
@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 года назад
10:25 Incorrect. The timeout is between characters, not for the whole duration of parseInt().
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for pointing this out!
@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 3 месяца назад
The way you explained the function is of a professional teacher. Thank you for this hard work.
Далее
Serial Communication with Arduino - The details!
16:51
How to Organize Code
14:37
Просмотров 60 тыс.
▼ЕГО БОЯЛИСЬ МОНГОЛЫ 🍣
32:51
Просмотров 485 тыс.
One moment can change your life ✨🔄
00:32
Просмотров 19 млн
Using Serial.read() with Arduino | Part 1
10:30
Просмотров 130 тыс.
Arduino Sketch with millis() instead of delay()
14:27
Просмотров 225 тыс.
How to Use Arduino Interrupts The Easy Way
33:28
Просмотров 78 тыс.