Regela

VoidGoL293D

Apr 27th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. // Arduino pins for the shift register
  2. #define MOTORLATCH 12
  3. #define MOTORCLK 4
  4. #define MOTORENABLE 7
  5. #define MOTORDATA 8
  6.  
  7. // 8-bit bus after the 74HC595 shift register
  8. // (not Arduino pins)
  9. // These are used to set the direction of the bridge driver.
  10. #define MOTOR1_A 2
  11. #define MOTOR1_B 3
  12. #define MOTOR2_A 1
  13. #define MOTOR2_B 4
  14. #define MOTOR3_A 5
  15. #define MOTOR3_B 7
  16. #define MOTOR4_A 0
  17. #define MOTOR4_B 6
  18.  
  19. // Arduino pins for the PWM signals.
  20. #define MOTOR1_PWM 11
  21. #define MOTOR2_PWM 3
  22. #define MOTOR3_PWM 6
  23. #define MOTOR4_PWM 5
  24. #define SERVO1 10
  25. #define SERVO2 9
  26.  
  27. void shiftWrite(int output, int high_low)
  28. {
  29.   static int latch_copy;
  30.   static int shift_register_initialized = false;
  31.  
  32.   // Do the initialization on the fly,
  33.   // at the first time it is used.
  34.   if (!shift_register_initialized)
  35.   {
  36.     // Set pins for shift register to output
  37.     pinMode(MOTORLATCH, OUTPUT);
  38.     pinMode(MOTORENABLE, OUTPUT);
  39.     pinMode(MOTORDATA, OUTPUT);
  40.     pinMode(MOTORCLK, OUTPUT);
  41.  
  42.     // Set pins for shift register to default value (low);
  43.     digitalWrite(MOTORDATA, LOW);
  44.     digitalWrite(MOTORLATCH, LOW);
  45.     digitalWrite(MOTORCLK, LOW);
  46.     // Enable the shift register, set Enable pin Low.
  47.     digitalWrite(MOTORENABLE, LOW);
  48.  
  49.     // start with all outputs (of the shift register) low
  50.     latch_copy = 0;
  51.  
  52.     shift_register_initialized = true;
  53.   }
  54.  
  55.   // The defines HIGH and LOW are 1 and 0.
  56.   // So this is valid.
  57.   bitWrite(latch_copy, output, high_low);
  58.  
  59.   // Use the default Arduino 'shiftOut()' function to
  60.   // shift the bits with the MOTORCLK as clock pulse.
  61.   // The 74HC595 shiftregister wants the MSB first.
  62.   // After that, generate a latch pulse with MOTORLATCH.
  63.   shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
  64.   delayMicroseconds(5);    // For safety, not really needed.
  65.   digitalWrite(MOTORLATCH, HIGH);
  66.   delayMicroseconds(5);    // For safety, not really needed.
  67.   digitalWrite(MOTORLATCH, LOW);
  68. }
  69.  
  70. void motors_init() {
  71.   //PWMS
  72.   pinMode(MOTOR1_PWM, OUTPUT);
  73.   pinMode(MOTOR2_PWM, OUTPUT);
  74.   pinMode(MOTOR3_PWM, OUTPUT);
  75.   pinMode(MOTOR4_PWM, OUTPUT);
  76. }
  77.  
  78. void motor1_set(int speed) {
  79.   speed = constrain(speed, -255, 255);
  80.   if (speed == 0) {
  81.     shiftWrite(MOTOR1_A, 0);
  82.     shiftWrite(MOTOR1_B, 0);
  83.   } else if (speed < 0) {
  84.     shiftWrite(MOTOR1_A, 0);
  85.     shiftWrite(MOTOR1_B, 1);
  86.     speed *= -1;
  87.   } else {    
  88.     shiftWrite(MOTOR1_A, 1);
  89.     shiftWrite(MOTOR1_B, 0);
  90.   }
  91.   analogWrite(MOTOR1_PWM, speed);
  92. }
  93. void motor2_set(int speed) {
  94.   speed = constrain(speed, -255, 255);
  95.   if (speed == 0) {
  96.     shiftWrite(MOTOR2_A, 0);
  97.     shiftWrite(MOTOR2_B, 0);
  98.   } else if (speed < 0) {
  99.     shiftWrite(MOTOR2_A, 0);
  100.     shiftWrite(MOTOR2_B, 1);
  101.     speed *= -1;
  102.   } else {
  103.     shiftWrite(MOTOR2_A, 1);
  104.     shiftWrite(MOTOR2_B, 0);
  105.   }
  106.   analogWrite(MOTOR2_PWM, speed);
  107. }
  108. void motor3_set(int speed) {
  109.   speed = constrain(speed, -255, 255);
  110.   if (speed == 0) {
  111.     shiftWrite(MOTOR3_A, 0);
  112.     shiftWrite(MOTOR3_B, 0);
  113.   } else if (speed < 0) {
  114.     shiftWrite(MOTOR3_A, 0);
  115.     shiftWrite(MOTOR3_B, 1);
  116.     speed *= -1;
  117.   } else {
  118.     shiftWrite(MOTOR3_A, 1);
  119.     shiftWrite(MOTOR3_B, 0);
  120.   }
  121.   analogWrite(MOTOR3_PWM, speed);
  122. }
  123. void motor4_set(int speed) {
  124.   speed = constrain(speed, -255, 255);
  125.   if (speed == 0) {
  126.     shiftWrite(MOTOR4_A, 0);
  127.     shiftWrite(MOTOR4_B, 0);
  128.   } else if (speed < 0) {
  129.     shiftWrite(MOTOR4_A, 0);
  130.     shiftWrite(MOTOR4_B, 1);
  131.     speed *= -1;
  132.   } else {
  133.     shiftWrite(MOTOR4_A, 1);
  134.     shiftWrite(MOTOR4_B, 0);
  135.   }
  136.   analogWrite(MOTOR4_PWM, speed);
  137. }
  138. void motor1_stop() {
  139.   shiftWrite(MOTOR1_A, 1);
  140.   shiftWrite(MOTOR1_B, 1);
  141. }
  142. void motor2_stop() {
  143.   shiftWrite(MOTOR2_A, 1);
  144.   shiftWrite(MOTOR2_B, 1);
  145. }
  146. void motor3_stop() {
  147.   shiftWrite(MOTOR3_A, 1);
  148.   shiftWrite(MOTOR3_B, 1);
  149. }
  150. void motor4_stop() {
  151.   shiftWrite(MOTOR4_A, 1);
  152.   shiftWrite(MOTOR4_B, 1);
  153. }
  154. void go(int m1, int m2, int m3, int m4) {
  155.   motor1_set(m1);
  156.   motor2_set(m2);
  157.   motor3_set(m3);
  158.   motor4_set(m4);
  159. }
  160. void motors_stop() {
  161.   motor1_stop();
  162.   motor2_stop();
  163.   motor3_stop();
  164.   motor4_stop();
  165. }
  166. void setup() {
  167.   motors_init();
  168. }
  169. void loop() {
  170.   go(255, 255, 255, 255);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment