Setting up OTG communication with MIT app inventor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to tape top robotics i'm justin today i'm going to show you how to make an app an mit app inventor that lets you have your phone communicate with an arduino using an otg cable and serial communication okay let's get started [Music] so here we are in mit app inventor we got a new project all set up and ready to go okay so how this app we're going to make today works is is going to be a simple calculator so we're gonna take two user input numbers we're gonna send those two numbers to an arduino that arduino is gonna add those two numbers and send the sum back to the phone and we're gonna display it so to start making our app we're gonna do is set up all the connection stuff right away and get just get that done over with to make the phone connect within arduino we're going to need two buttons to start off with we're going to label the first button connect and the second button to disconnect and we're going to rename the button itself and the text displayed on the button okay and we're also going to set up a check box and this check box is always going to do is show that if we are connected it's going to show a check mark if we're not connected it's just not going to show anything so we're just going to rename that to connected and another thing we're going to need is we're going to go down here into census we want to add a clock we want to set the time interval to 500 and we're going to go back down here into connectivity and right here where it says serial which i'm going to drag that over and if we look at the properties for the cereal you want to see it says 9600 as a bog weight now this can be anything all you have to make sure is that the bar grade set on the phone is the same set on the arduino so i'm just going to keep it 9600 as a default and the buffer size is good enough right now for my uses at least so i'm not going to change any of that okay after that we can go into blocks so the blocks for just connecting are quite simple and straightforward okay so we're going to do is when the connect button is pressed we want to initialize serial then we'll do an if call open serial and then if that's open correctly and successfully we're going to set the check box to true meaning it's been checked so then we're going to do the disconnect and what we're going to do is when disconnect is clicked we're going to do a evaluate but ignore result then call close serial so all that does it close the serial then we'll go into our clock and when clock 1 dot timer we're going to do an if statement we're going to add an else to it and what we're going to do is do some logic we're going to do if serial one is initialized and open we're going to set check box 1 to be checked else we're going to uncheck the box so when we run the program if it ever disconnects the checkbox will be unchecked then we can go back into designer and finish the rest of the program so now we have all the connection stuff finished we can get into the meat of the program now we're going to take two user inputs add them together and display the output so what we're going to do is first we're going to get two text boxes and these are going to be the user inputs so i'm going to name one of them in one as an input one one of them in two and now i'm going to take in a label put it in between the two inputs and one right after the input and these labels are just going to be the plus sign and the equal sign and then we're going to get the input back from the arduino so we're going to get one more label put it on bottom and we're going to call this sum number and right now this can just have a zero in it and then one last thing we're going to put in one button on the very bottom label that as calculate and for text we're going to write in calculate we go now for the two inputs we're going to want just number inputs so what we're going to do is we're going to click this little box that says numbers only and we're going to set that for both inputs and that is all we need for our program to function correctly and now we can do the blocks for the addition so here we are back into blocks i'm just going to zoom out a little bit and then we can start doing the block coding so first thing we're going to do is when the calculate button is pressed we're going to send the two numbers that we decided through serial to the arduino where it can be added so we're going to do an if statement and this if statement is going to check that the signal is open and that the two inputs have numbers in them so what we're going to do is if in text 1 does not equal a blank meaning that there is an input in it and in text two does not equal blank we're also going to check if c mule is still open so then we also need another end and we're going to check if serial is open so if that statement is true we're gonna send the two input numbers now how we do that is we're gonna do serial one print serial and in this data location right here we're gonna make a list and in that list we're gonna have text 1 text and text 2 text so that covers all the sending data now we have to do the receiving so i'm just going to move this code a little bit up and move the clock down because we still need to edit the clock so we're going to make a new variable and this variable is going to be called in number and we're going to have the in number as blank for now so now we're going to edit this timer block right here and what we're going to do is every time the timer activates we want to set in number to be whatever values are in serial so whatever values the arduino sent back to the phone and all we have to do that is in global number and all we need for that is call civic 1 read serial okay then after that we're going to need one more if statement if in number does not equal blank we're going to set the sum number to be equal that of in number and that's all the code we want to need to run our program so let's head to the arduino ide and start programming the arduino so here i have the arduino ide open and a new project setup so right above the void setup we're going to make an array this array is going to hold all our input numbers and this array is going to be a float and in this array we're going to have three numbers and we're just going to set them all to zero for now next in void setup we're going to set up serial so serial dot begin and now here we're going to put 9600 or whatever you set the serial bog rate on your phone they do have to match for this to work correctly in void loop i want to check if there's any incoming serial data so if serial not available is greater than zero do something now this greater than zero meaning that there is data coming in if it say if it's equal to 0 meaning there's no data coming in so that's why we have the greater than 0 here then after that we're going to delay by 400 milliseconds and then we're going to set in numbers zero meaning the first number in the array i'm going to set that to serial dot pause float and all this does it takes the first float number in serial and puts it into the array and then we're going to do the same thing a second time so it's going to copy this paste it again and instead of number zero we want to have number one so the first two numbers in the array are the two incoming numbers then after this we're going to have to get rid of all the other information that's interior that we don't need so what we can do is do a while statement and we do a while serial dot available is greater than zero we're going to do a serial dot read and that will just continue until there's no more data in the serial and after that we're going to do we want to set in numbers and then we're going to set the third number in there and that third number is going to equal in number zero plus in numbers one and then we're just going to do a serial nut print in numbers 2 and all that does it just gets the sum and sends it back to the phone okay so here i have the phone it's all hooked up to an arduino with the otg cable i got the app installed and running it's all good for now okay so all we have to do is press connect you're gonna have to give it permission let's connect one more time okay so after the phone is connected we can just put in two numbers just do like 58 and uh 23.9 and after that if we press calculate it just send the numbers and we get the sum right there and you can do this with any numbers up to two decimal points so if we do 56.95 and we just change it to like something anything like 1 102 point six eight press calculate again we got the answer right there okay so after that if we press disconnect it disconnects the phone and the arduino okay guys so that's the end of this video if you liked it give it a thumbs up consider subscribing i'll see you next time bye [Music] [Music] so [Music] [Music] [Music] you
Info
Channel: Tabletop Robotics
Views: 24,800
Rating: undefined out of 5
Keywords: 3d printing for beginners, 3d printing for nobs, 3d printing 101, getting into 3d printing, how to 3d print, 3d printing how to, 3D, printing, mr maker, master maker, how to make, latest maker's news, teaching, new maker, young maker, makers news, what makes a maker, make, TTR, robotics, arduino, tabletop robotics, how to, projects, Code, 3D printed, learning, STEAM, STEM, electronic, computer, simple, easy, learn, maker, makers, maker faire, maker's muse, future maker, OTG, MIT app inventor
Id: 5iOG3Ko9OCY
Channel Id: undefined
Length: 11min 53sec (713 seconds)
Published: Sat Nov 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.