SHARE
TWEET

ServoTesterUltimate 1.0

szymonwalle Aug 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. Servo s;
  3. void setup() {
  4.   Serial.begin(115200);
  5.   Serial.println(F("//servotest//CR|LF//[a <deg>|s <mspdeg>]//"));
  6.   s.attach(9);
  7. }
  8. char buf[64];
  9. char tmp;
  10. uint8_t cur = 0;
  11. char state = 0;
  12. char tmpm;
  13. int opt = 0;
  14. int tmpopt;
  15. long dela = 0;
  16. long diff;
  17. int swstate = 0;
  18. int wrapval;
  19. void loop() {
  20.   while(Serial.available()) {
  21.     buf[cur] = Serial.read();
  22.     Serial.write(buf[cur]);
  23.     if (buf[cur] == 'X' || buf[cur] == '\n'  || buf[cur] == '\r') {
  24.       buf[cur] = '\0';
  25.       sscanf(buf, " %c %i ", &tmpm, &tmpopt);
  26.       switch (tmpm) {
  27.         case 'a':
  28.         state = 0;
  29.         opt = tmpopt;
  30.         Serial.println(F("\nANGLE OK"));
  31.         break;
  32.         case 's':
  33.         state = 1;
  34.         opt = tmpopt;
  35.         dela = millis();
  36.         wrapval = 360*opt;
  37.         Serial.println(F("\nSWEEP OK"));
  38.         break;
  39.         default:
  40.         Serial.println(F("\nOPCOD UNKN"));
  41.       }
  42.       cur = 0;
  43.     } else {
  44.       cur++;
  45.     }
  46.   }
  47.   if (state == 0) {
  48.     s.write(opt);
  49.   } else if (state == 1) {
  50.     diff = millis() - dela;
  51.     if (diff > wrapval) {
  52.       diff -= wrapval;
  53.       dela += wrapval;
  54.     }
  55.     swstate = diff / opt;
  56.     if (swstate > 180) {
  57.       swstate = 360-swstate;
  58.     }
  59.     s.write(swstate);
  60.   }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top