Advertisement
Guest User

Untitled

a guest
Feb 16th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1.  
  2. char commanddata[2];
  3. byte speeddata;
  4.  
  5. unsigned long time;
  6. unsigned long c1timer;
  7.  
  8. int pwm_a = 6; //PWM control for motor outputs 1 and 2 is on digital pin 3
  9. int pwm_b = 9; //PWM control for motor outputs 3 and 4 is on digital pin 11
  10. int dir_a = 7; //direction control for motor outputs 1 and 2 is on digital pin 12
  11. int dir_b = 8; //direction control for motor outputs 3 and 4 is on digital pin 13
  12.  
  13. void setup()
  14. {
  15. // initialize the serial communication:
  16. Serial.begin(9600);
  17. Serial.setTimeout(3);
  18.  
  19. pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
  20. pinMode(pwm_b, OUTPUT);
  21. pinMode(dir_a, OUTPUT);
  22. pinMode(dir_b, OUTPUT);
  23.  
  24. analogWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
  25. analogWrite(pwm_b, 0);
  26. }
  27.  
  28. void loop() {
  29. byte command;
  30. byte relaycommand;
  31.  
  32.  
  33.  
  34. // check if data has been sent from the computer:
  35. if(Serial.readBytes(commanddata,2)){
  36. // read the most recent byte (which will be from 0 to 255):
  37. command = commanddata[0];
  38. speeddata = commanddata[1];
  39.  
  40.  
  41. switch (command) {
  42. case 90: // get software version 5a
  43. Serial.write(15);
  44. Serial.write(1);
  45. break;
  46. case 91: // get relay states 5b
  47. //Serial.write(50);
  48. break;
  49. case 93: // get relay states 5b
  50. Serial.write(50);
  51. break;
  52. case 100: // motorA forward
  53. digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
  54. analogWrite(pwm_a, speeddata);
  55. c1timer=millis();
  56. break;
  57. case 101: // motorB forward
  58. digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
  59. analogWrite(pwm_b, speeddata);
  60. c1timer=millis();
  61. break;
  62. case 102: // motorA backward
  63. digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low
  64. analogWrite(pwm_a, speeddata);
  65. c1timer=millis();
  66. break;
  67. case 103: // motorB backward
  68. digitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high
  69. analogWrite(pwm_b, speeddata);
  70. c1timer=millis();
  71. break;
  72. }
  73.  
  74. }
  75.  
  76. time = millis();
  77. if((time-c1timer)>500){
  78. analogWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
  79. analogWrite(pwm_b, 0);
  80. }
  81.  
  82.  
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement