EvilZone

Programming and Scripting => Projects and Discussion => Topic started by: WirelessDesert on February 10, 2013, 02:16:16 PM

Title: Moving car, school project!
Post by: WirelessDesert on February 10, 2013, 02:16:16 PM
Hello EZ!


In my school, we will have a project where we should make a moving car.
The project instructions are like this; rough translation from swedish.
Code: [Select]
What to do.You shall make a draft and then build a small electric car.
MaterialDC-engines, flowersticks, cabbles, 4,5 V batteries, cardboard, wheels, metal wire, and other things your teacher decides.

 
What you will do.
1. How will you make the car to move, explain?
2. Figure how you will transfer current from the battery to the DC-engine.
3. How will the wheels be positioned?
4. How will the chassi look?
5. Draw the draft of your car and explain how it works, show your teacher.
6. Build your car.
7. Document what you have done and how you thought.

One word. BORING!.

So, I'm thinking about spicing it up a bit by adding an arduino, IR diodes and IR sensors.


All I will do will be documented here, wish me luck, and if someone got information about IR technology, please do tell me.


::Project start::

Day. 1

So first thing I've done was to get all my equipment ready;
This is not all I need, I think I will buy some real good IR diodes and other things needed.


So my first thing is to research about IR diodes.
Links:
    http://arduino.cc/forum/index.php/topic,12218.0.html (http://arduino.cc/forum/index.php/topic,12218.0.html)
    http://en.wikipedia.org/wiki/Infrared (http://en.wikipedia.org/wiki/Infrared)
    http://learn.adafruit.com/ir-sensor (http://learn.adafruit.com/ir-sensor)
    ::EDIT::

    http://www.youtube.com/watch?v=vjnOvdJrHMo (http://www.youtube.com/watch?v=vjnOvdJrHMo)  Link's to this.
    http://arduinoarts.com/2011/08/tutorial-arduino-controlled-by-apple-remote/ (http://arduinoarts.com/2011/08/tutorial-arduino-controlled-by-apple-remote/)

    http://www.youtube.com/watch?v=eCEg3PgDitI (http://www.youtube.com/watch?v=eCEg3PgDitI)



This should satisfy for now. I'll come back when I've finished reading :)



Day. 3


So today, I've thought about how to make the car go left/right and backwards, I'm thinking of making the analog input to the DC-engine lower on the side to turn to for going left/right.
To go backwards, I think reversing the polarity would make the engines go backward, however, I have no idea o how to do that.
Anyway, I will talk with a friend that has worked/is working with IR today, I think he can provide some tips to me.
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 10, 2013, 02:17:15 PM
This place is reserved, sorry if it counts as a double post, I'm so sorry if it does.
Title: Re: Moving car, school project!
Post by: Kulverstukas on February 10, 2013, 03:37:35 PM
Doesn't count as double post if you post to update the thread.

On topic: I like this. I would like to see more about it. Maybe eventually make a PDF with instructions? write Evilzone all over it :D
Title: Re: Moving car, school project!
Post by: jonneburger on February 10, 2013, 03:57:58 PM
i'd never cant make this. i would freak out in seconds. +1
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 12, 2013, 10:28:37 AM
Day. 3


So today, I've thought about how to make the car go left/right and backwards, I'm thinking of making the analog input to the DC-engine lower on the side to turn to for going left/right.
To go backwards, I think reversing the polarity would make the engines go backward, however, I have no idea o how to do that.
Anyway, I will talk with a friend that has worked/is working with IR today, I think he can provide some tips to me.

I will update the first post whenever I write about a new day.


::UPDATE::


Searched the internets and found this
    http://arduinotronics.blogspot.it/2012/11/arduino-ir-receiver-part-2.html (http://arduinotronics.blogspot.it/2012/11/arduino-ir-receiver-part-2.html)
    http://playground.arduino.cc/Code/InfraredReceivers (http://playground.arduino.cc/Code/InfraredReceivers)
The first link will be extremely useful, as it is almost what I want to do.
One thing that I've found, is that the usual place where I buy my components from does not have any LED IR or IR receivers. Which is sad, I've got one other place to get it from, but that could take days before I get it. (Even though I have like 6 weeks on me.)


I will make the draft today, I'll try to upload it to EZ when I'm done.
Title: Re: Moving car, school project!
Post by: Kulverstukas on February 12, 2013, 10:55:22 AM
While you're at it, make it controllable from a computer. Attach a router to it so it can be controlled from where ever there is internet! haha!
I'm just brainstorming... great work you are doing. I always wanted to make my own RC car too.
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 12, 2013, 01:19:29 PM
Found a way to reverse polarity without touching the wires,
    http://en.wikipedia.org/wiki/H_bridge (http://en.wikipedia.org/wiki/H_bridge)
    http://arduino.cc/forum/index.php?topic=122987.0 (http://arduino.cc/forum/index.php?topic=122987.0)


    (Edit again: http://www.hvlabs.com/hbridge.html (http://www.hvlabs.com/hbridge.html))


Now, I don't want to spend to much money on this, so I will see if I will buy one or not.

::EDIT::


Found even more :D , internet is great!
    http://itp.nyu.edu/physcomp/Labs/DCMotorControl (http://itp.nyu.edu/physcomp/Labs/DCMotorControl)
    http://arduino.cc/blog/2012/10/31/drive-a-dc-motor-with-arduino-due/ (http://arduino.cc/blog/2012/10/31/drive-a-dc-motor-with-arduino-due/)


That Arduino Due seems interesting, I may end up buying one.
My teacher said that the DC motors would be 3V
So using the Due would be great, as it only has 3.3V I/O pins. (http://arduino.cc/en/Main/ArduinoBoardDue (http://arduino.cc/en/Main/ArduinoBoardDue))

Title: Re: Moving car, school project!
Post by: techb on February 12, 2013, 07:03:07 PM
You don't need a Due, your Uno will work just fine. You will either need a voltage shifter
https://www.sparkfun.com/products/8745 (https://www.sparkfun.com/products/8745)

Or use diodes to drop the voltage.

For speed control, use PWM. Also a note on the motors, if they spin freely like down a hill or something they will generate voltage and current that could hurt your comoponents including your arduino. So you need some kind of protection or isolation. Also be sure to put some capacitors on the motor as well to help with noise.

As for the IR receivers, your a hacker right? Go salvage them from old electronics that have been thrown away or don't work anymore.

If you wanna make the thing really cool, forget the IR stuff and use DTMF.
http://www.electronics-diy.com/dtmf-decoder-using-mt8870.php (http://www.electronics-diy.com/dtmf-decoder-using-mt8870.php)
That way you can plug in a cell phone, call the cellphone from anywhere in the world, and control the car by pushing buttons.

Oh, and here is a site I frequent that might help with ideas and what-not
http://letsmakerobots.com/
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 12, 2013, 08:31:35 PM
You don't need a Due, your Uno will work just fine. You will either need a voltage shifter
https://www.sparkfun.com/products/8745 (https://www.sparkfun.com/products/8745)

Or use diodes to drop the voltage.

For speed control, use PWM. Also a note on the motors, if they spin freely like down a hill or something they will generate voltage and current that could hurt your comoponents including your arduino. So you need some kind of protection or isolation. Also be sure to put some capacitors on the motor as well to help with noise.

As for the IR receivers, your a hacker right? Go salvage them from old electronics that have been thrown away or don't work anymore.

If you wanna make the thing really cool, forget the IR stuff and use DTMF.
http://www.electronics-diy.com/dtmf-decoder-using-mt8870.php (http://www.electronics-diy.com/dtmf-decoder-using-mt8870.php)
That way you can plug in a cell phone, call the cellphone from anywhere in the world, and control the car by pushing buttons.

Oh, and here is a site I frequent that might help with ideas and what-not
http://letsmakerobots.com/

Thank you techb, I did say that I had some remotes at home that I can use, I will take them apart when need to. However, I tried to find a ir receiver, but I didn't find one.

For the due, I would also like to have it because it seems awesome.

And wow, dtmf seems awesome!

Title: Re: Moving car, school project!
Post by: techb on February 12, 2013, 09:14:31 PM
Arduino's are cool and all, and great for beginners, but you really should learn to code the AVR itself and learn not to rely on pre-built dev boards.

Just my $0.02, and I am willing to help with your project if you need any questions answered.
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 13, 2013, 12:35:28 PM
So, I'm thinking about how I will put this all together.


I will probably use DTMF, as it would be so awesome. Else, IR.
I want to use H Bridges to make the car go backwards.
The car will have lamps made out of LED's.
Turning left/right will be done by changing the output to the DC-engines.
Speed will be controlled with Pulse-width modulation.
I think the engines will be 3V ones, I'm not quite sure.


Well, that concludes it.
I will start to make functions for going forward, left and right today, maybe the speed too. I'm open for suggestion, talk to me at #Evilzone or here. :)
Title: Re: Moving car, school project!
Post by: hanorotu on February 13, 2013, 04:58:46 PM
I will be following this, thanks for the read!
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 14, 2013, 09:19:32 AM
Have a question, how do I do a feedback for my motors?


and why do I need them?




Found a H-Bridge I think I will use.
IR2184PBF DIP-8.


and the File for it. http://upload.evilzone.org/download.php?id=5594887&type=rar (http://upload.evilzone.org/download.php?id=5594887&type=rar)



Title: Re: Moving car, school project!
Post by: techb on February 14, 2013, 11:21:37 AM
The only way to really get feedback from a motor is to use rotary encoders. Something like servos get feedback from potentiometers but have a min and max turn degrees.

So you will need rotary encoders to get feedback.

As for the hbridge, why not just use some transistors since the voltage is so low? Could even FETs if your worried about current draw.
Title: Re: Moving car, school project!
Post by: WirelessDesert on February 14, 2013, 11:25:19 AM
The only way to really get feedback from a motor is to use rotary encoders. Something like servos get feedback from potentiometers but have a min and max turn degrees.

So you will need rotary encoders to get feedback.

As for the hbridge, why not just use some transistors since the voltage is so low? Could even FETs if your worried about current draw.


Yeah, I was thinking about doing that to, MOSFET NPN transistors should work.
Anyway, I've done some code today, whatdaya think guys?
http://liveworkspace.org/code/1vlDG1$3 (http://liveworkspace.org/code/1vlDG1$3)
(Changed it after founding some errors and stupid coding on my side)
Title: Re: Moving car, school project!
Post by: WirelessDesert on March 03, 2013, 12:57:49 PM

Day 22.So, I found the "perfect" battery for my car. The V4034PX varta 6V, 100 mAh.

(http://i.imgur.com/PHeZ2jW.jpg)
The motor takes 2,4V to 6V and is on 250 mA.
The Transistor in the picture is just random, idk what I should use. (Help is appreciated, the Transistor is Tesla BUF74).
Found the battery while looking around for some old computers to make into a server for me and my family, found only one, but I think there are more where I found it.

::EDIT:: wow, totally failed with that picture, sorry if you saw it XD.
Title: Re: Moving car, school project!
Post by: WirelessDesert on March 19, 2013, 10:30:58 PM
Day WOOHOO! (#idk)


So yesterday, I bought the arduino motor shield just because I wanted to buy it. (Sorry techb :P )


Made some coding while in school, and I've come up with this.
http://pastebin.com/CTxQD9Hm (http://pastebin.com/CTxQD9Hm)
Much of it is commented, but feel free to ask about anything you want.


Anyway, here is a picture of my current setup.




(http://i.imgur.com/mpKSAEj.jpg)


Clean desktop FTW!




Thinking about using one TSOP38238 38KHz IR receivers to control it, I'll just have to figure out the protocol of a matching remote control.
Oh, And I'm wondering if I need to use diodes between the motors two pins so I don't get the drawback current thingy. Think I should do it.
Title: Re: Moving car, school project!
Post by: Stackprotector on March 19, 2013, 10:34:07 PM
Day WOOHOO! (#idk)


So yesterday, I bought the arduino motor shield just because I wanted to buy it. (Sorry techb :P)


Made some coding while in school, and I've come up with this.
http://pastebin.com/CTxQD9Hm (http://pastebin.com/CTxQD9Hm)
Much of it is commented, but feel free to ask about anything you want.


Anyway, here is a picture of my current setup.




(http://i.imgur.com/mpKSAEj.jpg)


Clean desktop FTW!




Thinking about using one TSOP38238 38KHz IR receivers to control it, I'll just have to figure out the protocol of a matching remote control.
Nice one :D. Now you can cool the car with a computer fan.
Title: Re: Moving car, school project!
Post by: WirelessDesert on April 01, 2013, 06:43:19 PM
Day IDK again(sorry).


So, today I really tried to make my car wirelessly steerable.
Using the library IrRemote (https://github.com/shirriff/Arduino-IRremote (https://github.com/shirriff/Arduino-IRremote)) for arduino.
Been trying to figure out this old remote I found at home.




It works surprisingly well, the only problem is that it returns a 12th bit of value 1(true) every second press.
First Press on button 1:

Code: [Select]
1Second Press on Button 1:
Code: [Select]
100000000001It even keeps the toggle for a new button:
First Press on Button 2:
Code: [Select]
10Second Press on Button 4:

Code: [Select]
100000000100

Should be easily filtered out by removing every 12th bit with a bitwise subtraction.
However, the wierd thing happens when I hold down a button.
Sometimes, it returns things like this. (Held down The button 3 for 10 seconds):
Code: [Select]

11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11100100010000000000101100010100
11
11100100010000000000101100010100
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11100100010000000000101100010100
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
Have no idea what it is, but I'll either filter out it by doing an OR on the if statements, or to just ignore every input over 13 bits. However, I think it is a "placeholder" or "marker" for something, to indicate a cycle maybe.
And oh, here is my code, no comments on it because it is all on the website of the library I'm using. http://pastebin.com/iR3Bau6G (http://pastebin.com/iR3Bau6G)
::UPDATE::
I think the error stated above is caused by bounces in the remote. Or that Serial.println() isn't fast enough. Have no idea, maybe it's even a problem with the IRremote library. I should maybe try another remote and see. (Not a sony one though, heard that it sends the info three times so that would just be a mess.)
Title: Re: Moving car, school project!
Post by: WirelessDesert on April 10, 2013, 04:53:40 PM
Day (#fill in the day here.)


So, today, I've fixed it all, the only thing missing is the car chassi.
Other then that, it's finished. Some pictures and maybe a video will come soon.
Final Code:
Code: (cpp) [Select]
#include <IRremote.h>




struct WheelStruct {
  int mspeed; // Max is 255
  int mForward; // Is forward or not.
  const int speedPinD; // Digital, PWM
  const int directionPinD; // Digital
  const int brakePinD; // Digital (I've got no clue what this is.)
  const int currentSensingPinA; // Analog


};


const int ZERO = 0;
const int MAX = 255;
const int HALF = 123+40;
const int QUARTER = 95;
boolean CLOCKWISE = true;


WheelStruct motorA = {
  ZERO, true, 3, 12, 9, 0}; // Motor A is left
WheelStruct motorB = {
  ZERO, true, 11, 13, 8, 1}; // Motor B is right.


const int ledLampAPin = 4;
const int ledLampBPin = 5;
boolean stopped = false;


boolean toggle = false;
long previousMillis = 0;
long interval = 1000;


int recvPin = 2;
IRrecv irrecv(recvPin);
decode_results results;




void setup(){


  Serial.begin(9600);


  pinMode(motorA.speedPinD, OUTPUT);
  pinMode(motorB.speedPinD, OUTPUT);


  pinMode(motorA.directionPinD, OUTPUT);
  pinMode(motorB.directionPinD, OUTPUT);


  pinMode(motorA.brakePinD, OUTPUT);
  pinMode(motorB.brakePinD, OUTPUT);


  pinMode(motorA.currentSensingPinA, INPUT);
  pinMode(motorB.currentSensingPinA, INPUT);


  irrecv.enableIRIn();


}


void loop(){


  if(gotKey()){
    while(getKey() >= 3000){
      softReset();
    }
    switch(getKey()){
    case 5:
      motorTurn(ZERO);
      Serial.println("Stopped");
      break;
    case 2:
      motorTurn(MAX);
      Serial.println("Forwards.");
      break;
    case 4:
      motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
      Serial.println("Left.");
      break;
    case 6:
      motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
      Serial.println("Right.");
      break;
    case 8:
      motorTurn(-MAX);
      Serial.println("Backwards.");
      break;
    case 1:
      motorTurn(HALF, MAX);
      Serial.println("Left, forward.");
      break;
    case 3:
      motorTurn(MAX, HALF-40);
      Serial.println("Right, forward.");
      break;
    case 7:
      motorTurn(-HALF, -MAX);
      Serial.println("Left, backwards.");
      break;
    case 9:
      motorTurn(-MAX, -HALF);
      Serial.println("Right, backwards.");
      break;
    case 12:
      if(stopped)
        unlock();
      else{
        brake();
        stopped = true;
      }
      delay(100);
    default:
      break;
    }
    irrecv.resume();
  }
}


/*                                   FUNCTIONS STARTS HERE                            */


/*                                                                                    *
 *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
 *                                                                                    */


void switchWheelDirection(int mDirPin){
  if(digitalRead(mDirPin) == HIGH)
    digitalWrite(mDirPin, LOW);
  else
    digitalWrite(mDirPin, HIGH);
}




/*                                                                                    *
 *     writeMotor - replaces the analogWrite() function, positive values = forward    *
 *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
 *                                                                                    */
void motorWrite(char motor, int mspeed){
  if (mspeed >= 0 && mspeed <= 255){


    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, HIGH);
      analogWrite(motorA.speedPinD, mspeed);
      motorA.mForward = true;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, HIGH);
      analogWrite(motorB.speedPinD, mspeed);
      motorB.mForward = true;
    }
  }
  else if(mspeed >= -255 && mspeed <= 0){
    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, LOW);
      analogWrite(motorA.speedPinD, abs(mspeed));
      motorA.mForward = false;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, LOW);
      analogWrite(motorB.speedPinD, abs(mspeed));
      motorB.mForward = false;
    }
  }


}




/*                                                                                    *
 *                motorTurn - change the speed of each wheel idividually.             *
 *                                                                                    */
void motorTurn(int lSpeed, int rSpeed){
  motorWrite('A', lSpeed);
  motorWrite('B', rSpeed);
}
void motorTurn(int mspeed){
  motorWrite('A', mspeed);
  motorWrite('B', mspeed);
}
/*                                                                                    *
 *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
 *            use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE               *
 *                                                                                    */
void spinAround(boolean clockwise){
  if(clockwise == true){
    // Makes the other wheel go forward, the other one backward.


  }
}
/*                                                                                    *
 *                                 brake - stops all motors                           *
 *                                                                                    */
void brake(){
  digitalWrite(motorA.brakePinD, HIGH);
  digitalWrite(motorB.brakePinD, HIGH);
}


/*                                                                                    *
 *                       unlock - reverses the effects of brake                       *
 *                                                                                    */
void unlock(){
  digitalWrite(motorA.brakePinD, LOW);
  digitalWrite(motorB.brakePinD, LOW);
}


/*                                                                                    *
 *         gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results)       *
 *                                                                                    */
boolean gotKey(){
  return irrecv.decode(&results);
}


/*                                                                                    *
 *      getKey - "lazydog" for decode_results results.value & 2047                    *
 *                                                                                    */
int getKey(){
  return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
}
/*                                                                                    *
 *          softReset - macro for soft reseting the arduino, doesn't wipe             *
 *                                                                                    */
void softReset(){
  asm volatile ("  jmp 0");
}
/*                                   FUNCTIONS ENDS HERE                              */
























http://pastebin.com/Cg8LBrRy (http://pastebin.com/Cg8LBrRy)
Title: Re: Moving car, school project!
Post by: Stackprotector on April 10, 2013, 05:02:14 PM
Day (#fill in the day here.)


So, today, I've fixed it all, the only thing missing is the car chassi.
Other then that, it's finished. Some pictures and maybe a video will come soon.
Final Code:
Code: (cpp) [Select]
#include <IRremote.h>




struct WheelStruct {
  int mspeed; // Max is 255
  int mForward; // Is forward or not.
  const int speedPinD; // Digital, PWM
  const int directionPinD; // Digital
  const int brakePinD; // Digital (I've got no clue what this is.)
  const int currentSensingPinA; // Analog


};


const int ZERO = 0;
const int MAX = 255;
const int HALF = 123+40;
const int QUARTER = 95;
boolean CLOCKWISE = true;


WheelStruct motorA = {
  ZERO, true, 3, 12, 9, 0}; // Motor A is left
WheelStruct motorB = {
  ZERO, true, 11, 13, 8, 1}; // Motor B is right.


const int ledLampAPin = 4;
const int ledLampBPin = 5;
boolean stopped = false;


boolean toggle = false;
long previousMillis = 0;
long interval = 1000;


int recvPin = 2;
IRrecv irrecv(recvPin);
decode_results results;




void setup(){


  Serial.begin(9600);


  pinMode(motorA.speedPinD, OUTPUT);
  pinMode(motorB.speedPinD, OUTPUT);


  pinMode(motorA.directionPinD, OUTPUT);
  pinMode(motorB.directionPinD, OUTPUT);


  pinMode(motorA.brakePinD, OUTPUT);
  pinMode(motorB.brakePinD, OUTPUT);


  pinMode(motorA.currentSensingPinA, INPUT);
  pinMode(motorB.currentSensingPinA, INPUT);


  irrecv.enableIRIn();


}


void loop(){


  if(gotKey()){
    while(getKey() >= 3000){
      softReset();
    }
    switch(getKey()){
    case 5:
      motorTurn(ZERO);
      Serial.println("Stopped");
      break;
    case 2:
      motorTurn(MAX);
      Serial.println("Forwards.");
      break;
    case 4:
      motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
      Serial.println("Left.");
      break;
    case 6:
      motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
      Serial.println("Right.");
      break;
    case 8:
      motorTurn(-MAX);
      Serial.println("Backwards.");
      break;
    case 1:
      motorTurn(HALF, MAX);
      Serial.println("Left, forward.");
      break;
    case 3:
      motorTurn(MAX, HALF-40);
      Serial.println("Right, forward.");
      break;
    case 7:
      motorTurn(-HALF, -MAX);
      Serial.println("Left, backwards.");
      break;
    case 9:
      motorTurn(-MAX, -HALF);
      Serial.println("Right, backwards.");
      break;
    case 12:
      if(stopped)
        unlock();
      else{
        brake();
        stopped = true;
      }
      delay(100);
    default:
      break;
    }
    irrecv.resume();
  }
}


/*                                   FUNCTIONS STARTS HERE                            */


/*                                                                                    *
 *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
 *                                                                                    */


void switchWheelDirection(int mDirPin){
  if(digitalRead(mDirPin) == HIGH)
    digitalWrite(mDirPin, LOW);
  else
    digitalWrite(mDirPin, HIGH);
}




/*                                                                                    *
 *     writeMotor - replaces the analogWrite() function, positive values = forward    *
 *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
 *                                                                                    */
void motorWrite(char motor, int mspeed){
  if (mspeed >= 0 && mspeed <= 255){


    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, HIGH);
      analogWrite(motorA.speedPinD, mspeed);
      motorA.mForward = true;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, HIGH);
      analogWrite(motorB.speedPinD, mspeed);
      motorB.mForward = true;
    }
  }
  else if(mspeed >= -255 && mspeed <= 0){
    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, LOW);
      analogWrite(motorA.speedPinD, abs(mspeed));
      motorA.mForward = false;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, LOW);
      analogWrite(motorB.speedPinD, abs(mspeed));
      motorB.mForward = false;
    }
  }


}




/*                                                                                    *
 *                motorTurn - change the speed of each wheel idividually.             *
 *                                                                                    */
void motorTurn(int lSpeed, int rSpeed){
  motorWrite('A', lSpeed);
  motorWrite('B', rSpeed);
}
void motorTurn(int mspeed){
  motorWrite('A', mspeed);
  motorWrite('B', mspeed);
}
/*                                                                                    *
 *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
 *            use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE               *
 *                                                                                    */
void spinAround(boolean clockwise){
  if(clockwise == true){
    // Makes the other wheel go forward, the other one backward.


  }
}
/*                                                                                    *
 *                                 brake - stops all motors                           *
 *                                                                                    */
void brake(){
  digitalWrite(motorA.brakePinD, HIGH);
  digitalWrite(motorB.brakePinD, HIGH);
}


/*                                                                                    *
 *                       unlock - reverses the effects of brake                       *
 *                                                                                    */
void unlock(){
  digitalWrite(motorA.brakePinD, LOW);
  digitalWrite(motorB.brakePinD, LOW);
}


/*                                                                                    *
 *         gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results)       *
 *                                                                                    */
boolean gotKey(){
  return irrecv.decode(&results);
}


/*                                                                                    *
 *      getKey - "lazydog" for decode_results results.value & 2047                    *
 *                                                                                    */
int getKey(){
  return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
}
/*                                                                                    *
 *          softReset - macro for soft reseting the arduino, doesn't wipe             *
 *                                                                                    */
void softReset(){
  asm volatile ("  jmp 0");
}
/*                                   FUNCTIONS ENDS HERE                              */
























http://pastebin.com/Cg8LBrRy (http://pastebin.com/Cg8LBrRy)
Nice :)