Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /*
  2. Coded by Alain Felger
  3.  
  4. All rights reserved.
  5. */
  6.  
  7. //Stepper
  8. const int pinDirection = 8;
  9. const int pinStepper = 9;
  10. const int minStepTime = 200;
  11. bool stepperDirection = false;
  12. const int pinMOne = 10;
  13. const int pinMTwo = 11;
  14. const int pinMThree = 12;
  15. long stepperPos = 0L;
  16.  
  17. int delTime = 1000;
  18.  
  19. void setup() {
  20.   pinMode(pinDirection, OUTPUT);
  21.   pinMode(pinStepper, OUTPUT);
  22.   pinMode(pinMOne, OUTPUT);
  23.   pinMode(pinMTwo, OUTPUT);
  24.   pinMode(pinMThree, OUTPUT);
  25.   stepperSetDirection(stepperDirection);
  26.   stepperSetMode(2);
  27.   digitalWrite(pinStepper, HIGH); //To keep momentum on device
  28. }
  29.  
  30. void loop() {
  31. }
  32.  
  33. void stepperSetDirection(bool dir){
  34.   if(dir){
  35.     digitalWrite(pinDirection, HIGH);
  36.   }
  37.   else{digitalWrite(pinDirection, LOW);}
  38. }
  39.  
  40. void doStep(){
  41.     digitalWrite(pinStepper, LOW);
  42.     delayMicroseconds(100);
  43.     digitalWrite(pinStepper, HIGH);
  44.     unsigned long now = millis();
  45.     countStep();
  46.     while(!millis()-now <= minStepTime){
  47.       //do loop
  48.       //if Scan active change data to unusable
  49.     }
  50. }
  51.  
  52. void countStep(){
  53.   //to slow?
  54.   int state;
  55.   if(digitalRead(pinMOne)){
  56.     state = state + 1;
  57.   }
  58.   if(digitalRead(pinMTwo)){
  59.     state = state + 2;
  60.   }
  61.   if(digitalRead(pinMThree)){
  62.     state = state + 3;
  63.   }
  64.   switch(state){
  65.     case B0:
  66.       stepperPos = stepperPos + 1800L;
  67.       break;
  68.     case B1:
  69.       stepperPos = stepperPos + 900L;
  70.       break;
  71.     case B10:
  72.       stepperPos = stepperPos + 450L;
  73.       break;
  74.     case B11:
  75.       stepperPos = stepperPos + 225L;
  76.       break;
  77.     case B111:
  78.       //sixteenth step not possible to keep track of with this method
  79.       break;
  80.   }
  81.   if(stepperPos >= 360000L){
  82.     stepperPos = stepperPos - 360000L;
  83.     }
  84. }
  85.  
  86. void stepperSetMode(int mode){
  87.   switch(mode){
  88.     default:
  89.     case 0:
  90.       digitalWrite(pinMOne, LOW);
  91.       digitalWrite(pinMTwo, LOW);
  92.       digitalWrite(pinMThree, LOW);
  93.       break;
  94.     case 1:
  95.       digitalWrite(pinMOne, HIGH);
  96.       digitalWrite(pinMOne, LOW);
  97.       digitalWrite(pinMOne, LOW);
  98.       break;
  99.     case 2:
  100.       digitalWrite(pinMOne, LOW);
  101.       digitalWrite(pinMOne, HIGH);
  102.       digitalWrite(pinMOne, LOW);
  103.       break;
  104.     case 3:
  105.       digitalWrite(pinMOne, HIGH);
  106.       digitalWrite(pinMOne, HIGH);
  107.       digitalWrite(pinMOne, LOW);
  108.       break;
  109.     case 4:
  110.       digitalWrite(pinMOne, HIGH);
  111.       digitalWrite(pinMOne, HIGH);
  112.       digitalWrite(pinMOne, HIGH);
  113.       break;
  114.   }  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement