Advertisement
Guest User

button.c

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