Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. /* Jazda w przod i tyl dwa silniki*/
  2. #include <Servo.h>
  3. Servo centerServo;
  4. const int motorDirectionA = 12;
  5. const int motorDirectionB = 13;
  6. const int motorSpeedA = 3;
  7. const int motorSpeedB = 11;
  8. const int motorBrakeA = 9;
  9. const int motorBrakeB = 8;
  10. const int fullSpeed = 255;
  11. const int halfSpeed = 123;
  12. char command;
  13. void setup() {
  14. Serial.begin(9600);
  15. pinMode(16, OUTPUT);
  16. centerServo.attach(16);
  17. pinMode(motorDirectionA, OUTPUT);
  18. pinMode(motorBrakeA, OUTPUT);
  19. pinMode(motorDirectionB, OUTPUT);
  20. pinMode(motorBrakeB, OUTPUT);
  21. centerServo.write(90);
  22. }
  23. void forward() {
  24. digitalWrite(motorDirectionA, LOW); //Forward Direction for Motor A
  25. digitalWrite(motorDirectionB, HIGH); //Forward Direction for Motor B
  26. digitalWrite(motorBrakeA, LOW); //Turn off brake for Motor A
  27. digitalWrite(motorBrakeB, LOW); //Turn off brake for Motor B
  28. analogWrite(motorSpeedA, 220); //Set speed of Motor A to MAX.
  29. analogWrite(motorSpeedB, 220); //Set speed of Motor A to MAX.
  30. }
  31. void backward() {
  32. digitalWrite(motorDirectionA, HIGH); //Backward Direction for Motor A
  33. digitalWrite(motorDirectionB, LOW); //Backward Direction for Motor B
  34. digitalWrite(motorBrakeA, LOW); //Turn off brake for Motor A
  35. digitalWrite(motorBrakeB, LOW); //Turn off brake for Motor B
  36. analogWrite(motorSpeedA, 220); //Set speed of Motor A to half of MAX.
  37. analogWrite(motorSpeedB, 220); //Set speed of Motor A to half of MAX.
  38. }
  39. void right() {
  40. digitalWrite(motorDirectionA, HIGH); //Backward Direction for Motor A
  41. digitalWrite(motorDirectionB, LOW); //Backward Direction for Motor B
  42. digitalWrite(motorBrakeA, LOW); //Turn off brake for Motor A
  43. digitalWrite(motorBrakeB, LOW); //Turn off brake for Motor B
  44. analogWrite(motorSpeedA, 0); //Set speed of Motor A to half of MAX.
  45. analogWrite(motorSpeedB, 220); //Set speed of Motor A to half of MAX.
  46. }
  47. void left() {
  48. digitalWrite(motorDirectionA, HIGH); //Backward Direction for Motor A
  49. digitalWrite(motorDirectionB, LOW); //Backward Direction for Motor B
  50. digitalWrite(motorBrakeA, LOW); //Turn off brake for Motor A
  51. digitalWrite(motorBrakeB, LOW); //Turn off brake for Motor B
  52. analogWrite(motorSpeedA, 220); //Set speed of Motor A to half of MAX.
  53. analogWrite(motorSpeedB, 0); //Set speed of Motor A to half of MAX.
  54. }
  55. void brake() {
  56. digitalWrite(motorBrakeA, HIGH); //Turn on brake for Motor A
  57. digitalWrite(motorBrakeB, HIGH); //Turn on brake for Motor B
  58. }
  59. void loop() {
  60. if (Serial.available() > 0) {
  61. command = Serial.read();
  62. switch (command) {
  63. case 'F':
  64. forward();
  65. break;
  66. case 'B':
  67. backward();
  68. break;
  69. case 'L':
  70. left();
  71. break;
  72. case 'R':
  73. right();
  74. break;
  75. case 'S':
  76. brake();
  77. break;
  78. case 'V':
  79. centerServo.write(90);
  80. break;
  81. case '0':
  82. centerServo.write(20);
  83. break;
  84. case '1':
  85. centerServo.write(30);
  86. break;
  87. case '2':
  88. centerServo.write(40);
  89. break;
  90. case '3':
  91. centerServo.write(60);
  92. break;
  93. case '4':
  94. centerServo.write(80);
  95. break;
  96. case '5':
  97. centerServo.write(100);
  98. break;
  99. case '6':
  100. centerServo.write(120);
  101. break;
  102. case '7':
  103. centerServo.write(140);
  104. break;
  105. case '8':
  106. centerServo.write(160);
  107. break;
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement