Guest User

Untitled

a guest
Jan 17th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. int button=2;
  2. int sw;
  3. const int x_axis = 4;
  4. const int y_axis = 5;
  5. int dir1PinA = 4;
  6. int speedPinA = 5; // Needs to be a PWM pin to be able to control motor speed
  7. int dir2PinB = 7;
  8. int speedPinB = 6; // Needs to be a PWM pin to be able to control motor speed
  9. int x_hiz=0;
  10. int y_hiz=0;
  11. boolean lock;
  12.  
  13. void setup() {
  14.  
  15. Serial.begin(9600);
  16. pinMode(button, INPUT);
  17. digitalWrite(button,HIGH);
  18. pinMode(dir1PinA,OUTPUT);
  19. pinMode(speedPinA,OUTPUT);
  20. pinMode(dir2PinB,OUTPUT);
  21. pinMode(speedPinB,OUTPUT);
  22. //Setup Channel A
  23. pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  24. pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  25.  
  26. //Setup Channel B
  27. pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  28. pinMode(8, OUTPUT); //Initiates Brake Channel B pin
  29. }
  30.  
  31. void loop() {
  32. sw = digitalRead(button);
  33. if (sw == LOW)
  34. {
  35. analogWrite(3, 0);
  36. analogWrite(11, 0);
  37. y_hiz= analogRead(y_axis);
  38. //y_hiz= map(y_hiz,0,1023,255,0);
  39.  
  40. if (y_hiz > 600)
  41. {
  42. analogWrite(speedPinA, 127);//Sets speed variable via PWM
  43. analogWrite(speedPinB, 127);//Sets speed variable via PWM
  44. digitalWrite(dir1PinA, LOW);
  45. digitalWrite(dir2PinB, HIGH);
  46. lock = true;
  47. }
  48.  
  49. else if (y_hiz < 424)
  50. {
  51. analogWrite(speedPinA, 127);//Sets speed variable via PWM
  52. analogWrite(speedPinB, 127);//Sets speed variable via PWM
  53. digitalWrite(dir1PinA, HIGH);
  54. digitalWrite(dir2PinB, LOW);
  55. lock = true;
  56. }
  57. else
  58. {
  59. digitalWrite(12, HIGH);
  60. digitalWrite(9, LOW);
  61. analogWrite(3, 0);
  62. digitalWrite(13, HIGH);
  63. digitalWrite(8, LOW);
  64. analogWrite(11, 0);
  65. analogWrite(speedPinA, 0);
  66. analogWrite(speedPinB, 0);
  67. lock = false;
  68. }
  69. }
  70. else if (sw == HIGH)
  71. {
  72. analogWrite(speedPinA, 0);
  73. analogWrite(speedPinB, 0);
  74. y_hiz= analogRead(y_axis);
  75. //y_hiz= map(y_hiz,0,1023,255,0);
  76. x_hiz = analogRead(x_axis);
  77.  
  78. if (y_hiz > 324 && y_hiz < 700 || lock)
  79. {
  80. analogWrite(3, 0);
  81. analogWrite(11, 0);
  82. }
  83.  
  84. if (y_hiz > 700)
  85. {
  86. if (x_hiz > 700)
  87. {
  88. analogWrite(3, 127);//Sets speed variable via PWM
  89. analogWrite(11, 0);//Sets speed variable via PWM
  90. }
  91. else if (x_hiz < 324)
  92. {
  93. analogWrite(3, 0);//Sets speed variable via PWM
  94. analogWrite(11, 127);//Sets speed variable via PWM
  95. }
  96. else
  97. {
  98. analogWrite(3, 127);//Sets speed variable via PWM
  99. analogWrite(11, 127);//Sets speed variable via PWM
  100.  
  101. }
  102. digitalWrite(12, HIGH);
  103. digitalWrite(13, HIGH);
  104. }
  105.  
  106. else if (y_hiz < 324)
  107. {
  108. if (x_hiz > 700)
  109. {
  110. analogWrite(3, 127);//Sets speed variable via PWM
  111. analogWrite(11, 0);//Sets speed variable via PWM
  112. }
  113. else if (x_hiz < 324)
  114. {
  115. analogWrite(3, 0);//Sets speed variable via PWM
  116. analogWrite(11, 127);//Sets speed variable via PWM
  117. }
  118. else
  119. {
  120. analogWrite(3, 127);//Sets speed variable via PWM
  121. analogWrite(11, 127);//Sets speed variable via PWM
  122. }
  123. digitalWrite(12, HIGH);
  124. digitalWrite(13, HIGH);
  125. }
  126.  
  127. else
  128. {
  129. analogWrite(3, 0);
  130. analogWrite(11, 0);
  131. }
  132. }
  133.  
  134. /* Serial.print("vertical: ");
  135. Serial.print(y_hiz,DEC);
  136. Serial.print(" horizontal: ");
  137. Serial.print(x_hiz,DEC);
  138. Serial.print(" select: ");
  139. if(sw == HIGH)
  140. Serial.println("not pressed");
  141. else
  142. Serial.println("PRESSED!");*/
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment