Advertisement
Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /* By Jacob Vilevac */
  2.  
  3. #include <Servo.h>
  4.  
  5. //User configuration:
  6. int percent = 0; // between -100 and 100, indicates boot speed
  7.  
  8. int pins[] = {5, 6}; // Left Motor, Right Motor (L:5, R:6)
  9. int maxSpeed = 40;
  10. int leftMotor = 0;
  11. int rightMotor = 0;
  12. int motors[] = {leftMotor, rightMotor};
  13.  
  14. const int arraySize = sizeof(pins)/sizeof(int);
  15. Servo controllers[arraySize];
  16.  
  17. void setup() {
  18. Serial.begin(9600);
  19. Serial.println("Ready For Input\n");
  20. controllers[0].attach(pins[0]);
  21. controllers[1].attach(pins[1]);
  22. delay(1000);
  23. Serial.println("Input Command and Output PWM\n");
  24. }
  25.  
  26. void loop() {
  27. controllers[0].writeMicroseconds(pwm(leftMotor));
  28. controllers[1].writeMicroseconds(pwm(rightMotor));
  29. Serial.println(pwm(leftMotor));
  30.  
  31. if (Serial.available()) {
  32. int input = Serial.read();
  33. if (input != 13) {
  34. Serial.println(input);
  35. handleInput(input);
  36. }
  37. }
  38. }
  39.  
  40. int pwm(int input) {
  41. // int result = input * 5 + 1500;
  42. int result = input * 20 + 1500;
  43. return result;
  44. }
  45.  
  46. void motor(String input) {
  47. if (input.equals("forward")) {
  48. if (leftMotor != rightMotor) { // If we're turning at all (motors not going same speed)
  49. if (leftMotor > rightMotor) { // Set the slower motor to go as fast as the faster motor
  50. rightMotor = leftMotor;
  51. }
  52. else {
  53. leftMotor = rightMotor;
  54. }
  55. }
  56. else { // Increase speed by 1
  57. rightMotor += 1;
  58. leftMotor += 1;
  59. }
  60. }
  61. else if (input.equals("back")) {
  62. if (leftMotor != rightMotor) { // If we're turning at all (motors not going same speed)
  63. // Set the slower motor to go as fast as the faster motor (because they are negatives the faster motor is less than the slower motor)
  64. if (leftMotor < rightMotor) rightMotor = leftMotor;
  65. else leftMotor = rightMotor;
  66. }
  67. else { // Decrease speed by 1
  68. rightMotor -= 1;
  69. leftMotor -= 1;
  70. }
  71. }
  72. else if (input.equals("left")) { // Movement is like turning the wheel of a car
  73. if (rightMotor < 0 || leftMotor < 0) rightMotor -= 1; // Reversing we want negative because speed is negative
  74. else rightMotor += 1;
  75. }
  76. else if (input.equals("right")) {
  77. if (rightMotor < 0 || leftMotor < 0) leftMotor -= 1;
  78. else leftMotor += 1;
  79. }
  80. else if (input.equals("stop")) {
  81. rightMotor = 0;
  82. leftMotor = 0;
  83. }
  84. }
  85.  
  86. void handleInput(float input) {
  87. if (input == 119) motor("forward"); // w
  88. else if (input == 97) motor("left"); // a
  89. else if (input == 100) motor("right"); // d
  90. else if (input == 115) motor("back"); // s
  91. else if (input == 120) motor("stop"); // X x
  92. else if (input == 113); // Square q
  93. else if (input == 99); // Circle c
  94. else if (input == 116); // Triangle t
  95. else if (input == 108); // Select l
  96. else if (input == 114); // Start r
  97. else if (input == 13);
  98. else Serial.println("Broken");
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement