#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
#define S0 4
#define S1 5
#define S2 7
#define S3 6
#define sensorOut 8
int frequency = 0;
int color=0;
void setup() {
mySoftwareSerial.begin(9600);
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();
Serial.println(F("Initializing DFPlayer..."));
//Use softwareSerial to communicate with MP3
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
//Set volume value (From 0 to 30)
myDFPlayer.volume(30);
}
void loop() {
color = readColor();
delay(10);
switch (color) {
case 1:
Serial.println("RED detected!");
myDFPlayer.play(1);
break;
case 2:
Serial.println("BLUE detected!");
myDFPlayer.play(2);
break;
case 3:
Serial.println("GREEN detected!");
myDFPlayer.play(3);
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<260 & R>230 & G<860 & G>800){
color = 1; // Red
}
if(G<420 & G>370 & B<350 & B>305){
color = 2; // Blue
}
if(R<450 & R>420 & G<420 & G>390){
color = 3; // Green
}
return color;
}
Comments