SallatielFernandes

servo-sem-lib

Jul 1st, 2022 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define servoPin 9
  2. #define t_periodo 20000
  3. #define t_min 900
  4. #define t_max 2300
  5.  
  6.  
  7. void setup() {
  8.   pinMode(servoPin, OUTPUT);
  9. }
  10.  
  11. void loop() {
  12.    // Pulso a cada 20ms
  13.  
  14.    // INCREMENTO
  15.    for(int i=0;i<=180;i+=1){
  16.    
  17.     digitalWrite(servoPin, HIGH);
  18.     delayMicroseconds(t_min + (i*((t_max-t_min)/180))); // tempo em nível alto
  19.    
  20.     digitalWrite(servoPin, LOW);
  21.     delayMicroseconds(t_periodo - (t_min + (i*((t_max-t_min)/180))));
  22.  
  23.     delay(20);
  24.    }
  25.  
  26.    // DECREMENTO
  27.    for(int i=180;i>0;i-=1){
  28.    
  29.     digitalWrite(servoPin, HIGH);
  30.     delayMicroseconds(t_min + (i*((t_max-t_min)/180))); // tempo em nível alto
  31.    
  32.     digitalWrite(servoPin, LOW);
  33.     delayMicroseconds(t_periodo - (t_min + (i*((t_max-t_min)/180))));
  34.  
  35.     delay(20);
  36.    }
  37.    
  38. }
Add Comment
Please, Sign In to add comment