Guest User

Arduino RC car proto.

a guest
Mar 19th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1.  
  2. struct WheelStruct {
  3.   int mspeed; // Max is 255
  4.   boolean mForward; // Is going forward or not.
  5.   const int speedPinD; // Digital, PWM
  6.   const int directionPinD; // Digital
  7.   const int brakePinD; // Digital (I've got no clue what this is.)
  8.   const int currentSensingPinA; // Analog
  9.  
  10. };
  11.  
  12. const int ZERO = 0;
  13. const int MAX = 255;
  14. const int HALF = 128;
  15. const int QUARTER = 64;
  16.  
  17. WheelStruct motorA = {
  18.   ZERO, true, 3, 12, 9, 0}; // Motor A is left
  19. WheelStruct motorB = {
  20.   ZERO, true, 11, 13, 8, 1}; // Motor B is right.
  21.  
  22. const int ledLampAPin = 4;
  23. const int ledLampBPin = 5;
  24. /*                                   PROTOTYPE STARTS HERE                           */
  25.  
  26. void switchWheelDirection(int);
  27. void writeMotor(int, int);
  28. void forward(int);
  29. void motorTurn(int, int);
  30. void spinAround(boolean);
  31.  
  32. /*                                   PROTOTYPE ENDS HERE                             */
  33.  
  34.  
  35. void setup(){
  36.  
  37.   pinMode(motorA.speedPinD, OUTPUT);
  38.   pinMode(motorB.speedPinD, OUTPUT);
  39.  
  40.   pinMode(motorA.directionPinD, OUTPUT);
  41.   pinMode(motorB.directionPinD, OUTPUT);
  42.  
  43.   pinMode(motorA.brakePinD, OUTPUT);
  44.   pinMode(motorB.brakePinD, OUTPUT);
  45.  
  46.   pinMode(motorA.currentSensingPinA, INPUT);
  47.   pinMode(motorB.currentSensingPinA, INPUT);
  48. }
  49. void loop(){
  50.   forward(255);
  51.  
  52. }
  53.  
  54. /*                                   FUNCTIONS STARTS HERE                            */
  55.  
  56. /*                                                                                    *
  57.  *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
  58.  *                                                                                    */
  59.  
  60. void switchWheelDirection(int mDirPin){
  61. }
  62.  
  63.  
  64. /*                                                                                    *
  65.  *     writeMotor - replaces the analogWrite() function, positive values = forward    *
  66.  *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
  67.  *                                                                                    */
  68. void writeMotor(int pin, int mspeed){
  69.   if (mspeed >= 0 || mspeed < 255){
  70.     analogWrite(pin, mspeed); // Will be writeMotor
  71.   }
  72.   else if(mspeed >= -255 && mspeed <= 0){
  73.     // Switch to backwards and
  74.     analogWrite(pin, abs(mspeed)); // Will be writeMotor
  75.   }
  76.  
  77. }
  78.  
  79.  
  80. /*                                                                                    *
  81.  *forward - changes the state of motor A & B to forward & sets motorX.mspeed to fspeed*
  82.  *                                                                                    */
  83. void forward(int fspeed){
  84.   digitalWrite(motorA.directionPinD, HIGH);
  85.   digitalWrite(motorB.directionPinD, HIGH);
  86.  
  87.   analogWrite(motorA.speedPinD, fspeed); // Will be writeMotor
  88.   analogWrite(motorB.speedPinD, fspeed); // -||-
  89. }
  90.  
  91. /*                                                                                    *
  92.  *                motorTurn - change the speed of each wheel idividually.             *
  93.  *                                                                                    */
  94. void motorTurn(int lSpeed, int rSpeed){
  95.   analogWrite(motorA.speedPinD, lSpeed); // Will be writeMotor
  96.   analogWrite(motorB.speedPinD, rSpeed); // Will be writeMotor
  97. }
  98. /*                                                                                    *
  99.  *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
  100.  *                    use forward or brake to disable.                                *
  101.  *                                                                                    */
  102. void spinAround(boolean clockwise){
  103.   if(clockwise == true){
  104.     // Makes the other wheel go forward, the other one backward.
  105.  
  106.   }
  107. }
  108. /*                                                                                    *
  109.  *                brake - stops all motors and sets their mspeed at 0                 *
  110.  *                                                                                    */
  111. void brake(){
  112.  digitalWrite(motorA.brakePinD, HIGH);
  113.  digitalWrite(motorA.brakePinD, HIGH);
  114.  analogWrite(motorA.speedPinD, ZERO);
  115.  analogWrite(motorB.speedPinD, ZERO);
  116. }
  117.  
  118.  
  119. /*                                   FUNCTIONS ENDS HERE                              */
Advertisement
Add Comment
Please, Sign In to add comment