TheVideoVolcano

WIP - Stepper Class Arduino Test

Oct 26th, 2020
1,146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TStepper{
  2.  
  3. int g_stepPin = 3;
  4. int g_dirPin = 4;
  5. int g_stepsPerRev = 200;
  6. int g_dirPinState = 1;
  7.  
  8. private:
  9.  
  10.   uint8_t getDirection(char* dirParam){
  11.  
  12.   if (dirParam=="clockwise"){
  13.     //dirPinState = 1;
  14.     return 1;
  15.   }else if (dirParam=="anticlockwise"){
  16.     return 0;
  17.     //dirPinState = 0;
  18.   }
  19. }
  20.  
  21. public:
  22.  
  23.   TStepper(int stepPin, int dirPin, int stepsPerRev){ //constructor
  24.     pinMode(stepPin,OUTPUT);
  25.     pinMode(dirPin,OUTPUT);
  26.     g_stepPin = stepPin;
  27.     g_dirPin = dirPin;
  28.     g_stepsPerRev = stepsPerRev;
  29.   }
  30.  
  31.  
  32.  
  33.   //1.8 deg per step calced with (360/200)
  34.  
  35.   void rotate(char* direction, int angle){
  36.  
  37.    // g_dirPinState = getDirection(direction);
  38.    
  39.     //digitalWrite(g_dirPin,g_dirPinState); //Changes the rotations direction
  40.     // Makes 400 pulses for making two full cycle rotation
  41.  
  42.   int angleToSteps = (360/g_stepsPerRev);
  43.   int stepMax = angle/angleToSteps;
  44.  
  45.     step(stepMax,g_dirPinState);
  46.     }
  47.  
  48.     void step(int stepCount, char* direction){
  49.  
  50.       g_dirPinState = getDirection(direction);
  51.  
  52.       digitalWrite(g_dirPin,g_dirPinState); //Changes the rotations direction
  53.      
  54.       for(int x = 0; x < stepCount; x++) {
  55.       digitalWrite(g_stepPin,HIGH);
  56.       delayMicroseconds(1000);
  57.       digitalWrite(g_stepPin,LOW);
  58.       delayMicroseconds(1000);
  59.      
  60.       }
  61.     }
  62. };
RAW Paste Data