Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <Servo.h> //inclui a biblioteca Servo.h
  2. Servo servo_1;
  3. Servo servo_2;
  4.  
  5. int minPulse_1     =  0;   // Posição (angular) mínima do servo
  6. int maxPulse_1     =  180; // Posição (angular) máxima do servo
  7. int turnRate_1     =  1;  // Incremento do servo (Maior valor = maior velocidade, Menor valor = Mais precisão)
  8.  
  9.  
  10. int minPulse_2     =  0;   // Posição (angular) mínima do servo
  11. int maxPulse_2     =  180; // Posição (angular) máxima do servo
  12. int turnRate_2     =  1;  // Incremento do servo (Maior valor = maior velocidade, Menor valor = Mais precisão)
  13.  
  14. int pulseWidth_1;            // Largura do Pulso do servo (PWM)
  15. int pulseWidth_2;        // Largura do Pulso do servo (PWM)
  16.  
  17. void setup() {
  18.   servo_1.attach(2); //Pino do arduino do Servo 1  (D2)
  19.   servo_2.attach(3); //Pino do arduino do Servo 2  (D3)      
  20.   Serial.begin(9600); // Abre conexão serial e define a taxa de transmissão em 9.600kbps  
  21. }
  22. void loop() {
  23.   if (Serial.available() > 0) { // verifica serial
  24.  
  25.     int data = Serial.read();     // lê o byte na serial
  26.     switch(data)
  27.     {
  28.     case 'a' :
  29.       {
  30.         pulseWidth_1 = pulseWidth_1 - turnRate_1;
  31.         break;
  32.       }
  33.     case 'q' :
  34.       {
  35.         pulseWidth_1 = pulseWidth_1 + turnRate_1;  
  36.         break ;
  37.       }
  38.     case 's' :
  39.       {
  40.         pulseWidth_2 = pulseWidth_2 - turnRate_2;
  41.         break;
  42.       }
  43.     case 'w' :
  44.       {
  45.         pulseWidth_2 = pulseWidth_2 + turnRate_2;
  46.         break ;
  47.       }
  48.     }      
  49.  
  50.     // Verifica os limites do servo 1
  51.     if (pulseWidth_1 > maxPulse_1) {
  52.       pulseWidth_1 = maxPulse_1;
  53.     }
  54.     if (pulseWidth_1 < minPulse_1) {
  55.       pulseWidth_1 = minPulse_1;
  56.     }
  57.  
  58.     // Verifica os limites do servo 2
  59.     if (pulseWidth_2 > maxPulse_2) {
  60.       pulseWidth_2 = maxPulse_2;
  61.     }
  62.     if (pulseWidth_2 < minPulse_2) {
  63.       pulseWidth_2 = minPulse_2;
  64.     }
  65.  
  66.     //envia o "comando" para os servos
  67.     servo_1.write(pulseWidth_1);
  68.     servo_2.write(pulseWidth_2);
  69.  
  70.     // imprime na serial os angulos dos servos
  71.     Serial.print(" Servo _1: ");
  72.     Serial.print(pulseWidth_1);
  73.     Serial.print("- Servo _2: ");
  74.     Serial.println(pulseWidth_2);
  75.   }
  76. }