v12345vtm

pantilt-endlimit

May 1st, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. /*
  2. * Switch test program
  3. * with end limit switches by v12345vtm
  4. */
  5.  
  6. /*
  7. Changing the PWM frequency on Pins 11 and 3
  8. Setting Divisor Frequency
  9. 0x01 1 31250 //should not be able to hear this though the frequency is a little high for the l298
  10. 0x02 8 3906.25
  11. 0x03 32 976.5625
  12. 0x04 64 488.28125 // default
  13. 0x05 128 244.140625
  14. 0x06 256 122.0703125
  15. 0x07 1024 30.517578125 //here is also good..in theroy but may be a little to low for the dc motor
  16. */
  17.  
  18.  
  19. int potx = A0; // analog pin 0, connected to VRx on the joystick board
  20. int poty = A1; // analog pin 1, connected to VRy on the joystick board
  21. // The joystick board 5 volts needs to be connected to the Arduino 5 volts pin
  22. // The joystick board ground needs to be connected to an Arduino ground pin
  23.  
  24. int FDCX1 = A2;
  25. int FDCX2 = A3;
  26. int FDCY1 = A4;
  27. int FDCY2 = A5;
  28.  
  29. int motorX = 3; //Digital pin 3, connected to Enable pin A(ENA) on the L298N
  30. int motorY = 11; //Digital pin 11, connected to Enable pin B(ENB) on the L298N
  31.  
  32. int IN1 = 4; //Digital pin 4, connected to IN1 on the L298N
  33. int IN2 = 5; //Digital pin 5, connected to IN2 on the L298N
  34. int IN3 = 6; //Digital pin 6, connected to IN3 on the L298N
  35. int IN4 = 7; //Digital pin 7, connected to IN4 on the L298N
  36. // The L298N ground needs to be connected to an Arduino ground pin.
  37. // In the L298N one DC motor terminals will be connected to OUT1 and OUT2 and the second DC motor to OUT3 and OUT4
  38. // polarity in the motors is determined by the direction that you want for pan and tilt.
  39.  
  40. int value = 6; //4 being the default giving 488.28125hz
  41.  
  42. int CenterX = 0;
  43. int CenterY = 0;
  44.  
  45. void setup() // run once, when the sketch starts
  46. {
  47. Serial.begin(115200); // set up Serial library at 9600 bps
  48. pinMode(potx, INPUT);
  49. pinMode(poty, INPUT);
  50. pinMode(motorX, OUTPUT);
  51. pinMode(motorY, OUTPUT);
  52. pinMode (IN1, OUTPUT);
  53. pinMode (IN2, OUTPUT);
  54. pinMode (IN3, OUTPUT);
  55. pinMode (IN4, OUTPUT);
  56.  
  57.  
  58.  
  59.  
  60. pinMode (FDCX1, INPUT_PULLUP);
  61. pinMode (FDCX2, INPUT_PULLUP);
  62. pinMode (FDCY1, INPUT_PULLUP);
  63. pinMode (FDCY2, INPUT_PULLUP);
  64.  
  65. TCCR2B = (TCCR2B & 0xF8) | value;// change the value of the (Timer 2 pins 11, 3?) register to a value ranging between 1 - 7
  66.  
  67. CenterX = analogRead(potx); // Get the center x value in the joystick
  68. CenterY = analogRead(poty); // Get the center y value in the joystick
  69.  
  70. }
  71.  
  72. int getPotX() {
  73. int v;
  74. v = analogRead(potx);
  75. return v;
  76. }
  77. int getPotY() {
  78. int v;
  79. v = analogRead(poty);
  80. return v;
  81. }
  82.  
  83. int moveMotorX() {
  84. int ValueX = getPotX();
  85. Serial.print("potx=");
  86. Serial.print(ValueX);
  87.  
  88. if( ValueX < CenterX ){
  89. digitalWrite (IN1, HIGH);
  90. digitalWrite (IN2, LOW);
  91. ValueX = CenterX - ValueX;
  92. ValueX /= 4;
  93. ValueX = max(ValueX, 0);
  94. ValueX = min(ValueX, 255);
  95. analogWrite(motorX, ValueX);
  96.  
  97. if( digitalRead(FDCX2) == 0)
  98. {
  99. ValueX = CenterX;
  100. digitalWrite (IN1, LOW);
  101. digitalWrite (IN2, LOW);
  102. }
  103.  
  104. }
  105. else{
  106. digitalWrite (IN1, LOW);
  107. digitalWrite (IN2, HIGH);
  108. ValueX = ValueX - CenterX;
  109. ValueX /= 4;
  110. ValueX = max(ValueX, 0);
  111. ValueX = min(ValueX, 255);
  112. analogWrite(motorX, ValueX);
  113.  
  114.  
  115. if( digitalRead(FDCX1) == 0)
  116. {
  117. ValueX = CenterX;
  118. digitalWrite (IN1, LOW);
  119. digitalWrite (IN2, LOW);
  120. }
  121.  
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
  130. int moveMotorY() {
  131.  
  132. int ValueY = getPotY();
  133.  
  134. Serial.print(" poty=");
  135. Serial.println(ValueY);
  136. if( ValueY < CenterY ){
  137. digitalWrite (IN3, HIGH);
  138. digitalWrite (IN4, LOW);
  139. ValueY = CenterY - ValueY;
  140. ValueY /= 4;
  141. ValueY = max(ValueY, 0);
  142. ValueY = min(ValueY, 255);
  143. analogWrite(motorY, ValueY);
  144.  
  145.  
  146. if( digitalRead(FDCY1) == 0)
  147. {
  148. ValueY = CenterY;
  149. digitalWrite (IN3, LOW);
  150. digitalWrite (IN4, LOW);
  151. }
  152. }
  153. else{
  154. digitalWrite (IN3, LOW);
  155. digitalWrite (IN4, HIGH);
  156. ValueY = ValueY - CenterY;
  157. ValueY /= 4;
  158. ValueY = max(ValueY, 0);
  159. ValueY = min(ValueY, 255);
  160. analogWrite(motorY, ValueY);
  161.  
  162.  
  163. if( digitalRead(FDCY2) == 0)
  164. {
  165. ValueY = CenterY;
  166. digitalWrite (IN3, LOW);
  167. digitalWrite (IN4, LOW);
  168. }
  169.  
  170. }
  171. }
  172.  
  173. void loop() // run over and over again
  174. {
  175. moveMotorX();
  176. moveMotorY();
  177.  
  178. Serial.print(digitalRead(FDCX1));
  179. Serial.print(digitalRead(FDCX2));
  180. Serial.print(" ");
  181. Serial.print(digitalRead(FDCY1));
  182. Serial.println(digitalRead(FDCY2));
  183.  
  184.  
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment