RuiViana

Dois_Servos

Oct 29th, 2016
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <IRremote.h> //must copy IRremote library to arduino libraries
  2. #include <Servo.h>
  3. #define left1 0xE17AD827 //clockwise rotation button
  4. #define right1 0xE17A38C7 //counter clockwise rotation button
  5. #define left2 0xE17AD02F //clockwise rotation button
  6. #define right2 0xE17A30CF //counter clockwise rotation button
  7. int RECV_PIN = 2; //IR receiver pin
  8. Servo servo1;
  9. Servo servo2;
  10. int val1; //rotation angle
  11. int val2; //rotation angle
  12. bool cwRotation1, ccwRotation1; //the states of rotation
  13. bool cwRotation2, ccwRotation2; //the states of rotation
  14. IRrecv irrecv(RECV_PIN);
  15. decode_results results;
  16. //--------------------------
  17. void setup()
  18. {
  19.   Serial.begin(9600);
  20.   irrecv.enableIRIn(); // Start the receiver
  21.   servo1.attach(9); //servo pin
  22.   servo2.attach(10); //servo pin
  23. }
  24. //---------------------------
  25. void loop()
  26. {
  27.   if (irrecv.decode(&results))
  28.   {
  29.     Serial.println(results.value, HEX);
  30.     irrecv.resume(); // Receive the next value
  31.     motor1();
  32.     motor2();
  33.   }
  34. }
  35. //----------------------------
  36. void motor1()
  37. {
  38.   if (results.value == left1)
  39.   {
  40.     cwRotation1 = !cwRotation1; //toggle the rotation value
  41.     ccwRotation1 = false; //no rotation in this direction
  42.   }
  43.   if (results.value == right1)
  44.   {
  45.     ccwRotation1 = !ccwRotation1; //toggle the rotation value
  46.     cwRotation1 = false; //no rotation in this direction
  47.   }
  48.   if (cwRotation1 && (val1 != 175))
  49.   {
  50.     val1++; //for colockwise button
  51.   }
  52.   if (ccwRotation1 && (val1 != 0))
  53.   {
  54.     val1--; //for counter colockwise button
  55.   }
  56.   servo1.write(val1);
  57.   delay(20); //General speed
  58. }
  59. //----------------------------
  60. void motor2()
  61. {
  62.   if (results.value == left2)
  63.   {
  64.     cwRotation2 = !cwRotation2; //toggle the rotation value
  65.     ccwRotation2 = false; //no rotation in this direction
  66.   }
  67.   if (results.value == right2)
  68.   {
  69.     ccwRotation2 = !ccwRotation2; //toggle the rotation value
  70.     cwRotation2 = false; //no rotation in this direction
  71.   }
  72.   if (cwRotation2 && (val2 != 175))
  73.   {
  74.     val2++; //for colockwise button
  75.   }
  76.   if (ccwRotation2 && (val2 != 0))
  77.   {
  78.     val2--; //for counter colockwise button
  79.   }
  80.   servo2.write(val2);
  81.   delay(20); //General speed
  82. }
RAW Paste Data