Advertisement
Guest User

rilevacolori2

a guest
Jan 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define S0 4
  2. #define S1 5
  3. #define S2 6
  4. #define S3 7
  5.  
  6. #define out 8
  7.  
  8. int pulse, R, G, B, toll, ColorR, ColorG, ColorB;
  9.  
  10. void setup() {
  11.  
  12.   for(int i = S0; i<= S3; i++){
  13.     pinMode(i,OUTPUT);
  14.   }
  15.  
  16.   pinMode(out, INPUT);
  17.  
  18.   //Scaling colore a 20%
  19.   digitalWrite(S0, HIGH);
  20.   digitalWrite(S1, LOW);
  21.  
  22.   //Comunicazione seriale
  23.   Serial.begin(9600);
  24. }
  25.  
  26. void loop() {
  27.   /* Combinazioni
  28.    *     S2   S3
  29.    * R: LOW  | LOW
  30.    * G: HIGH | HIGH
  31.    * B: LOW  | HIGH */
  32.  
  33.    //R
  34.    digitalWrite(S2, LOW);
  35.    digitalWrite(S3, LOW);
  36.    pulse = pulseIn(out, LOW); //frequenza
  37.    R = map(pulse,25,72,255,0);
  38.  
  39.    //G
  40.    digitalWrite(S2, HIGH);
  41.    digitalWrite(S3, HIGH);
  42.    pulse = pulseIn(out, LOW);
  43.    G = map(pulse,30,90,255,0);
  44.  
  45.    //B
  46.    digitalWrite(S2, LOW);
  47.    digitalWrite(S3, HIGH);
  48.    pulse = pulseIn(out, LOW);
  49.    R = map(pulse,25,70,255,0);
  50.  
  51.    Serial.print("R=");
  52.    Serial.print(R);
  53.    Serial.print("G=");
  54.    Serial.print(G);
  55.    Serial.print("B=");
  56.    Serial.println(B);
  57.  
  58.  
  59.    if(isGray(R,G,B) == true){
  60.     Serial.println("Gray");
  61.    }
  62.    else if (isYellow(R,G,B) == true){
  63.     Serial.println("Yellow");
  64.    }
  65.    else if (isRed(R,G,B) == true){
  66.     Serial.println("Red");
  67.    }
  68.    
  69. }
  70.  
  71.  
  72.  
  73. bool isGray(int R, int G, int B){
  74.   ColorR = 127;
  75.   ColorG = 127;
  76.   ColorB = 127;
  77.   bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  78.   return colorgray;
  79.  }
  80.  
  81. bool isYellow(int R, int G, int B){
  82.   ColorR = 252;
  83.   ColorG = 241;
  84.   ColorB = 0;
  85.   bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  86.   return colorgray;
  87.  }
  88.  
  89. bool isRed(int R, int G, int B){
  90.   ColorR = 245;
  91.   ColorG = 23;
  92.   ColorB = 34;
  93.   bool colorgray = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  94.   return colorgray;
  95.  }
  96.  
  97. bool isColor(int ColorR, int ColorG, int ColorB, int R, int G, int B){
  98.   toll = 10; //tolleranza
  99.  
  100.   if(R <= ColorR+toll && R>= ColorR-toll){
  101.     if(G <= ColorG+toll && G>= ColorG-toll){
  102.       if(B <= ColorB+toll && B>= ColorB-toll){
  103.       return true;
  104.       } else return false;
  105.     } else return false;
  106.   } else return false;
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement