Advertisement
Guest User

Untitled

a guest
Apr 30th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1.  /*
  2.  * Skittles Color Sorter using Arduino UNO R3.
  3.  * S0, S1, S2, S3 - PINs used by the color sensor.
  4.  * Colours return IDs: 1 - red, 2 - green, 3 - yellow, 4 - orange, 5 - blue
  5.  */
  6.  
  7. #include <Servo.h>
  8.  
  9.  /*
  10.  * PIN-urile pentru senzorul de lumina.
  11.  */
  12. #define S0 3
  13. #define S1 4
  14. #define S2 5
  15. #define S3 6
  16.  
  17. #define sensorOut 7
  18.  
  19.  /*
  20.  * Coduri pentru culori.
  21.  */
  22. #define RED     1
  23. #define GREEN   2
  24. #define YELLOW  3
  25. #define ORANGE  4
  26. #define BLUE    5
  27.  
  28. #define START_POS   100 // 100°
  29. #define SENSOR_POS  50  // 65°
  30. #define END_POS     29  // 29°
  31.  
  32. #define DEBUG
  33.  
  34. Servo topServo;     // motor sus
  35. Servo bottomServo;  // motor jos
  36.  
  37. int frequency = 0, color = 0;
  38. int R, G, B;
  39.  
  40. void setup() {
  41.   pinMode(S0, OUTPUT);
  42.   pinMode(S1, OUTPUT);
  43.   pinMode(S2, OUTPUT);
  44.   pinMode(S3, OUTPUT);
  45.   pinMode(sensorOut, INPUT);
  46.  
  47.   // Setting frequency-scaling to 20%
  48.   digitalWrite(S0, HIGH);
  49.   digitalWrite(S1, LOW);
  50.  
  51.   topServo.attach(8);     // PIN #8
  52.   bottomServo.attach(9);  // PIN #9
  53.  
  54.   Serial.begin(9600);
  55. }
  56.  
  57. void loop() {
  58.   topServo.write(START_POS); // seteaza servomotorul sus la 115° (ia bomboana)
  59.   delay(500); // asteapta 500ms
  60.  
  61.   for (int i = START_POS; i >= SENSOR_POS; i--) { // muta motorul sub senzor
  62.     topServo.write(i); // 110 - 66° cu delay de 2ms intre fiecare °
  63.     delay(4); // 2ms
  64.   }
  65.  
  66.   delay(500); // 500ms
  67.  
  68.   getRGB(&R, &G, &B); // afla culoarea
  69.   color = getColor(R, G, B); // afla culoarea
  70.  
  71.   delay(10); // delay 10ms
  72.  
  73.   switch (color) {
  74.     case RED: {
  75.       //Serial.println("RED");
  76.       bottomServo.write(50); // 50°
  77.       break;
  78.     }
  79.     case GREEN: {
  80.       Serial.println("GREEN");
  81.       bottomServo.write(75); // 75°
  82.       break;
  83.     }
  84.     case YELLOW: {
  85.       Serial.println("YELLOW");
  86.       bottomServo.write(100); // 100°
  87.       break;
  88.     }
  89.     case ORANGE: {
  90.       Serial.println("ORANGE");
  91.       bottomServo.write(125); // 125°
  92.       break;
  93.     }
  94.     case BLUE: {
  95.       //Serial.println("BLUE");
  96.       bottomServo.write(150); // 150°
  97.       break;
  98.     }
  99.    
  100.     case 0: break; // invalid colour
  101.   }
  102.  
  103.   delay(300); // delay 300ms
  104.  
  105.   for (int i = SENSOR_POS; i >= END_POS; i--) {
  106.     topServo.write(i);
  107.     delay(4);
  108.   }
  109.  
  110.   delay(200); // 200ms
  111.  
  112.   for (int i = END_POS; i <= START_POS; i++) {
  113.     topServo.write(i); // revine la pozitia initiala de 115°
  114.     delay(4);
  115.   }
  116.  
  117.   color = 0; // reseteaza variabila de culoare
  118. }
  119.  
  120. void getRGB(int *R, int *G, int *B)
  121. {
  122.   // Setting red filtered photodiodes to be read
  123.   digitalWrite(S2, LOW);
  124.   digitalWrite(S3, LOW);
  125.  
  126.   frequency = pulseIn(sensorOut, LOW);
  127.   *R = frequency;
  128.   // frequency = map(frequency, 165, 180, 255, 0);
  129.  
  130.   #if defined DEBUG
  131.     Serial.print("R= ");//printing name
  132.     Serial.print(frequency);//printing RED color frequency
  133.     Serial.print("  ");
  134.   #endif
  135.   delay(100);
  136.  
  137.   // Setting Green filtered photodiodes to be read
  138.   digitalWrite(S2, HIGH);
  139.   digitalWrite(S3, HIGH);
  140.  
  141.   frequency = pulseIn(sensorOut, LOW);
  142.   *G = frequency;
  143.   // frequency = map(frequency, 195, 210, 255, 0);
  144.  
  145.   #if defined DEBUG
  146.     Serial.print("G= ");//printing name
  147.     Serial.print(frequency);//printing RED color frequency
  148.     Serial.print("  ");
  149.   #endif
  150.  
  151.   delay(100);
  152.  
  153.   digitalWrite(S2, LOW);
  154.   digitalWrite(S3, HIGH);
  155.  
  156.   frequency = pulseIn(sensorOut, LOW);
  157.   *B = frequency;
  158.   // frequency = map(frequency, 30, 90, 255, 0);
  159.  
  160.   #if defined DEBUG
  161.     Serial.print("B= ");//printing name
  162.     Serial.print(frequency);//printing RED color frequency
  163.     Serial.println("  ");
  164.   #endif
  165.   delay(100);
  166. }
  167.  
  168. int getColor(int R, int G, int B)
  169. {
  170.   if ((R <= 105 && R >= 90) && (G <= 128 && G >= 110) && (B >= 90 && B <= 105))
  171.     return RED; // RED
  172.  
  173.   else if ((R >= 85 && R <= 95) && (G >= 85 && G <= 100) && (B >= 83 && B <= 100))
  174.     return GREEN; // GREEN
  175.  
  176.   else if ((R >= 70 && R <= 80) && (G >= 85 && G <= 90) && (B >= 79 && B <= 90))
  177.     return YELLOW; // YELLOW
  178.  
  179.   else if ((R >= 70 && R <= 90) && (G >= 94 && G <= 114) && (B >= 82 && B <= 106))
  180.     return ORANGE; // ORANGE
  181.  
  182.   //return 0; // INVALID
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement