Advertisement
lllClusterlll

Arduino - Control Servo 3 State

Feb 29th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <CytronServoShield.h>
  3.  
  4. SoftwareSerial mySerial(10, 11); // ON_BOARD_TX, ON_BOARD_RX
  5. CytronServoShield servo;
  6.  
  7. int STATE = 0;  // 0, 1, 2  => 0 deg, 90 deg, 180 deg
  8.  
  9. void setup()
  10. {
  11.  
  12.   pinMode(8, INPUT);
  13.  
  14.   Serial.begin(115200);
  15.   mySerial.begin(9600);
  16.   servo.init(&mySerial);
  17.  
  18.   servo.setStartAngle(ALL_CHANNELS, 0);
  19.   servo.setChannel(ALL_CHANNELS, OFF);
  20.   servo.setChannel(CHANNEL_1, ON);
  21.  
  22.   Serial.println("Init angle");
  23.   servo.angle(CHANNEL_1, 0);
  24.   while (servo.getAngle(CHANNEL_1) > 5)
  25.   {
  26.     delay(100);
  27.   }
  28.   delay(1000);
  29.  
  30. }
  31.  
  32. void loop()
  33. {
  34.  
  35.   int sw  = digitalRead(8);
  36.   if (sw == LOW)
  37.   {
  38.  
  39.     servo.angle(CHANNEL_1, STATE * 90);
  40.  
  41.     STATE++;
  42.     if(STATE > 2)
  43.     {
  44.       STATE = 0;
  45.     }
  46.  
  47.     delay(1000);
  48.  
  49.   }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement