Advertisement
RuiViana

Controle posicao de Step com Potencimetro

Nov 17th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. /* FILE: Posição Step
  2. DATE: 17/11/15
  3. VERSION: 0.1
  4. AUTHOR: Rui
  5. */
  6.  
  7. #define DIR_PIN 8 //Connect to drive modules 'direction' input.
  8. #define STEP_PIN 9 //Connect to drive modules 'step' or 'CLK' input.
  9.  
  10. /* Set the analogue pin the potentiometer will be connected to. */
  11. #define POT_PIN A0
  12. int Pos = 0; // inicial position off stepper
  13. int Speed, Val;
  14. unsigned int Angulo;
  15. byte Vel = 10;
  16. byte Sample = 20;
  17. //---------------------------------
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21. pinMode(DIR_PIN, OUTPUT);
  22. pinMode(STEP_PIN, OUTPUT);
  23. }
  24. //--------------------------------
  25. void loop()
  26. {
  27. for (unsigned int j = 0; j<Sample ; j++)
  28. {
  29. Val = Val = analogRead(POT_PIN);
  30. delay(1);
  31. }
  32. Val + Val /Sample;
  33. Angulo = map(Val,0,1023,1,100);
  34. Pos = Angulo-Pos;
  35. if (Pos>0)
  36. {
  37. digitalWrite(DIR_PIN,HIGH);
  38. for (unsigned i = 0 ; i<abs(Pos) ; i++)
  39. {
  40. digitalWrite(STEP_PIN,HIGH);
  41. delay(Vel);
  42. digitalWrite(STEP_PIN,LOW);
  43. delay(Vel);
  44. }
  45. }
  46. else
  47. {
  48. digitalWrite(DIR_PIN,LOW);
  49. for (unsigned i = 0 ; i<abs(Pos) ; i++)
  50. {
  51. digitalWrite(STEP_PIN,HIGH);
  52. delay(Vel);
  53. digitalWrite(STEP_PIN,LOW);
  54. delay(Vel);
  55. }
  56. }
  57. Pos = Angulo;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement