color detector

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.2 kB
9
Indexable
Never
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
#define s0 11       //Module pins wiring
#define s1 12
#define s2 9
#define s3 10
#define out 8

int data,r,g,b;        //This is where we're going to stock our values
bool red,green,blue,white,black,yellow,cyan,magenta;
void setup() 
{
   pinMode(s0,OUTPUT);    //pin modes
   pinMode(s1,OUTPUT);
   pinMode(s2,OUTPUT);
   pinMode(s3,OUTPUT);
   pinMode(out,INPUT);
 lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("COLOUR DETECTOR");

   Serial.begin(9600);   //intialize the serial monitor baud rate
   
   digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended)
   digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is  2%
   
}

void loop()                  //Every 2s we select a photodiodes set and read its data
{

   digitalWrite(s2,LOW);        //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green
   digitalWrite(s3,LOW);
   Serial.print("Red value= "); 
   GetData();                   //Executing GetData function to get the value
r=data;
Serial.print(r); 
   digitalWrite(s2,LOW);
   digitalWrite(s3,HIGH);
   Serial.print(" | Blue value= ");
   GetData();
b=data;
Serial.print(b); 
   digitalWrite(s2,HIGH);
   digitalWrite(s3,HIGH);
   Serial.print(" | Green value= ");
   GetData();
g=data*0.65;
   Serial.print(g); 
      lcd.setCursor(0, 1);
if(r>36&b>28&g>22)   lcd.print("      BLACK     ");
else if(r>32&b>16&g>20)   lcd.print("      BLUE      ");
else if(r>24&b>25&g>10)   lcd.print("     GREEN      ");
else if(r>14&b>26&g>19)   lcd.print("      RED       ");
else if(r>14&b>22&g>11)   lcd.print("     YELLOW     ");
else if(r>14&b>14&g>9)   lcd.print("      WHITE     ");
else if(r>9&b>9&g>6)   lcd.print("   NO COLOUR    ");


   Serial.println();
   delay(500);
}

void GetData(){
   data=pulseIn(out,LOW);       //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again
            //The higher the frequency the lower the duration
   delay(20);
}