Advertisement
RuiViana

Step_Motor_Poteciometro

Nov 16th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. /* FILE: HCMotor_Stepper_Example
  2. DATE: 09/07/15
  3. VERSION: 0.1
  4. AUTHOR: Andrew Davies
  5.  
  6. http://forum.hobbycomponents.com/viewtopic.php?f=58&t=1870
  7.  
  8.  
  9. This example uses the library to control a stepper motor via a standard stepper driver
  10. module (with step/clock & direction inputs) using a potentiometer connected to
  11. analogue pin A0. For suitable driver modules see items HCMODU0022 & HCMODU0068.
  12.  
  13. Do not connect the motor directly to your Arduino's digital pins as you may damage your
  14. Arduino.
  15.  
  16. Note about driving more than one motor:
  17. By default this library can drive up to 4 motors. However this can be increased by
  18. editing the following line in the libraries HCMotor.h file:
  19.  
  20. #define MAXMOTORS 4 <-- change to match the number of motors you require.
  21.  
  22. If you are using less than 4 motors and your sketch is processor intensive you may
  23. also want to reduce this value to match the number of motors you have attached as this
  24. will free up processing cycles for your main loop.
  25.  
  26. You may copy, alter and reuse this code in any way you like, but please leave
  27. reference to HobbyComponents.com in your comments if you redistribute this code.
  28. This software may not be used directly for the purpose of selling products that
  29. directly compete with Hobby Components Ltd's own range of products.
  30.  
  31. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  32. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  33. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  34. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  35. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  36. REASON WHATSOEVER.
  37. */
  38.  
  39.  
  40.  
  41. /* Include the library */
  42. #include "HCMotor.h"
  43.  
  44. /* Pins used to drive the motors */
  45. #define DIR_PIN 8 //Connect to drive modules 'direction' input.
  46. #define CLK_PIN 9 //Connect to drive modules 'step' or 'CLK' input.
  47.  
  48. /* Set the analogue pin the potentiometer will be connected to. */
  49. #define POT_PIN A0
  50.  
  51. /* Set a dead area at the centre of the pot where it crosses from forward to reverse */
  52. #define DEADZONE 20
  53.  
  54. /* The analogue pin will return values between 0 and 1024 so divide this up between
  55. forward and reverse */
  56. #define POT_REV_MIN 0
  57. #define POT_REV_MAX (512 - DEADZONE)
  58. #define POT_FWD_MIN (512 + DEADZONE)
  59. #define POT_FWD_MAX 1024
  60.  
  61.  
  62. /* Create an instance of the library */
  63. HCMotor HCMotor;
  64.  
  65.  
  66.  
  67. void setup()
  68. {
  69. //Serial.begin(9600);
  70. /* Initialise the library */
  71. HCMotor.Init();
  72.  
  73. /* Attach motor 0 to digital pins 8 & 9. The first parameter specifies the
  74. motor number, the second is the motor type, and the third and forth are the
  75. digital pins that will control the motor */
  76. HCMotor.attach(0, STEPPER, CLK_PIN, DIR_PIN);
  77.  
  78. /* Set the number of steps to continuous so the the motor is always turning whilst
  79. not int he dead zone*/
  80. HCMotor.Steps(0,CONTINUOUS);
  81. }
  82.  
  83.  
  84. void loop()
  85. {
  86. int Speed, Pot;
  87.  
  88. /* Read the analogue pin to determine the position of the pot. */
  89. Pot = analogRead(POT_PIN);
  90.  
  91. /* Is the pot in the reverse position ? */
  92. if (Pot >= POT_REV_MIN && Pot <= POT_REV_MAX)
  93. {
  94. HCMotor.Direction(0, REVERSE);
  95. Speed = map(Pot, POT_REV_MIN, POT_REV_MAX, 10, 1024);
  96.  
  97. /* Is the pot in the forward position ? */
  98. }else if (Pot >= POT_FWD_MIN && Pot <= POT_FWD_MAX)
  99. {
  100. HCMotor.Direction(0, FORWARD);
  101. Speed = map(Pot, POT_FWD_MIN, POT_FWD_MAX, 1024, 10);
  102.  
  103. /* Is the pot in the dead zone ? */
  104. }else
  105. {
  106. Speed = 0;
  107. }
  108.  
  109. /* Set the duty cycle of the clock signal in 100uS increments */
  110. HCMotor.DutyCycle(0, Speed);
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement