Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <DFPlayer_Mini_Mp3.h>
- void printDetail(uint8_t type, int value);
- SoftwareSerial mySerial(10, 11); // RX, TX
- #define S0 4
- #define S1 5
- #define S2 7
- #define S3 6
- #define sensorOut 8
- int frequency = 0;
- int color = 0;
- void setup() {
- mySerial.begin (9600);
- mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
- mp3_set_volume (25);
- Serial.begin(115200);
- pinMode(S0, OUTPUT);
- pinMode(S1, OUTPUT);
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(sensorOut, INPUT);
- //Setting frequency-scaling
- digitalWrite(S0, HIGH);
- digitalWrite(S1, LOW);
- Serial.println();
- }
- void loop() {
- color = readColor();
- delay(10);
- switch (color) {
- case 1:
- Serial.println("\t\t\tRED detected!");
- // ./mp3/0001.mp3
- mp3_play (1);
- delay (3000);
- mp3_stop ();
- break;
- case 2:
- Serial.println("\t\t\tBLUE detected!");
- // ./mp3/0002.mp3
- mp3_play (2);
- delay (3000);
- mp3_stop ();
- break;
- case 3:
- Serial.println("\t\t\tGREEN detected!");
- // ./mp3/0003.mp3
- mp3_play (3);
- delay (3000);
- mp3_stop ();
- break;
- case 4:
- Serial.println("\t\t\tWHITE detected!");
- // ./mp3/0004.mp3
- mp3_play (4);
- delay (3000);
- mp3_stop ();
- break;
- case 5:
- Serial.println("\t\t\tORANGE detected!");
- // ./mp3/0005.mp3
- mp3_play (5);
- delay (3000);
- mp3_stop ();
- break;
- case 6:
- Serial.println("\t\t\tBLACK detected!");
- // ./mp3/0006.mp3
- mp3_play (6);
- delay (3000);
- mp3_stop ();
- break;
- case 7:
- Serial.println("\t\t\tYELLOW detected!");
- // ./mp3/0007.mp3
- mp3_play (7);
- delay (3000);
- mp3_stop ();
- break;
- case 8:
- Serial.println("\t\t\tPURPLE detected!");
- // ./mp3/0008.mp3
- mp3_play (8);
- delay (3000);
- mp3_stop ();
- break;
- case 0:
- 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<75 & R>50 && G < 250 && G > 110 && B < 160 && B > 90 ) {
- color = 1; // Red
- }
- else if (R<45 & R>30 && G < 40 && G > 25 && B < 30 && B > 10 ) {
- color = 2; // Blue
- }
- else if (R<45 & R>30 && G < 40 && G > 25 && B < 250 && B > 200 ) {
- color = 3; // Green
- }
- else if (R<35 & R>4 && G < 45 && G > 15 && B < 40 && B > 10 ) {
- color = 4; // WHITE
- }
- else if (R<40 & R>15 && G < 130 && G > 50 && B < 150 && B > 50 ) {
- color = 5; // ORANGE
- }
- else if (R<270 & R>240 && G < 300 && G > 270 && B < 210 && B > 190 ) {
- color = 6; // BLACK
- }
- else if (R<30 & R>15 && G < 41 && G > 28 && B < 60 && B > 42 ) {
- color = 7; // YELLOW
- }
- else if (R<150 & R>100 && G < 250 && G > 200 && B < 250 && B > 200 ) {
- color = 8; // PURPLE
- }
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment