Advertisement
davidevb6

Untitled

Oct 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #define S0 4
  2. #define S1 5
  3. #define S2 6
  4. #define S3 7
  5. #define sensorOut 8
  6.  
  7. int led = 13;
  8.  
  9.  
  10. int R = 0;
  11. int G = 0;
  12. int B = 0;
  13. // QUI I VALORI DIPENDONO ANCHE DALL'ILLUMINAZIONE AMBIENTALE
  14. int red_valore_minimo = 25;
  15. int red_valore_massimo = 72;
  16. int green_valore_minimo = 30;
  17. int green_valore_massimo = 90;
  18. int blue_valore_minimo = 25;
  19. int blue_valore_massimo = 70;
  20.  
  21.  
  22. void setup() {
  23.   pinMode(S0, OUTPUT);
  24.   pinMode(S1, OUTPUT);
  25.   pinMode(S2, OUTPUT);
  26.   pinMode(S3, OUTPUT);
  27.   pinMode(sensorOut, INPUT);
  28.   pinMode(led, OUTPUT);
  29.   // SCALI LA FREQUENZA DEGLI IMPOLSI DI USCITA DAL SENSORE AL 20%
  30.   digitalWrite(S0, HIGH);
  31.   digitalWrite(S1, LOW);
  32.   Serial.begin(9600);
  33. }
  34.  
  35. void loop() {
  36.   int color;
  37.  
  38.   delay(1000);
  39.   R = readColor('r');
  40.   delay(50);
  41.   G = readColor('g');
  42.   delay(50);
  43.   B = readColor('b');
  44.   delay(50);
  45.  
  46.   if(R<45 & R>32 & G<65 & G>55) {
  47.     color = 1; // Red
  48.   }
  49.   else if(G<55 & G>43 & B<47 &B>35) {
  50.     color = 2; // Orange
  51.   }
  52.   else if(R<53 & R>40 & G<53 & G>40) {
  53.     color = 3; // Green
  54.   }
  55.   else if(R<38 & R>24 & G<44 & G>30) {
  56.     color = 4; // Yellow
  57.     }
  58.   else if(R<56 & R>46 & G<65 & G>55) {
  59.     color = 5; // Brown
  60.     }
  61.   else if(G<58 & G>45 & B<40 &B>26) {
  62.     color = 6; // Blue
  63.     }
  64.   else{
  65.     color = -1;
  66.   }
  67.   Serial.println(color);
  68.   if (color > 0){
  69.     accendi_led();
  70.   }
  71.   else{
  72.     spegni_led();
  73.   }
  74. }
  75.  
  76. void accendi_led(){
  77.   digitalWrite(led,HIGH);
  78. }
  79.  
  80. void spegni_led(){
  81.   digitalWrite(led,LOW);
  82. }
  83.  
  84. int readColor(char colore_da_testare) {
  85.   int frequency = 0;
  86.  
  87.   frequency = pulseIn(sensorOut, LOW);
  88.  
  89.   if (colore_da_testare == 'r'){
  90.     digitalWrite(S2, LOW);
  91.     digitalWrite(S3, LOW);
  92.     // RIMAPPO I VALORI PER POTERLI POI USARE COME VALORI RGB
  93.     frequency = map(frequency,red_valore_minimo,red_valore_massimo,0,255);
  94.   }
  95.   else if (colore_da_testare == 'g'){
  96.     digitalWrite(S2, HIGH);
  97.     digitalWrite(S3, HIGH);
  98.     // RIMAPPO I VALORI PER POTERLI POI USARE COME VALORI RGB
  99.     frequency = map(frequency,green_valore_minimo,green_valore_massimo,0,255);
  100.   }
  101.   else if (colore_da_testare == 'b'){
  102.     digitalWrite(S2, LOW);
  103.     digitalWrite(S3, HIGH);
  104.     // RIMAPPO I VALORI PER POTERLI POI USARE COME VALORI RGB
  105.     frequency = map(frequency,blue_valore_minimo,blue_valore_massimo,0,255);
  106.   }
  107.  
  108.   //Serial.print(colore_da_testare);
  109.   //Serial.print(" ");
  110.   //Serial.println(frequency);
  111.  
  112.  
  113.   return frequency;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement