How to code an Arduino to run two DC motors: Part 1

How to code an Arduino to run two DC motors: Part 1

This is a continuation of the four part series on wiring the Arduino:

Part 1 is coding.

You will need to have downloaded the Arduino IDE:

all right so we're up for the coding part missus vigil is thrilled to pieces hi guys i quite like clothing what's the program that we use okay so we're going to use obviously the ones supplied for the arduino yes and you've hopefully already downloaded it from self-service now you can see it it should you should have saved it inside your little tool bar thingy and so i'm just going to open it up when you open it up you should get a fresh new what they call sketch okay so yeah i just noticed that we've got so we've got void set up we've got round brackets and then we've got curly brackets in there and then we've got void loop and the voids both in the blue and the loops in the mustard so they're actually really important types of coding aren't they yeah all your new files that you create your new sketches will all have a set up and a loop and you need to have them they can be blank like for example set up is obviously all the key things you need to set up your code like for example we're going to put in our left and right motors that they're inputs and outputs whatever we're going to put them in set up loop is just things that you want to happen continuously as it says to run repeatedly if you don't want to run something repeatedly you can keep that blank but you have to have it in there because it's part of the system so if it doesn't have that there it will send an error to us yeah absolutely so the first thing that i think we should do is definitely outline which pins we've identified as being a right motor in which pins we've identified as being a left motor so we should do that in setup so that actually occurs outside of set up if you want to name pins a name that you want to give it yep you can give up in any name you want so what we will do is we're going to start with our right motor so our right motors both you and I chose pins 6 and 7 yes for our right motors so what we're going to do is we're going to type in int int stands for integer so we're going to tell the computer or the program that integer we're going to call motor right I'm going to put a capital A for right motor right to go forward we it's equal to PN at six and then every line of code which is critical for any code program but you know this one in particular every line of code has to have a semicolon at the end so it not really stinks when you forget that sense all these errors error error error you need to let the computer or the program know that your line is finished and that semicolon tells whether it's finished so what I've done so if I we've said we're going to set motor right forward as an integer yeah and it's going to be integer six nose pin 6 on our Arduino meadow copy that and do that for all those and just change that means because our if we want to then turn it backwards yeah so I'm going to change that to reverse because motors go forward or reverse yep that's going to be my pin 7 but you asked me earlier you said well how do I know which ones which well actually I don't yeah I know but we're going to fix that later on out of the testing that's right exactly so then they swim I'm going to copy both of those and I'm going to paste them down here and just change it to left motor and so which pens did we use for the left motor we use 8 and 9 so again we don't know which is which but you know you're going to go angular and like that but now for the best and the reason we do that is so that when we do the testing later it's really easy for us to change the numbers rather than having to go through our whole code and keep changing where number 2 so now anywhere that I write motor are forward it knows that's pin 6 brilliant ok now we need to set these guys up so in the setup you can notice that this bits in grey everything else we've written has a color but gray is comments so what we're going to now set up is actually telling the program that these digital pins are outputs so this part you use pin mode right so mode is the terminology that we're going to use and if it's correct terminology that Arduino code understands then it goes to orange because it's something in its library that it recognizes right plus ten mohs you must use a capital m m4 mode and notice as I finish writing it it goes arena and I'm on the right track it but every command in orange needs the Open bracket yeah so you can actually tell it what to do and then you need to close it so what we're going to do is we're going to say which pin is in pinmode so pin modus first one is pin six but we've called pin six motor right forward so what I'm going to do that I'm actually just going to copy that to avoid any mistakes because in coding the second you make a mistake it can't read what you've done so you have to make sure it's exactly what you've written so the first pin I'm setting up is murder right forward I put a comma and I need to say that it's an output and notice as soon as I've finished my output it goes blue I close my brackets and I have to finish with a nickel it's now telling me that pin six will send a signal that's what output is indicating it's going to send the signal to the motor yep we could use input in which it would receive a signal so for example get a sensor connected you make that pin and input pin so you're going to receive information from the sensor but in this case or sending information out to the motor so we're going to output so I'm just going to repeat that for all of them I've got four pins so I need four of those I will change them in a moment I just want to get them out and there's only two isn't there there's input and output so I'm either identifying them as input and output and I'm in this case all of our pins are output pins correct so I'm changing that back to what we had through that okay and they're all the names that are above so they're identifying pin six seven eight and nine are all output pins exactly right okay so that is our setup so the next thing we can do now you can either put the next thing in setup or in loop now we're going to put it in lieu just so we're going to feel for what move does yet Makassar inside loop so again we've got to make sure we're inside the bracket we're now going to tell these pins to do something and the command you want to use for telling a pin to do something is digital right so it's not digital riders in right side digital right as in writing so we're going to write to the pin yep so we've typed digital and the second word usually always has a capital letter so digital right it went Orange so I know I'm on the right track and you remember what has to happen with orange commands yeah so I always open with a kill a round round bracket very good practice and say in here I guess what we're going to do is again use all those motor so the pin terms and we're going to identify that as we're going to deal with that I'll let me low or high well let's have a look so I'm going to first write to the pin six which is my motor right follow I'm writing something to it and then I so first you need to tell who are you writing to that's why I put that first you put a comma and then you tell what do you want it to do and so we want to turn it on yep now the language as you mentioned for on is actually high yeah so high turns it on and low turns it off so we're going to type high and we'd be good if they just called it on and off here would say wouldn't it yeah that would be wave yeah they like to make life difficult phone and like so many things in your home seriously and again I'm going to close the bracket and put the semi back on so important and we want to copy and paste so well what do we actually want to do we want to turn the right motor forward and I suppose in where we want to make measure other forward yeah we do but the other thing is that I guess we've got to make sure that the other one is off at the same time because otherwise it's going to get confused isn't it so if I went and turned everything on then it's going to be trying to spin forwards and backwards at the same time and that's not going to work yeah exactly so I've got my pin 6 on yeah but my pin 7 which is the motor going in Reverse I don't want it going in Reverse so I'm going to turn that off technically you don't need that it won't not on but it's good for us to understand that that's exactly what's happening so let's just do the same thing for our left one yeah I'm just going to copy that again paste that in and turn the left one on and the reverse ones off and at the moment they could be going in opposite direction but then we're going to check it and change the either change the pins here or we can change the pins in the code exactly right

5 thoughts on “How to code an Arduino to run two DC motors: Part 1

Leave a Reply