Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. int motorOneLeft = 3;
  2. int motorOneRight = 4;
  3. int motorTwoLeft = 5;
  4. int motorTwoRight = 6;
  5. int motorThreeLeft = 7;
  6. int motorThreeRight = 8;
  7.  
  8. int motorAllDelay = 1000;
  9.  
  10. int savedButtonState = 0;
  11. int savedTime = 0;
  12.  
  13. void internal_adjustMotor(int leftAddress, int rightAddress, int leftVal, int RightVal, int speed) {
  14. if (leftVal == 0) {
  15. digitalWrite(leftAddress, LOW);
  16. }
  17. else if (leftVal == 1) {
  18. digitalWrite(leftAddress, HIGH);
  19. }
  20.  
  21. if (rightVal == 0) {
  22. digitalWrite(rightAddress, LOW);
  23. }
  24. else if (rightVal == 1) {
  25. digitalWrite(rightAddress, HIGH);
  26. }
  27.  
  28. // Speed
  29. analogWrite(leftAddress, speed);
  30. analogWrite(rightAddress, speed);
  31. }
  32.  
  33. void digitalMotorMode(int motor, int status, int speed) { // status 0 = off, status 1 = left, status 2 = right
  34. if (motor == 1) {
  35. if (status == 0) {
  36. internal_adjustMotor(motorOneLeft, motorOneRight, 0, 0, speed);
  37. }
  38. else if (status == 1) {
  39. internal_adjustMotor(motorOneLeft, motorOneRight, 1, 0, speed);
  40. }
  41. else if (status == 2) {
  42. internal_adjustMotor(motorOneLeft, motorOneRight, 0, 1, speed);
  43. }
  44. }
  45. if (motor == 2) {
  46. if (status == 0) {
  47. internal_adjustMotor(motorTwoLeft, motorTwoRight, 0, 0, speed);
  48. }
  49. else if (status == 1) {
  50. internal_adjustMotor(motorTwoLeft, motorTwoRight, 1, 0, speed);
  51. }
  52. else if (status == 2) {
  53. internal_adjustMotor(motorTwoLeft, motorTwoRight, 0, 1, speed);
  54. }
  55. }
  56. if (motor == 3) {
  57. if (status == 0) {
  58. internal_adjustMotor(motorThreeLeft, motorThreeRight, 0, 0, speed);
  59. }
  60. else if (status == 1) {
  61. internal_adjustMotor(motorThreeLeft, motorThreeRight, 1, 0, speed);
  62. }
  63. else if (status == 2) {
  64. internal_adjustMotor(motorThreeLeft, motorThreeRight, 0, 1, speed);
  65. }
  66. }
  67. }
  68.  
  69. void loop() {
  70. int buttonState = digitalRead(pushButton);
  71.  
  72. if (savedButtonState == 0 && buttonState == 1) {
  73. // Wir nehmen an 2 = Ausfahren (right)
  74. digitalMotorMode(1, 2, 10);
  75. digitalMotorMode(2, 2, 10);
  76. digitalMotorMode(3, 2, 10);
  77.  
  78. // We got with a sum delay here, it's easier to set the motor speed .. another option would be
  79. // delaying after each motor starting with the one taking the most time, second most etc.
  80. delay(motorAllDelay);
  81.  
  82. // power off
  83. digitalMotorMode(1, 0, 0);
  84. digitalMotorMode(2, 0, 0);
  85. digitalMotorMode(3, 0, 0);
  86. }
  87. else if (savedButtonState == 1 && buttonState == 0) {
  88. // Wir nehmen an 1 = Einfahren (left)
  89. digitalMotorMode(1, 1, 10);
  90. digitalMotorMode(2, 1, 10);
  91. digitalMotorMode(3, 1, 10);
  92.  
  93. delay(motorAllDelay);
  94.  
  95. // power off
  96. digitalMotorMode(1, 0, 0);
  97. digitalMotorMode(2, 0, 0);
  98. digitalMotorMode(3, 0, 0);
  99.  
  100. savedTime = 0;
  101. }
  102. else if (buttonState == 1) {
  103. savedTime += 1;
  104. if (savedTime == 10000) { // 10 sec
  105. digitalMotorMode(1, 1, 10);
  106. digitalMotorMode(2, 1, 10);
  107. digitalMotorMode(3, 1, 10);
  108.  
  109. delay(motorAllDelay);
  110.  
  111. digitalMotorMode(1, 0, 0);
  112. digitalMotorMode(2, 0, 0);
  113. digitalMotorMode(3, 0, 0);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement