Advertisement
safwan092

Untitled

Dec 19th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. //Ingeimaks
  2. #include <Servo.h>
  3. //definiamo i servomotori orizzontale e verticale
  4. Servo servohori;
  5. int servoh = 0;
  6. int servohLimitHigh = 160;
  7. int servohLimitLow = 60;
  8.  
  9. Servo servoverti;
  10. int servov = 0;
  11. int servovLimitHigh = 160;
  12. int servovLimitLow = 60;
  13. //Pin fotoresistenze
  14. int ldrtopl = 2;//1;//2; //top left
  15. int ldrtopr = 1;//2;//1; //top right
  16. int ldrbotl = 4;//3;//3; // bottom left
  17. int ldrbotr = 3;//4;//0; // bottom right
  18.  
  19. void setup ()
  20. {
  21. servohori.attach(9);
  22. servohori.write(60);
  23. servoverti.attach(10);
  24. servoverti.write(60);
  25. Serial.begin(9600);
  26. delay(500);
  27.  
  28. }
  29.  
  30. void loop()
  31. {
  32. /*
  33. servoverti.write(60);
  34. servohori.write(60);
  35. delay(5000);
  36. servoverti.write(160);
  37. servohori.write(160);
  38. delay(5000);
  39. }*/
  40.  
  41. servoh = servohori.read();
  42. servov = servoverti.read();
  43. //Valore Analogico delle fotoresistenza
  44. int topl = analogRead(ldrtopl);
  45. int topr = analogRead(ldrtopr);
  46. int botl = analogRead(ldrbotl);
  47. int botr = analogRead(ldrbotr);
  48. Serial.println("---------------");
  49. Serial.print(topl);
  50. Serial.print(" ");
  51. Serial.print(topr);
  52. Serial.println(" ");
  53. Serial.print(botl);
  54. Serial.print(" ");
  55. Serial.print(botr);
  56. Serial.println(" ");
  57. Serial.println("***************");
  58. // Calcoliamo una Media
  59. int avgtop = (topl + topr) ; //average of top
  60. int avgbot = (botl + botr) ; //average of bottom
  61. int avgleft = (topl + botl) ; //average of left
  62. int avgright = (topr + botr) ; //average of right
  63.  
  64. if (avgtop < avgbot)
  65. {
  66. servoverti.write(servov + 1);
  67. if (servov > servovLimitHigh)
  68. {
  69. servov = servovLimitHigh;
  70. }
  71. delay(10);
  72. }
  73. else if (avgbot < avgtop)
  74. {
  75. servoverti.write(servov - 1);
  76. if (servov < servovLimitLow)
  77. {
  78. servov = servovLimitLow;
  79. }
  80. delay(10);
  81. }
  82. else
  83. {
  84. servoverti.write(servov);
  85. }
  86.  
  87. if (avgleft > avgright)
  88. {
  89. servohori.write(servoh + 1);
  90. if (servoh > servohLimitHigh)
  91. {
  92. servoh = servohLimitHigh;
  93. }
  94. delay(10);
  95. }
  96. else if (avgright > avgleft)
  97. {
  98. servohori.write(servoh - 1);
  99. if (servoh < servohLimitLow)
  100. {
  101. servoh = servohLimitLow;
  102. }
  103. delay(10);
  104. }
  105. else
  106. {
  107. servohori.write(servoh);
  108. }
  109. delay(50);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement