Advertisement
nikolas77

moteur pas a pas avec joystick

Apr 21st, 2024
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <Stepper.h>
  2.  
  3. const int stepsPerRevolution = 48;
  4.  
  5. Stepper myStepper(stepsPerRevolution, 8,10,9,11);
  6.  
  7. unsigned long lastTime = 0;
  8. unsigned long thisTime = 0;
  9. int led = 12;
  10.  
  11. void setup() {
  12.  
  13. myStepper.setSpeed(350);
  14. pinMode(led, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18.  
  19. int sensorReading = analogRead(A0);
  20. int motorSpeed = map(sensorReading, 0, 1023, -500, 500);
  21.  
  22. thisTime = millis();
  23.  
  24. if (abs(motorSpeed) > 0){
  25.  
  26. if ((thisTime - lastTime) > abs(abs(motorSpeed)-500)){
  27.  
  28. if (motorSpeed > 0){
  29. myStepper.step(1);
  30. }
  31. else
  32. {
  33. myStepper.step(-1);
  34. }
  35. lastTime = thisTime;
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement