Serial Communication with Processing :: Video #2 :: Arduino Serial Communication Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello I hope you're doing well thank you so much for joining me all right so in this tutorial what we're gonna do is set up a simple communication between our Arduino and a circuit we set up and a sketch in processing so it's going to be pretty straightforward and I just kind of want to get you familiar with the Arduino side and a little bit of the processing side of this information pass okay so in order to do this sketch all you need is your Arduino you're going to need a push-button momentary push button one 10k resistor 10k ohm resistor two jumper wires and a breadboard now setting up the circuit we're just putting together a push button and the push button is going to be connected to pin number four so one side of the push button with a jump wire goes to pin four on the Arduino and then on that same side of the push button we're going to connect the 10k ohm resistor to ground okay so basically when the button is not being pressed pin four is always going to see ground right because it's directly connected to ground now with the other jumper wire we're gonna take the other side of the push button and we're going to connect it to five 5 volts but the only time pin four is going to see five volts is when we press the button okay so just a straight forward button circuit alright so that's it let's go ahead and jump in to processing alright so here we are in processing what we're gonna do is go up to file examples it's going to open up a little applet here and that's gonna be closed so we're going to do is come down to libraries and we are interested in the serial library so we'll click serial here and then where you want to open up cereal read all right so let's take a look at this sketch okay so we've got some comments at the top we're used to seeing comments that's pretty straightforward we come down here and here's a line of code import processing dot serial dot asterisk and basically what this line of code is doing is it is importing the serial library for processing not much more to say there I mean if you've gone through the library tutorials you know what that kind of entails so there's some functions available that are associated with a class we want to be able to use that class in processing okay here we are initializing and declaring variables again very similar to what we'd see in Arduino so here what they're actually doing is creating an instance of a class again if this sounds a little confusing just kind of review those library videos and it will make more sense but they are making a instance of the serial class and it's called my port there's going to be functions associated with this my port that allow us to read and write information to and from the serial port okay really enough about that and then this is kind of an interesting variable here it's an integer and it's named Val and what val is going to do is it is going to hold the information that we collect from the serial port and so let's just do a quick review of how this is going to work Arduino is going to write information to the serial port and processing is going to check the serial port see if anything's there and it's gonna take the information in the serial port it's going to pull it out and it's going to assign it to that variable Val and it's that that's kind of that's it alright so moving on we can see there's a void set up just like arduino has a set up mandus it that's why i really went with processing it's got these very these similar aspects to it now inside the curly brackets would set up we've got a function cult size and we're not too worried about this this is going to determine the size of the applet window that opens when you hit run in processing and then we've got some comments here and these comments are very important because they talk about this line of code right here that can be just a bit tricky so we're going to try to talk through this but it's actually a lot easier to just kind of see and mess with than to talk about but we're going to talk about it just shortly so here's the deal your Arduino talks to your computer using a serial port so let's just I'll just point it out real quick here's our Arduino program when we go to tools and we go to serial port it's gonna list all the serial ports that processing would be looking at or Arduino would be looking at alright and there's gonna be a little check mark next to the port that your Arduino is using ok so again let's this is your do we know IDE you go to tools serial port and then you just check which port are you using now a Mac this is usually what the serial ports look like something similar to this ok on a PC usually it's like com1 come to comm 3 come for something like that now it's actually much easier on a PC when we're trying to figure out what port we want to talk to because generally speaking Arduino will default to your first comport ok and so you won't have to change this line of code at all alright this because with this line of code is let's just take a let's just take a look at this line of code what it's saying is it's creating a string and a string is just a line of characters it's naming that string port name and it's setting it equal to a function and called serial dot list but more specifically it's setting it equal to the first element in that function alright so serial dot list that's a function and then this is specifying the element inside the first element inside that string okay that syntax is a little different than you're used to seeing but that's just how it is so zero here generally works on a PC here's one way to figure out if if this doesn't work as as this setting when you hit run and and you get some type of error nothing's communicating what you can do is type this simple function it's called print line you've probably seen it before and what you're going to do is just print that function right there serial dot list and what this is going to do is it is going to print off down here all of the serial ports okay and then you'll know which which one to choose now I happen to know I want to say it's like five oops I happen to be using the fifth one all right so you know I'll tell you what I'm just gonna run this real quick and I will minimize that so here's down here the listing of all the serial ports again these are the exact same ones that I just showed you over on my Arduino there's the Bluetooth incoming fourth the Bluetooth modem the USB 141 Bluetooth Bluetooth bloody blah and then here's this last one TTY Bluetooth 141 see the line it wraps around here so all I'm doing is I'm just counting counting these out here's one two three four five six alright so it is the sixth port now you're probably asking why do you have five in there then well strings are indexed just like arrays so the index of a string starts at zero so this is actually the fifth element in the character string okay so that probably makes sense all right so basically and another way to do it is just to try you know try some different numbers after you list your serial port if you're still not sure okay try it you know try one it may work it doesn't work try to I mean that's that's kind of like bulldozing it but that's one way to try to okay so I'm gonna go ahead and stop this now alright so there we are string port name equal serial dot list all right so we're finding our serial port the final line of code here in the set up is initializing the serial the instance of that serial class okay so again really not too worried about it I mean you can see they're just specifying the baud rate the port name which is going to be the port that we just identified and then this is just a really funky syntax form a name for setting up a class and processing so really not too worried about it okay so again the only thing you're really going to be interested in is these two lines of code and it's probably going to work just fine if you're on a PC you might just have to change that number here there and if you're on a Mac you'll almost absolutely have to change that number but you can use the print line serial dot list kind of set up here to do that before we move on to void draw I am going to draw your attention to the very bottom of this sketch and you'll notice there is a there's some code down here that has been commented out and up here it says wiring slash Arduino code so what they've been kind enough to do is include the code that we can load on on our Arduino so all I'm going to do is I'm going to copy this and I'm gonna come to our dueƱo and I'm gonna paste it in here so all this code that they gave us I can throw right into my Arduino that's how awesome the folks up at processing are that they did that for us okay so before we look at the draw portion of processing let's look at this Arduino code so this should all be more familiar to us so here up at the top they got some comments all right and basically it's code for sensing a switch status and writing the value to it the serial port so here int switch pin 4 so this is the switch is connected to pin 4 here we're using a button switch doesn't make a difference we'll be happy there just an integer variable we move in to set up and in set up we're just declaring the mode of the switch pin which is going to be an input because again we want to read information and from the button we want to know is it being pushed or is it not is it high or is it low and then we want to start serial communications so we do serial begin and set the baud rate at 9600 all right nothing crazy here so now we come down to void loop and it stays not very crazy so what I'm try what I mean to say is it stays relatively simple so let's take a look at what we've got here we've got an if statement actually an if-else statement in the first condition in this if statement says digital read switch pin equal equal high so what is that what is that asking so if the digital read of switch pin if the output of digital read is high then do this right here so basically what's that's what is that saying that's saying if we're pressing down the button we know that when we do a digital read at pin four it's going to return the value high all right so if we're pressing the button do this what will we do well we use a function called serial dot right and we send the value one so you've seen serial print and now here is serial right and the only difference is that serial right is actually sending bytes all right so it's going to encode that value one in a series of bytes as opposed to serial dot print which actually sends the characters okay so we're not interested in the characters we're interested in the bytes because that's what processing isn't going to be interested in the bytes okay so we do serial doubt right I mean it's that simple we send a one or if we're not pressing the button what do we do well if we're not pressing the button the buttons up then we just do a serial down zero and that closes the FL statement and then we delay 100 milliseconds and then we do that all over again so we're constantly checking what is the state of the push-button and based on that state we're going to be sending a value to the serial port and that is it that's it that's all you have to worry about on the Arduino side you know how to send information already so you're pretty much you're kind of a master at this already so you can imagine right now we're just sending a discrete value like a 1 or a 0 but you know and that's because we've got a button hooked up but could you imagine if we had a potentiometer hooked up that maybe we could send an entire range of values or if we had a sensor hooked up we could send an entire range of values all right and we're gonna be sending that using the serial right function all right so again this is just a really simple example of how we're gonna handle it on the Arduino side but it really doesn't get much more complicated than this which is nice to know all right so now now we know how we're getting information from the Arduino to the serial port now we need to figure out well how are we going to grab the information from the serial port into processing so let's go back to processing ok so here we are we remember we had just finished the setup we figured out which port to point our our new instance of the class - and now we're in void draw and draw is - processing which loop is - Arduino all right it holds all the big stuff so what's the first thing we do here and again we're not gonna get too crazy in depth on this because we don't have to but let's just take a look at it so the first thing is an if statement and it says if my port dot available is greater than 0 then do something right so my port dot available what that's basically saying is hey look at the serial port if there's any value in there greater than 0 then that means some information has been sent ok and the the dot available function is specifically looking at the bytes that are in the serial port not the value because you might say well hey well what if I passed the number zero well we passed the number zero but we sent it as a series of bytes okay so it's not looking is there the value zero at the serial port it's looking is our are there any bytes basically at the serial port okay that's what the my port not available is looking at so I know that could be a little bit confusing because you're thinking well hey we're sending a zero you know shouldn't we have it said as something else but anyway all right so what do we do if if it is available well what we're gonna do is we see this Val variable that we set before we're gonna set it equal to my port dot read so I could imagine I mean that's pretty intuitive name right there my port dot read is going to read the value at the serial port into this variable value all right so now value is gonna hold whatever value we sent all right so my port that read it reads the bytes and then it's gonna sign the value of those bytes to this variable which happens to be Val hey so that's easy now this this is now we get into this some more processing very specific processing code here and it's it's background is 255 so the background color this this function sets the color of the background of the applet all right and so we're sanding we're saying set the background to white I'm not worried about that and so now here's really where it gets a little interesting now it says if val is equal to zero so what's that for us for us that means if we're not pressing the button then we're gonna fill with zero zero happens to be black okay so 255 is white zero is black all right so we're gonna fill ends up being we're gonna fill a little square with with black otherwise sowhat's otherwise for us you know else so either we're not pressing the button and the values zero or we are pressing the button the values 1 so that would fall into this else category then we're gonna change the color to a light gray which is somewhere between zero and 255 204 so that's a light gray and then what it does is it draws a rectangle with those dimensions okay does this seem straightforward enough okay so first thing we're gonna do is I'm going to go to my arduino ide i'm gonna upload the sketch and i hope it works alright so it's uploading and i can see the transmitted light is on on my arduino board and i know that you know i can see the light blinking and that makes sense because i'm not pressing the button alright so i'm not pressing the button and the else statement says hey if you're not pressing the button you know if the pin is not high right zero okay so that's easy enough so what I'm not gonna do is go up to tools and go to serial monitor because if you do that then processing is going to say hey some other program is already messing with that serial port I'm not allowed in there so you don't want to do a serial monitor now if you want to do some things you can do when debugging this is to do a serial dot print and print the same value you know since print that value to the serial monitor and then you know do a little serial monitoring and so you can see what you know make sure the correct values are getting sent so that's just one troubleshooting thing but just make sure that you don't open up the serial port when you're trying to write to processing so now I'm going to come into processing I'm gonna hit run and here's the little applet that gets opened I've got a black square because remember if val is equal to zero then it's gonna fill it zero and again zero is black and so this little rectangles filled with black now I'm gonna press the button oh hey look the button is filled with gray alright so else fill with 204 which happens to be the color gray okay hey I mean that's kind of neat alright look I think it's neat I bet my kids would love that so that's really what I wanted to demonstrate I wanted to show you how easy it is to get your Arduino to talk to some type of int immediate program and in this case we're using processing it's really that easy and you know it doesn't generally speaking on the Arduino side it really doesn't matter what that intermediate program is running on your computer you know we could have some Python script or we could have you know some type of MATLAB script or whatever running it's all gonna pretty much you know on the Adreno side it's pretty much gonna look the same you're just doing a serial dot right pretty easy huh so hey that's it thank you so much for listening to this tutorial I cannot wait to see you at the next tutorial make sure to do the challenge alright see you next time bye you [Music]
Info
Channel: Programming Electronics Academy
Views: 51,179
Rating: 4.94382 out of 5
Keywords: Serial Communication, ardui, Processing (Programming Language)
Id: 69EJkn65T_8
Channel Id: undefined
Length: 21min 15sec (1275 seconds)
Published: Wed Feb 19 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.