tuamigo

Arduino servo

Sep 3rd, 2012
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1.  
  2. /*
  3.   Control de servo, codigo base sin biblioteca
  4.  
  5.   Lee numeros del 0 al 9 desde el monitor serial siendo
  6.   0: 0 grados y 9: 180 grados y coloca el servo en la
  7.   posicion requerida.
  8.  
  9.   Creado el 03/09/2012
  10.   autor: 4m1g0
  11.  
  12. */
  13.  
  14. const int servo = 9;
  15. const int reloadTime = 30;
  16. unsigned int lastPulse;
  17. int value = 0;
  18.  
  19. void setup() {
  20.   pinMode(servo, OUTPUT);
  21.   Serial.begin(9600);
  22. }
  23.  
  24. void loop() {
  25.   if (Serial.available() > 0) {
  26.     value = Serial.read();
  27.     value = value -48;
  28.     Serial.print("He recibido: ");
  29.     Serial.println(value, DEC);
  30.   }
  31.  
  32.  
  33.   // control del servo
  34.   if (millis() - lastPulse >= reloadTime)
  35.   {
  36.     Sgiro(value);
  37.     lastPulse = millis();
  38.   }
  39.   delay(2);
  40. }
  41.  
  42. void Sgiro(int value)
  43. // Coloca al servo en el agulo dado entre 0 y 180 grados
  44. {
  45.   int pulseWidth = map(value, 0, 9, 500, 2100);
  46.   digitalWrite(servo, HIGH);
  47.   delayMicroseconds(pulseWidth);
  48.   digitalWrite(servo, LOW);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment