FollowTV


  1. // FollowTV
  2. // pkvi
  3. #include "Servo.h"
  4. const int atpin = 2;
  5. const int aepin = 3;
  6. const int btpin = 4;
  7. const int bepin = 5;
  8. const int ctpin = 6;
  9. const int cepin = 7;
  10. // For Cycle
  11. const int num = 15;
  12. int i = 0;
  13. // Limit Distance
  14. const int limit = 5500;
  15. int hold = 500;
  16. long adur, atot, afix;
  17. long bdur, btot, bfix;
  18. long cdur, ctot, cfix;
  19. Servo lfs; // Left Front Speaker
  20. const int lfsout = 69;
  21. const int lfsin = 92;
  22. Servo mon; // Monitor
  23. const int monl = 145;
  24. const int monc = 91;
  25. const int monr = 45;
  26. Servo rfs; // Right Front Speaker
  27. const int rfsin = 63;
  28. const int rfsout = 78;
  29. void setup() {
  30. Serial.begin(9600);
  31. pinMode(atpin, OUTPUT);
  32. pinMode(aepin, INPUT);
  33. pinMode(btpin, OUTPUT);
  34. pinMode(bepin, INPUT);
  35. pinMode(ctpin, OUTPUT);
  36. pinMode(cepin, INPUT);
  37. lfs.attach(8);
  38. mon.attach(9);
  39. rfs.attach(10);
  40. digitalWrite(atpin, LOW);
  41. digitalWrite(btpin, LOW);
  42. digitalWrite(ctpin, LOW);
  43. lfs.write(lfsin);
  44. mon.write(monc);
  45. rfs.write(rfsin);
  46. }
  47. void loop() {
  48. atot = 0;
  49. for (i=0; i <= num; i++){
  50. digitalWrite(atpin, HIGH);
  51. delayMicroseconds(10);
  52. digitalWrite(atpin, LOW);
  53. adur = pulseIn(aepin, HIGH);
  54. if (adur > limit) {
  55. adur = limit;
  56. }
  57. atot = atot + adur;
  58. }
  59. afix = atot / num;
  60. btot = 0;
  61. for (i=0; i <= num; i++){
  62. digitalWrite(btpin, HIGH);
  63. delayMicroseconds(10);
  64. digitalWrite(btpin, LOW);
  65. bdur = pulseIn(bepin, HIGH);
  66. if (bdur > limit) {
  67. bdur = limit;
  68. }
  69. btot = btot + bdur;
  70. }
  71. bfix = btot / num;
  72. ctot = 0;
  73. for (i=0; i <= num; i++){
  74. digitalWrite(ctpin, HIGH);
  75. delayMicroseconds(10);
  76. digitalWrite(ctpin, LOW);
  77. cdur = pulseIn(cepin, HIGH);
  78. if (cdur > limit) {
  79. cdur = limit;
  80. }
  81. ctot = ctot + cdur;
  82. }
  83. cfix = ctot / num;
  84. Serial.print(afix);
  85. Serial.print(" ");
  86. Serial.print(bfix);
  87. Serial.print(" ");
  88. Serial.println(cfix);
  89. if (afix < bfix && afix < cfix) {
  90. // Position 2
  91. lfs.write(lfsout);
  92. mon.write(monl);
  93. rfs.write(rfsin);
  94. }
  95. else if (bfix < afix && bfix < cfix) {
  96. // Position 1 and 3
  97. lfs.write(lfsin);
  98. mon.write(monc);
  99. rfs.write(rfsin);
  100. }
  101. else if (cfix < afix && cfix < bfix) {
  102. // Position 4
  103. lfs.write(lfsin);
  104. mon.write(monr);
  105. rfs.write(rfsout);
  106. }
  107. else if (afix == limit && bfix == limit && cfix == limit) {
  108. // Position Open
  109. lfs.write(lfsout);
  110. mon.write(monc);
  111. rfs.write(rfsout);
  112. }
  113. delay(hold);
  114. }

Menu
Index
Engineering
Entertainment
Literature
Miscellaneous
Contact
Search
tiktok.com/@pkvi.xyz
Why Ayh?
Miter
Miter
@pkvi
"...may not meet professional standards."
2,363 miters
123 tenons
Subscribe
0.02354