Advertisement
Guest User

Arduino RC car

a guest
Apr 10th, 2013
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include <IRremote.h>
  2.  
  3.  
  4. struct WheelStruct {
  5.   int mspeed; // Max is 255
  6.   int mForward; // Is forward or not.
  7.   const int speedPinD; // Digital, PWM
  8.   const int directionPinD; // Digital
  9.   const int brakePinD; // Digital (I've got no clue what this is.)
  10.   const int currentSensingPinA; // Analog
  11.  
  12. };
  13.  
  14. const int ZERO = 0;
  15. const int MAX = 255;
  16. const int HALF = 123+40;
  17. const int QUARTER = 95;
  18. boolean CLOCKWISE = true;
  19.  
  20. WheelStruct motorA = {
  21.   ZERO, true, 3, 12, 9, 0}; // Motor A is left
  22. WheelStruct motorB = {
  23.   ZERO, true, 11, 13, 8, 1}; // Motor B is right.
  24.  
  25. const int ledLampAPin = 4;
  26. const int ledLampBPin = 5;
  27. boolean stopped = false;
  28.  
  29. boolean toggle = false;
  30. long previousMillis = 0;
  31. long interval = 1000;
  32.  
  33. int recvPin = 2;
  34. IRrecv irrecv(recvPin);
  35. decode_results results;
  36.  
  37.  
  38. void setup(){
  39.  
  40.   Serial.begin(9600);
  41.  
  42.   pinMode(motorA.speedPinD, OUTPUT);
  43.   pinMode(motorB.speedPinD, OUTPUT);
  44.  
  45.   pinMode(motorA.directionPinD, OUTPUT);
  46.   pinMode(motorB.directionPinD, OUTPUT);
  47.  
  48.   pinMode(motorA.brakePinD, OUTPUT);
  49.   pinMode(motorB.brakePinD, OUTPUT);
  50.  
  51.   pinMode(motorA.currentSensingPinA, INPUT);
  52.   pinMode(motorB.currentSensingPinA, INPUT);
  53.  
  54.   irrecv.enableIRIn();
  55.  
  56. }
  57.  
  58. void loop(){
  59.  
  60.   if(gotKey()){
  61.     while(getKey() >= 3000){
  62.       softReset();
  63.     }
  64.     switch(getKey()){
  65.     case 5:
  66.       motorTurn(ZERO);
  67.       Serial.println("Stopped");
  68.       break;
  69.     case 2:
  70.       motorTurn(MAX);
  71.       Serial.println("Forwards.");
  72.       break;
  73.     case 4:
  74.       motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
  75.       Serial.println("Left.");
  76.       break;
  77.     case 6:
  78.       motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
  79.       Serial.println("Right.");
  80.       break;
  81.     case 8:
  82.       motorTurn(-MAX);
  83.       Serial.println("Backwards.");
  84.       break;
  85.     case 1:
  86.       motorTurn(HALF, MAX);
  87.       Serial.println("Left, forward.");
  88.       break;
  89.     case 3:
  90.       motorTurn(MAX, HALF-40);
  91.       Serial.println("Right, forward.");
  92.       break;
  93.     case 7:
  94.       motorTurn(-HALF, -MAX);
  95.       Serial.println("Left, backwards.");
  96.       break;
  97.     case 9:
  98.       motorTurn(-MAX, -HALF);
  99.       Serial.println("Right, backwards.");
  100.       break;
  101.     case 12:
  102.       if(stopped)
  103.         unlock();
  104.       else{
  105.         brake();
  106.         stopped = true;
  107.       }
  108.       delay(100);
  109.     default:
  110.       break;
  111.     }
  112.     irrecv.resume();
  113.   }
  114. }
  115.  
  116. /*                                   FUNCTIONS STARTS HERE                            */
  117.  
  118. /*                                                                                    *
  119.  *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
  120.  *                                                                                    */
  121.  
  122. void switchWheelDirection(int mDirPin){
  123.   if(digitalRead(mDirPin) == HIGH)
  124.     digitalWrite(mDirPin, LOW);
  125.   else
  126.     digitalWrite(mDirPin, HIGH);
  127. }
  128.  
  129.  
  130. /*                                                                                    *
  131.  *     writeMotor - replaces the analogWrite() function, positive values = forward    *
  132.  *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
  133.  *                                                                                    */
  134. void motorWrite(char motor, int mspeed){
  135.   if (mspeed >= 0 && mspeed <= 255){
  136.  
  137.     if(motor == 'A'){
  138.       digitalWrite(motorA.directionPinD, HIGH);
  139.       analogWrite(motorA.speedPinD, mspeed);
  140.       motorA.mForward = true;
  141.     }
  142.     if(motor == 'B'){
  143.       digitalWrite(motorB.directionPinD, HIGH);
  144.       analogWrite(motorB.speedPinD, mspeed);
  145.       motorB.mForward = true;
  146.     }
  147.   }
  148.   else if(mspeed >= -255 && mspeed <= 0){
  149.     if(motor == 'A'){
  150.       digitalWrite(motorA.directionPinD, LOW);
  151.       analogWrite(motorA.speedPinD, abs(mspeed));
  152.       motorA.mForward = false;
  153.     }
  154.     if(motor == 'B'){
  155.       digitalWrite(motorB.directionPinD, LOW);
  156.       analogWrite(motorB.speedPinD, abs(mspeed));
  157.       motorB.mForward = false;
  158.     }
  159.   }
  160.  
  161. }
  162.  
  163.  
  164. /*                                                                                    *
  165.  *                motorTurn - change the speed of each wheel idividually.             *
  166.  *                                                                                    */
  167. void motorTurn(int lSpeed, int rSpeed){
  168.   motorWrite('A', lSpeed);
  169.   motorWrite('B', rSpeed);
  170. }
  171. void motorTurn(int mspeed){
  172.   motorWrite('A', mspeed);
  173.   motorWrite('B', mspeed);
  174. }
  175. /*                                                                                    *
  176.  *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
  177.  *            use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE               *
  178.  *                                                                                    */
  179. void spinAround(boolean clockwise){
  180.   if(clockwise == true){
  181.     // Makes the other wheel go forward, the other one backward.
  182.  
  183.   }
  184. }
  185. /*                                                                                    *
  186.  *                                 brake - stops all motors                           *
  187.  *                                                                                    */
  188. void brake(){
  189.   digitalWrite(motorA.brakePinD, HIGH);
  190.   digitalWrite(motorB.brakePinD, HIGH);
  191. }
  192.  
  193. /*                                                                                    *
  194.  *                       unlock - reverses the effects of brake                       *
  195.  *                                                                                    */
  196. void unlock(){
  197.   digitalWrite(motorA.brakePinD, LOW);
  198.   digitalWrite(motorB.brakePinD, LOW);
  199. }
  200.  
  201. /*                                                                                    *
  202.  *         gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results)       *
  203.  *                                                                                    */
  204. boolean gotKey(){
  205.   return irrecv.decode(&results);
  206. }
  207.  
  208. /*                                                                                    *
  209.  *      getKey - "lazydog" for decode_results results.value & 2047                    *
  210.  *                                                                                    */
  211. int getKey(){
  212.   return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
  213. }
  214. /*                                                                                    *
  215.  *          softReset - macro for soft reseting the arduino, doesn't wipe             *
  216.  *                                                                                    */
  217. void softReset(){
  218.   asm volatile ("  jmp 0");
  219. }
  220. /*                                   FUNCTIONS ENDS HERE                              */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement