Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- class XServo {
- Servo xServo; // the servo
- int X_pos; // current servo position
- int X_increment; // increment to move for each interval
- int X_updateInterval; // interval between updates
- unsigned long X_lastUpdate; // last update of position
- int X_pin_CLK = 2;
- int X_pin_DT = 3;
- int X_pin_SW = 4;
- int X_position = 0;
- int X_state;
- int X_state_CLK;
- int X_state_SW;
- public:
- XServo(int X_interval)
- {
- X_updateInterval = X_interval;
- X_increment = 1;
- }
- void Attach(int pin)
- {
- xServo.attach(pin);
- }
- void Detach()
- {
- xServo.detach();
- }
- void Update()
- {
- if((millis() - X_lastUpdate) > X_updateInterval) // time to update
- {
- X_lastUpdate = millis();
- // do something
- X_state_CLK = digitalRead(X_pin_CLK);
- if (X_state_CLK != X_state) {
- if (digitalRead(X_pin_DT) != X_state_CLK) {
- Serial.print("X-Rotace vpravo => | ");
- X_position ++;
- X_position ++;
- if (X_position > 180) {
- X_position = 180;
- }
- } else {
- Serial.print("X-Rotace vlevo <= | ");
- X_position --;
- X_position --;
- if (X_position < 0) {
- X_position = 0;
- }
- }
- Serial.print("X-Pozice enkoderu: ");
- Serial.println(X_position);
- }
- X_state = X_state_CLK;
- xServo.write(X_position);
- //
- }
- }
- };
- class YServo {
- Servo yServo; // the servo
- int Y_pos; // current servo position
- int Y_increment; // increment to move for each interval
- int Y_updateInterval; // interval between updates
- unsigned long Y_lastUpdate; // last update of position
- int Y_pin_CLK = 5;
- int Y_pin_DT = 6;
- int Y_pin_SW = 7;
- int Y_position = 0;
- int Y_state;
- int Y_state_CLK;
- int Y_state_SW;
- public:
- YServo(int Y_interval)
- {
- Y_updateInterval = Y_interval;
- Y_increment = 1;
- }
- void Attach(int pin)
- {
- yServo.attach(pin);
- }
- void Detach()
- {
- yServo.detach();
- }
- void Update()
- {
- if((millis() - Y_lastUpdate) > Y_updateInterval) // time to update
- {
- Y_lastUpdate = millis();
- // do something
- Y_state_CLK = digitalRead(Y_pin_CLK);
- if (Y_state_CLK != Y_state) {
- if (digitalRead(Y_pin_DT) != Y_state_CLK) {
- Serial.print("Y-Rotace vpravo => | ");
- Y_position ++;
- Y_position ++;
- if (Y_position > 180) {
- Y_position = 180;
- }
- } else {
- Serial.print("Y-Rotace vlevo <= | ");
- Y_position --;
- Y_position --;
- if (Y_position < 0) {
- Y_position = 0;
- }
- }
- Serial.print("Y-Pozice enkoderu: ");
- Serial.println(Y_position);
- }
- Y_state = Y_state_CLK;
- yServo.write(Y_position);
- //
- }
- }
- };
- XServo xServo(15);
- YServo yServo(15);
- void setup()
- {
- Serial.begin(9600);
- xServo.Attach(9);
- yServo.Attach(10);
- }
- void loop()
- {
- xServo.Update();
- yServo.Update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement