RobotC for NXT Tutorial Part 6

hello this is Schuyler and bringing you the next episode of robot c4 Mindstorms tutorial so very quickly once again as always the music is by discord so check out is SoundCloud and download a stuff it’s pretty cool and let’s get to work today we are doing Bluetooth communication so we’ll get started with that the first thing we need to do is actually sync up the NXT sync them up together so I got two MINIX T’s and the first thing is turn them on so these are both running the same firmware I’m not sure if that really makes a difference but just be aware that if your thing doesn’t work check the firmware on both alright so the names of the bricks are kind of important it’s a little bit hard to see that that’s better so I got master and slave and that should make it fairly easy in this tutorial to figure out what’s going on the master is sending stuff to the slave and later will send stuff from the slave back to the master I think maybe but for now we’re just sending from the master to the slave so masters the boss so um the first thing that I want to call your attention to is right to the left of the name there is status stuff like there says USB in us because this brick is plugged in you must be and the other ones not and we need to turn bluetooth on because there’s a Bluetooth symbol that should pop up when it’s on and so you’re gonna go left or right just longer way around and get to bluetooth and you want to turn both of those on and turn it on and then I will say hey we’re gonna turn it on and you want to make sure your visibility is visible because that matters and you can you got all this stuff you want so you go to the search and I will use the master to search for the slave and you only have to do it on one and so this is gonna search for everything that it can find that has Bluetooth and if there’s a lot of if there’s a lot of devices on the network then it’ll take a long time to populate and so their slave and it’s just the name of the other brick and there’s a cell phone on here but since we’re not doing that we’ll just do the slave connect and this right here the the NXT can connect to three different devices at once and these this is basically which slot you want to connect on and so I’ll just pick number two because it’s in the middle and it’s the it’s the only even prime number that’s bigger than zero and because I’m a nerd like that be consistent with this and we’ll get into this later but you can think of it as like your second connection or your second best friend and or is something and this could change like you could connect it on a different channel at a different time but since I want to connect right now it will choose whatever I whatever I’d tell it to right here so I’ll pick to you know connect and on both entities it’ll show a passkey and the master defines the passkey the slave has to get the passkey that the master set and since I’m not really worried about security and it’s gonna hit okay mo since it’s one two three four and you’ll see right here there’s a Bluetooth symbol it’s got the B and it has a diamond and when the diamond is completed when it’s four sides they are connected I will show you what happens when I turn 1 and XT off on here is now half a rectangle it’s no longer it’s no longer a full diamond its half a diamond and so they’re disconnected that’s basically saying hey blue twos on but we’re not connecting anything and right now that’s how we need it to be and so bluetooth on theirs they know they have each other as a contact and and just to verify that and go to bluetooth my contacts this says master for that and this says slave for that and so that’s what we need now let’s get to the programming bluetooth menu there we go and all right it’s the programming we need a master program to send information in a slave program to receive it so there’s new and I’ll save the program as not in firmware I will save it as a robot see well this master master for Bluetooth all right so tasks main all that good stuff and I’ll compile the program so that I get the function library and here’s what we’re

gonna do for a robot see they make it really really easy for programming stuff especially with this function library I can’t stress how important the function library is because it’s just so helpful we’ve got a bluetooth and you go to intrinsic s– and it has a whole bunch of functions that you can just call and it will automatically send stuff and so the first thing we need to do is we need to connect the devices unless you want to do that every time you start your NXT up you want to go into that menu and hit connect I don’t like doing that I want the NXT to do that automatically and so there is this function called BT connect and it attempts to connect BT devices with specified specified name on a port and so the port range that’s what we talked about like when we connected those it connected it I’m on one two or three that’s what this is doing right here and so I can drag that in and I need to change this port to what I wanted to connect on I could either use one two or three but I don’t I’ll just do two because that’s what I’m going with and this is the name and so this is going to run on the master and it needs the name of the connection or the name of the other device which is slave and for yours it might be something else but yeah and then semicolon in the end so that right right here this program this connects the master to the slave and just for kicks let’s try running it and see see if it works plug that in it’s got connection to USB HID up five this right here is basically saying hey you this brick isn’t the last one I talked to you this is a different brick and that’s because there’s two bricks and so just hit okay that’s over it’s alright and get back to you cam and I’ll go into the file system and master B right now all it should do is connect and as you can see the diamond the diamond next to the B is complete and it wasn’t before and so mmm that program worked that line is as they okay and let’s go in to Bluetooth or to bluetooth and you can look at connections and slave is right there and you can disconnect from if you want and yeah so that’s that’s there as well so we can connect the vise but now I want them to actually send useful information so there is another function well there’s a lot more functions but we’ll just focus on one there is c CM message right to bluetooth and this right here is the one we want I’ll explain why in a second it has data length and query ID the query ID is basically this thing called a mailbox and what the mailbox are you’ve said to see you zooming out you’ve got your your network your network has two devices or more and just in this case just two and they’ve got their channels channel 1 channel 2 channel 3 and on that channel we can have a connection between those two devices and so on channel 2 we’ve got this connection now the master can send information to the slave to a particular mailbox the each NXT has 8 mailboxes per channel and so I could send some motor I could send like some particular information maybe about sensors on into mailbox 1 but if I wanted to send a message like hello I could do it on the box too and so it’s just mailbox oh well maybe there’s a lot more mailboxes but yeah I guess there are a lot more mailboxes must been an update in the firmware anyway um so just a mailbox one and you want to make sure it like wouldn’t the program that’s looking for it it has to be looking at the same mailbox because anything there and then P data P data is special and what it is although I suppose you can’t really tell from here it is looking for a unsigned byte array and so at the top here I’m going to type you byte outgoing message and put a square brackets and – this right here if you watch lesson 2 tutorials this right here is a declaration of an array what that is is basically a variable that has a whole bunch of itself like it’s it’s a you bite you bite means unsigned byte it’s a

color it’s a variable that can only have positive numbers up to a certain point and that’s alright that’s what the Bluetooth that that’s what it wants this to means that there’s actually two you bytes that are stored under the name of outgoing message and you can access them by saying which one it is so like if I want to actually set this to a value first of all I could either do that and it says all right the first first part is zero the second part zero or I could do like you know actual numbers 56 and 10 pull the stick with that and then for pietà we say is how going message and it sends the values of outgoing message now the length the length is how many numbers are there is to this length like that number right there needs to absolutely match that number this that has nothing to do it like but those two twos are none related this two has to be the same as this to all right so this sends the message and semicolon at the end so yeah that is like the basic program that sends a message that connects and then sends a message so now let’s start another program so that we can actually read the message because I mean if we just did this I could just say hey it works but you might not believe me and I want you to believe me and I want a program to work so I’ll save this one has let’s say the other one is master B else say slave beef so slave B we need a lot of times what I like to do is make the program symmetrical like obviously you can’t have a little bit command a command to connect because the master connects but like this same format is what I want for the slave so I’ll start this one started with outgoing message I’ll start with in going that’s this Jahl just copy this paste it in and change outgoing message to incoming message just so that yeah and since the incoming message will overwrite these values I’ll just leave it blank that basically just says hey there’s this array of unsigned or of unsigned bytes but I’m not going to tell you what they are yet and so robot sees a good with that so task main inside here it’s already going to connect but because because we want these programs to be versatile this program is going to need to be different it’s going to have to wait for a response like if we just ran this then if there wasn’t a message then it would just be like oh well there wasn’t a message for me to read sadface I’m done I’m gonna have a while loop well true and what its gonna do is it’s going to then read the message and so it’s going to keep on running it’s going to keep on looking for new information instead of just looking there one time to see if there was anything and then ending alright so for here there was C CMD message write to bluetooth we want to read function and so flip over to the intrinsic of Bluetooth and there is message read here we go get that in there P data that is the where it’s going to write the data and so we want in coming message and length to read we want to just like there’s two here there’s two here and here’s that here’s the huge big thing oh and this will be mailbox one this right here needs to be exactly the same as this right here as far as the mailbox and the length and and the length of the right you’re sending because here’s the thing this right here is gonna wait until two pieces of information have been sent but if this waited for for like if we had the incoming message have four parts and it waited for four it will if we send it you know 56 and then 10 this is still waiting for two more numbers and it’ll it’ll keep on waiting and so this has to be 2 this has to be 2 this has to be the same mailbox as the master program is sending – and this has to refer to a you byte array of – all right so sound good cool maybe possibly although I don’t know I’m asking you because you can’t talk to me all right so this reads the message in just continuous it just reads it will add a delay so that it doesn’t work harder than it has to like 10 milliseconds that’s that’s a decent refresh time and we’ll display let’s let’s display it to the screen so we can actually show that it’s there so NXT display that string we got a line number we’ll choose line 0 then we got content and what I’m gonna say is a message 0 equals and this is just text right here like I could put

anything I just a good I could just put but I want to make it coherent message 0 equals comma message 0 oh wait did I say message I’m an incoming message and message right there is just for short okay so what I wanted to do is I want to actually display most of 0 equals and then some kind of a number and so how I do that is I use modulus D modulus D is basically for the program’s way of saying hey whatever comes right after that but if this thing right here display it right there as a whole number and then since there are two parts of this match its message I mean that’s where it is and we’re sending 56 and 10 I’m going to display message 1 equals and if this is confusing how I’m using 0 there instead of a 1 there and like if you don’t know anything about a race google it and watch my previous tutorials because yeah you’ll learn some things all right so that’s that part and something coming message to man but yeah that looks so good so let’s all download master B do the master and that’s downloaded now I need to switch my USB connection so that I download the slave program to the slave brick because if you download the master and the slave to the same program then the NXT I don’t think it can connect with itself that would be cool though I’ll download that and it says hey it’s a different NXT is that okay I’m like yeah it’s okay oh it says it couldn’t find it brick and maybe that’s because the brick was it fell asleep sorry so yeah now the NXT is on and it’s like a it’s a different one and I’m like okay okay so the both programs have been downloaded so we have a master program that sends one thing at one time and we have a slave that will listen for an indefinite amount of time so I’m gonna run the slave first or at least I’m gonna start it first so oh I forgot something on here we’re displaying message zero equals blah blah blah on line zero and then we’re displaying message one equals blah blah blah on line zero that needs to change they needed to be displayed on separate lines and then we need a race display so that if the value changes then it actually works properly so well download that code and okay so I’ll run the program there there we go two lines I’ll turn this NXT on and right there we have incomplete diamond that’s because it hasn’t connected yet but our code will do that automatically run I think it needs a little bit longer to connect there we go now is connected whoops I think I did something wrong I will double-check everything we’ve got the master program it is connecting to slave on channel 2 and it’s gonna send outgoing message on mailbox 1 and on here it’s looking for incoming message incoming message to mailbox 1 it’s all good just to be safe I will put the master program in a loop so that it does its part indefinitely and we’ll just put not delay thinking of a different language here there we go put that in there and tab thing so it looks pretty and I’ll down the bat program to the master and we’ll see what we get and it’s different all right now back to here both of these programs are in the loops are in loop so they should run there we go just needed in an envelope and so as soon as this started running and it found its connection and is sending and sending and sending stuff this got us values so that right there is the basics the basic basic basics with excuse the pun of Bluetooth programming in robotc and we’ll change these numbers instead of 56 and 10 will send 32 and one and I’ll download that program yeah we can see here starts at zeroes as soon as that connects thirty-two and one and so we can basically do whatever we want with these numbers and just for kicks again if you want to send more than two values then you can make this number bigger

let’s say we wanted to send eight values now the thing is is not quite as simple as that remember this number right here has to be the same as this number right here which has to be the same as this number right here and this number right here those four numbers must be exactly the same because of that they’re not the same that bad stuff happens or nothing happens and so yeah now this is not the only thing if you hit f7 to compile the program there’s this big excellent comes right here and it says incorrect number of array of array initialization elements for outgoing message we’re just saying is hey what’s up you say this is going to be eight things long but here you only have two things and so we have to say you know give it more things so maybe sixteen seventy eight five four so that’s one two three four five six just need two more three and zero and we hit the seven and the air goes away it’s like yeah we’re all happy now all right so that’s cool and then on the slave let’s actually display all of those I’m gonna basically just copy and paste all of this in and then go zero one two three four five six seven and here we want to say two three four five six seven and then here one two three four five four five six seven all right and I guess I didn’t make a for loop but um previous lesson I talked about how to condense all of this into three lines of three or four lines of code instead of doing all these eight lines you can do it that way but I didn’t really think of it before I typed it all out one thing I want to mention before I talk about before and this is the master program wait when I’m sec this right here that is a bigger number than the slave and the reason why is like I did that on purpose because you don’t want to send fat you don’t want to send data faster than you’re reading it because if you if you send what if you send it faster than you’re processing it then it’s gonna amass you’re gonna have a whole bunch this buffer that’s full of readings that you need to look through and it basically means that your connection is going to accrue lag and so you want the master program to send it slower than the than the slave is reading it and so this could be you know 12 or 11 but all those make a 15 just I mean it’s not really that big of a deal anyway and so I’ll download the master program for the master it’s a good idea and then I will go to the slave program unplug the master plug in the slave and then we’ll download to the slave and it’s like yeah it’s a different one yeah it’s okay and so now the master program is going to send 32 116 78 5 4 3 and 0 let’s see if that’s let’s see if that’s right so we got slave program and they’re all zeros there and they change their values so yay I guess I come up I keep on coming up with one more thing to talk about let’s say you wanted to instead of just sending discrete values that were already written in the beginning of the program let’s say we wanted to make 32 you know the first one count up we could do something like outgoing message 0 + + + + + basically means add 1 to it outgoing message 0 that means the first part and yeah I know 0 isn’t for the first button come on and so I going message – 0 + + or I could say outgoing message 0 equals 5 or equals outgoing message 1 + 4 or whatever I want to do I’ll just keep it + + and this will mean that it’ll get bigger really fast in little truncate which means it’ll reset but for the purpose of this demonstration it’s alright so I’ll connect it to the master at 5 yeah it’s different we know and we’ll get here start them slaves and then I’ll start the master and as you can see the number on the top is getting really big and then it resets and that is Bluetooth communication for the NXT thank you for your patience with me not keeping schedule in the past I had finals and

then I had a job so I’m I’m actually employed right now and in school and yeah so yeah if you have any questions send them in the comments if you have any feedback or anything that you could tell me how to do better how I need to improve or our camp roof that’d be great and yeah next time guys