Arduino - Processing: serial data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to take a really quick look how to pass serial data from your Arduino board into a processing sketch to do whatever you want with it we're going to add a super simple error checker that make sure that all your serial data is read and the actual data has to be read before the serial data will do anything first I'm going to do and I'm going to start on Arduino and I am going to make a new sketch bring that over I'm going to save it and call it our communal to processing and since I'm doing the runs with the serial port embedded we don't need to do any special code apart from initialize it to do that we use the serial command dot begin and then a number in here what this number actually means is it's called the boundary if I open up the serial and reference and then we look again so set the data rate in bits per second bound for serial data transmission so basically the higher that number the boundary the more bits that can transfer per second the more bits you transfer the more unstable or get depending on how quickly your PC can process the data because Arduino and things like processing are all limited in the amount of memory licking the user access 96,000 9600 is a good sort of stable always available rate to use for transmitting data so what this means is we have now initialized the serial port for data upload we told Arduino to get ready on the serial port because we're about to put data into it capture data in Arduino you can set your write winner up however you want whether it's a button giving you a value of 1 or 0 or a dial that's gathering the value that's coming of that a resistor a photodiode anything there's lots of tutorials and loads of video out there for using Arduino boards and setup this is a way for you to send that data somewhere else so I'm just going to do int variable 1 equal to n 2 random 100 so whatever data you're using I have a a variable being randomized every loop that is between 0 and 99 simple what I'd then want to do is serial dot why they want to do is put the data on to the serial port now there's a few ways we can do this if we were just error checking inside Arduino we would normally do serial butt print people will also talk about serial dot right and depending on how you want to do it there are a few different ways as of our do we are doing a 1.0 in onwards 0 dot print and the entire serial port is called asynchronous which basically means that things happen in different orders so if I for example in Syria dot print variable one and then I upload that to my board and I open the serial monitor you'll see that this happens at a crazy fast rate but with print the serial monitor is being updated before the serial port so what I mean those Arduino calculates then we call it rendering or renders the value and then it transmits it if you reduce something like serial about right that would try and transmit the data even if it wasn't updated so you can run into a sort of duplicated variables are non available variables just depending on how you're using it what we are going to do is use the println command which stands for new print new light because the serial port adopts ASCII transmission the serial library has been embedded in processing sense processing to point on reading something to add it we need to go to processing sketch import library and then serial at the top you'll see that it will include something they will include this line of code called processing serial dot star includes the serial of project library and I was doing is that telling processing to go to a bank of cord stored in faders folders called serial we also need to initialize that library so I'm going to do serial which references up here my serial creates local 0 object from serial library do you will see I will should get error that I will get an error toll but if I remove this line of quartier it will tell me that serial does not exist and that's because it doesn't know what cĂ­cero command is about this library I'm also going to create a few variables I'm going to do a string called my string and this is going to be equal to null so we want that to be null when you load the patch and this is going to be where the variable to collect serial data I'm also going to do something called int a LS our new line you could call it equal to 10 and this is ASCII code for carriage return in serial so we're going to use this which is a 10 to validate whether we have finished receiving all the data on the serial port and they'll make a bit more sense in a second we're then going to do float [Music] my faul faul for stirring converted ASCII serial data so all data coming across the serial port will be a strength but we are going to tell processing to turn it into a float for us and you can use it as a number in setup we need to initialize our sketch so I'm going to size 200 by 400 swing up and that's tall box I'm going to do string we are we need to tell as with Arduino when you upload it and put it on the board we need to processing quite serial port to gather data off and link processing to serial port correct one as you can see from Arduino if we go to tools import you can see that I just have a lot of generic cereal information but we can use our kamenos to our advantage so I know that my board is on the 1 2 3 4 this port so I'm going to tell processing that string my port is equal to my port is equal to serial dot list for so serial dot list appears as an array and arrays start index 0 so I know that my serial port or my board is connected on the fifth so I need to go to the 1 2 3 4 5 v port hence why this is 4 and not 5 next I'm going to do is I'm going to initialize my serial ports I'm going in my cereal is equal to new serial this referencing sketch my port which port other data is coming in on the boundary and match that to the boundary on this cereal about the cereal and Arduino otherwise you'll just get really garbled information because you'll get different bits a tight way to visualize this if you open the serial monitor monitor and change the boundary you'll start doing all these question marks those are just purely bits of data that don't make any sense to the computer because it's receiving parts of packets rather than the whole thing so important that these numbers match the nine main 600 in the namesake subject so now as if we started processing it would create a two by four hundred panel start our serial port looking at the one that the board is connected to and that would be it or now we need to gather the data and then do something with it so in draw first thing I'm going to do is I'm going to check that there's information on the serial port for me to take so I'm going to do while Syria or my serial dot have fail above it's greater than zero do something so this is saying that go into this piece of code well there is available information on the serial port otherwise move on with the rest of the program then I'm going to strip everything of the serial port until I see that line return or that carriage return code so I'm going to do my string is equal to my serial dot root string until MML so it'd be 160 32 31 32 so that would be a letter a letter a space and it's going to keep reading that until it sees 10 which it knows as a newline and the second it receives 10 it's going to then bundle that data and make accessible for us the important thing to note to note is this is going to strip data of the port as an once processing has taken it it will no longer exist anywhere else I know what I'm going to do is I'm going to check to make sure that's valid so if my string does not equal no so if it's been able to capture data before it's reached the end of the line do something data was on the serial port background I'm going to refresh the background and then I'm going to do my file is equal to float of my strength takes data from serial and turns it into numbers so we know that because we're in this part of the code we know that the serial port exists and we know that the serial port contains data because I'm only sending numbers I'm going to take whatever data was there and turn it into a number so now if I was to print that print my foul play one second let me put that on a new line and then I'll bring the sketch window over so you see how about 200 by 400 bucks that does nothing but then I'm printing all these amazing values down here and this is where you could I could leave the tutorial but I'm quickly going to draw something with that value to show you how quickly it is to go from nothing to sending values from Arduino into processing and into your sketches so instead of printing it I'm going to do my file is equal to my file divided by 100 which is the maximum value it can receive I'm going to change that by the hype so now I my fire will be scaled between zero and four hundred then I'm going to wreck mode center rectangle we're going to draw a rectangle in the dead center of the screen that is the at the bottom of the screen so I need to do the height and because I'm doing record center I need to take away now my bow / - so that it adjusts so it's always on the bottom line I'm going to make 100 pixels wide and I'm going to make it my value tall so now all things go smoothly I'm going to command T ctrl T to auto farm at Mac Pro size and sketch I'm going to play it and then you'll see that this will scale between the bottom and the top of the sketch window depending entirely on four values come into the serial port and that is a really quick tutorial on how to go from the to go from data in Arduino through the serial port into processing to do whatever you want you
Info
Channel: Programming for People
Views: 76,691
Rating: 4.817143 out of 5
Keywords: arduino processing serial, tutorial, lesson, programming, serial data, processing serial
Id: NhyB00J6PiM
Channel Id: undefined
Length: 14min 58sec (898 seconds)
Published: Mon Apr 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.