Advertisement
AndyGrawell

C++ issue #001

Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. class XServo {
  4.   Servo xServo;              // the servo
  5.   int X_pos;              // current servo position
  6.   int X_increment;        // increment to move for each interval
  7.   int  X_updateInterval;      // interval between updates
  8.   unsigned long X_lastUpdate; // last update of position
  9.  
  10.   int X_pin_CLK = 2;
  11.   int X_pin_DT  = 3;
  12.   int X_pin_SW  = 4;
  13.  
  14.   int X_position = 0;
  15.   int X_state;
  16.   int X_state_CLK;
  17.   int X_state_SW;
  18.  
  19. public:
  20.   XServo(int X_interval)
  21.   {
  22.     X_updateInterval = X_interval;
  23.     X_increment = 1;
  24.   }
  25.  
  26.   void Attach(int pin)
  27.   {
  28.     xServo.attach(pin);
  29.   }
  30.  
  31.   void Detach()
  32.   {
  33.     xServo.detach();
  34.   }
  35.  
  36.   void Update()
  37.   {
  38.     if((millis() - X_lastUpdate) > X_updateInterval)  // time to update
  39.     {
  40.       X_lastUpdate = millis();
  41.       // do something
  42.       X_state_CLK = digitalRead(X_pin_CLK);
  43.       if (X_state_CLK != X_state) {
  44.         if (digitalRead(X_pin_DT) != X_state_CLK) {
  45.           Serial.print("X-Rotace vpravo => | ");
  46.      
  47.           X_position ++;
  48.           X_position ++;
  49.           if (X_position > 180) {
  50.             X_position = 180;
  51.           }
  52.      
  53.         } else {
  54.           Serial.print("X-Rotace vlevo  <= | ");
  55.      
  56.           X_position --;
  57.           X_position --;
  58.           if (X_position < 0) {
  59.             X_position = 0;
  60.           }
  61.         }
  62.         Serial.print("X-Pozice enkoderu: ");
  63.         Serial.println(X_position);
  64.       }
  65.       X_state = X_state_CLK;
  66.       xServo.write(X_position);
  67.       //
  68.     }
  69.   }
  70. };
  71.  
  72. class YServo {
  73.   Servo yServo;              // the servo
  74.   int Y_pos;              // current servo position
  75.   int Y_increment;        // increment to move for each interval
  76.   int  Y_updateInterval;      // interval between updates
  77.   unsigned long Y_lastUpdate; // last update of position
  78.  
  79.   int Y_pin_CLK = 5;
  80.   int Y_pin_DT  = 6;
  81.   int Y_pin_SW  = 7;
  82.  
  83.   int Y_position = 0;
  84.   int Y_state;
  85.   int Y_state_CLK;
  86.   int Y_state_SW;
  87.  
  88. public:
  89.   YServo(int Y_interval)
  90.   {
  91.     Y_updateInterval = Y_interval;
  92.     Y_increment = 1;
  93.   }
  94.  
  95.   void Attach(int pin)
  96.   {
  97.     yServo.attach(pin);
  98.   }
  99.  
  100.   void Detach()
  101.   {
  102.     yServo.detach();
  103.   }
  104.  
  105.   void Update()
  106.   {
  107.     if((millis() - Y_lastUpdate) > Y_updateInterval)  // time to update
  108.     {
  109.       Y_lastUpdate = millis();
  110.       // do something
  111.       Y_state_CLK = digitalRead(Y_pin_CLK);
  112.       if (Y_state_CLK != Y_state) {
  113.         if (digitalRead(Y_pin_DT) != Y_state_CLK) {
  114.           Serial.print("Y-Rotace vpravo => | ");
  115.      
  116.           Y_position ++;
  117.           Y_position ++;
  118.           if (Y_position > 180) {
  119.             Y_position = 180;
  120.           }
  121.      
  122.         } else {
  123.           Serial.print("Y-Rotace vlevo  <= | ");
  124.      
  125.           Y_position --;
  126.           Y_position --;
  127.           if (Y_position < 0) {
  128.             Y_position = 0;
  129.           }
  130.         }
  131.         Serial.print("Y-Pozice enkoderu: ");
  132.         Serial.println(Y_position);
  133.       }
  134.       Y_state = Y_state_CLK;
  135.       yServo.write(Y_position);
  136.       //
  137.     }
  138.   }
  139. };
  140.  
  141. XServo xServo(15);
  142. YServo yServo(15);
  143.  
  144. void setup()
  145. {
  146.   Serial.begin(9600);
  147.   xServo.Attach(9);
  148.   yServo.Attach(10);
  149. }
  150.  
  151.  
  152. void loop()
  153. {
  154.   xServo.Update();
  155.   yServo.Update();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement