Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- #define S0 4
- #define S1 5
- #define S2 7
- #define S3 6
- #define sensorOut 8
- int frequency = 0;
- int color = 0;
- void setup() {
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Scanning: ...");
- delay(5000);
- Serial.begin(115200);
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(sensorOut, INPUT);
- digitalWrite(S0, HIGH);
- digitalWrite(S1, LOW);
- }
- void loop() {
- color = readColor();
- delay(10);
- switch (color) {
- case 1:
- Serial.println("\t\t\tRED detected!");
- lcd.setCursor(0, 1);
- // "****************"
- lcd.print("RED detected ");
- break;
- case 2:
- Serial.println("\t\t\tBLUE detected!");
- lcd.setCursor(0, 1);
- // "****************"
- lcd.print("BLUE detected ");
- break;
- case 3:
- Serial.println("\t\t\tGREEN detected!");
- lcd.setCursor(0, 1);
- // "****************"
- lcd.print("GREEN detected ");
- break;
- case 4:
- Serial.println("\t\t\tYELLOW detected!");
- lcd.setCursor(0, 1);
- // "****************"
- lcd.print("YELLOW detected ");
- break;
- case 0:
- lcd.setCursor(0, 1);
- // "****************"
- lcd.print(" ");
- break;
- }
- color = 0;
- }
- //Read-Color Function
- int readColor() {
- ////////////////////////////////////////////////////////
- //Setting red filtered photodiodes to be read
- digitalWrite(S2, LOW);
- digitalWrite(S3, LOW);
- //Reading the output frequency
- frequency = pulseIn(sensorOut, LOW);
- int R = frequency;
- //Printing the value on the serial monitor
- Serial.print("R= "); //printing name
- Serial.print(frequency); //printing RED color frequency
- Serial.print(" ");
- delay(50);
- ////////////////////////////////////////////////////////
- //Setting Green filtered photodiodes to be read
- digitalWrite(S2, HIGH);
- digitalWrite(S3, HIGH);
- //Reading the output frequency
- frequency = pulseIn(sensorOut, LOW);
- int G = frequency;
- //Printing the value on the serial monitor
- Serial.print("G= "); //printing name
- Serial.print(frequency); //printing RED color frequency
- Serial.print(" ");
- delay(50);
- ////////////////////////////////////////////////////////
- //Setting Blue filtered photodiodes to be read
- digitalWrite(S2, LOW);
- digitalWrite(S3, HIGH);
- //Reading the output frequency
- frequency = pulseIn(sensorOut, LOW);
- int B = frequency;
- //Printing the value on the serial monitor
- Serial.print("B= "); //printing name
- Serial.print(frequency); //printing RED color frequency
- Serial.println(" ");
- delay(50);
- ////////////////////////////////////////////////////////
- if (R < 60 && R > 20 && G < 150 && G > 40 && B < 150 && B > 40 ) {
- color = 1; // Red
- }
- else if (R < 150 && R > 20 && G < 150 && G > 20 && B < 20 && B > 10 ) {
- color = 2; // Blue
- }
- else if (R < 70 && R > 30 && G < 40 && G > 10 && B < 70 && B > 30 ) {
- color = 3; // Green
- }
- else if (R < 20 && R > 10 && G < 40 && G > 20 && B < 50 && B > 20 ) {
- color = 4; // YELLOW
- }
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment