Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <VarSpeedServo.h>
  2. #include <SPI.h>
  3. #include <nRF24L01.h>
  4. #include <RF24.h>
  5. #include <Adafruit_TCS34725.h>
  6.  
  7. RF24 radio(9, 10);
  8. int data[4];
  9. int arr[4];
  10. /*
  11. VarSpeedServo myservo0;
  12. VarSpeedServo myservo1;
  13. VarSpeedServo myservo2;
  14. VarSpeedServo myservo3;
  15. */
  16. //--------- Magic --------------
  17. VarSpeedServo myServoArr[4];
  18. //------------------------------
  19. int pos0=90, pos1=90, pos2=90, pos3=50;
  20. int col4=0;
  21.  
  22. #define S0 4
  23. #define S1 5
  24. #define S2 6
  25. #define S3 7
  26. #define sensorOut 8
  27.  
  28. #define Speed 100
  29.  
  30. int redFrequency = 0;
  31. int greenFrequency = 0;
  32. int blueFrequency = 0;
  33.  
  34. int redColor = 0;
  35. int greenColor = 0;
  36. int blueColor = 0;
  37.  
  38. #define INTERVAL 10*1000
  39. long int prevMillisColor = 0;
  40. #define INTERVAL_SERIAL 500
  41. long int prevMillisSerial = 0;
  42.  
  43.  
  44. void setup() {
  45. //----------- More magic --------------
  46. // for (int i=0; i<4; i++) {
  47. // myServoArr[i].attach(i+3);
  48. // Тут проверь, чтобы он к корректным пинам подключался
  49. // а то я запамятовал куда что подключается
  50. //}
  51. //---------------------------------
  52. Serial.begin(9600);
  53. //Ниже не понятно почему у тебя 2 раза к 3 пину подключается
  54. myServoArr[0].attach(3);
  55. myServoArr[1].attach(A3);
  56. myServoArr[2].attach(A4);
  57. myServoArr[3].attach(A5);
  58.  
  59.  
  60. radio.begin();
  61. radio.setChannel(5);
  62. radio.setDataRate (RF24_1MBPS);
  63. radio.setPALevel (RF24_PA_HIGH);
  64. radio.openReadingPipe (1, 0x1234567890LL);
  65. radio.startListening ();
  66.  
  67. pinMode(S0, OUTPUT);
  68. pinMode(S1, OUTPUT);
  69. pinMode(S2, OUTPUT);
  70. pinMode(S3, OUTPUT);
  71.  
  72. pinMode(sensorOut, INPUT);
  73.  
  74. // Установка масштабирования частоты на 20%
  75. digitalWrite(S0,HIGH);
  76. digitalWrite(S1,LOW);
  77. }
  78.  
  79. void loop() {
  80.  
  81. //Процедура опроса датчика, выполняющаяся раз в INTERVAL миллисекунд
  82. if (millis()-prevMillisColor>INTERVAL) {
  83.  
  84. digitalWrite(S2,LOW);
  85. digitalWrite(S3,LOW);
  86.  
  87. redFrequency = pulseIn(sensorOut, LOW);
  88. redColor = map(redFrequency, 70, 120, 255,0);
  89. delay(30);
  90.  
  91. digitalWrite(S2,HIGH);
  92. digitalWrite(S3,HIGH);
  93.  
  94. greenFrequency = pulseIn(sensorOut, LOW);
  95. greenColor = map(greenFrequency, 100, 200, 255, 0);
  96. delay(30);
  97.  
  98. digitalWrite(S2,LOW);
  99. digitalWrite(S3,HIGH);
  100.  
  101. blueFrequency = pulseIn(sensorOut, LOW);
  102. blueColor = map(blueFrequency, 60, 100, 255, 0);
  103. delay(30);
  104.  
  105. if(redColor > greenColor && redColor > blueColor){
  106. Serial.println(" - RED detected!");
  107. col4 = 0;
  108. }
  109. if(greenColor > redColor && greenColor > blueColor){
  110. Serial.println(" - GREEN detected!");
  111. col4 = 0;
  112. }
  113. if(blueColor > redColor && blueColor > greenColor){
  114. Serial.println(" - BLUE detected!");
  115. col4 = 100;
  116. }
  117. prevMillisColor = millis();
  118. }
  119.  
  120.  
  121.  
  122. if (radio.available() > 0) {
  123. radio.read(&data, sizeof(data));
  124.  
  125. arr[0] = pos0;
  126. arr[1] = pos1;
  127. arr[2] = pos2;
  128. arr[3] = pos3;
  129.  
  130. if(millis()-prevMillisSerial>INTERVAL_SERIAL){
  131. Serial.print("X1: ");
  132. Serial.println(data[0]);
  133. Serial.print("Y1: ");
  134. Serial.println(data[1]);
  135. Serial.print("X2: ");
  136. Serial.println(data[2]);
  137. Serial.print("Y2: ");
  138. Serial.println(data[3]);
  139. Serial.println(col4);
  140. Serial.print("pos0: ");
  141. Serial.println(arr[0]);
  142. Serial.print("pos1: ");
  143. Serial.println(arr[1]);
  144. Serial.print("pos2: ");
  145. Serial.println(arr[2]);
  146. Serial.print("pos3: ");
  147. Serial.println(arr[3]);
  148. prevMillisSerial = millis();
  149. }
  150.  
  151. for(int i=0; i<4; i++){
  152. if (data[i]<300 && arr[i]>0){
  153. arr[i]--;
  154. myServoArr[i].slowmove (arr[i], Speed);
  155. }
  156. if (data[i]>600 && arr[i]<180){
  157. arr[i]++;
  158. myServoArr[i].slowmove (arr[i], Speed);
  159. }
  160. //delete [] myservo; //Ето вот в твоём случае делат не надо, можешь удалять
  161. }
  162. /*if (data[1]<300 && pos1>0){
  163. pos1-=2;
  164. myservo1.slowmove (pos1, Speed);
  165. //delay(300);
  166. }
  167. if (data[1]>600 && pos1<180){
  168. pos1+=2;
  169. myservo1.slowmove (pos1, Speed);
  170. //delay(300);
  171. }
  172. if (data[2]<300 && pos2>0){
  173. pos2-=2;
  174. myservo2.slowmove (pos2, Speed);
  175. //delay(300);
  176. }
  177. if (data[2]>600 && pos2<180){
  178. pos2+=2;
  179. myservo2.slowmove (pos2, Speed);
  180. //delay(300);
  181. }
  182. if (data[3]<300 && pos3>0 ){
  183. pos3--;
  184. myservo3.slowmove (pos3, Speed);
  185. //delay(300);
  186. }
  187. if (data[3]>600 && pos3<180 ){
  188. pos3++;
  189. myservo3.slowmove (pos3, Speed);
  190. //delay(300);
  191. }*/
  192. //col4=0;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement