Code to detect colour and play music

Hi everyone, I am currently doing a project to create an educational tool for blind children to learn about colours. I'm using Arduino Nano, TCS34725 and Mini MP3 Module. I would like to detect colour using the TCS34725, and play the respective colour name - if it detects red, the audio output will be 'red'. Does anybody know how my current code can be improved? As I'm unable to get it to have an audio output. Thank you very much!

#include "Wire.h"
#include "Adafruit_TCS34725.h"
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);

define Start_Byte 0x7E

define Version_Byte 0xFF

define Command_Length 0x06

define End_Byte 0xEF

define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X);

void setup() {

// Serielle Kommunikation zur Ausgabe der Wert im seriellen Monitor
Serial.begin(9600);
Serial.println("Makerblog.at - MuMs Color Sensor");
mySerial.begin (9600);
// Überprüfen, ob Color Sensor sich auch zurückmeldet
if (tcs.begin()) {
// Alles OK
Serial.println("Sensor found");
} else {
// Kein Sensor gefunden. Programm an dieser Stelle einfrieren
Serial.println("TCS34725 Sensor not found");
while (1); // Halt!
}

}

// loop() wird wiederholt, solange der Arduino läuft
void loop() {

// Der Sensor liefert Werte für R, G, B und einen Clear-Wert zurück
uint16_t clearcol, red, green, blue;
float average, r, g, b;
delay(200); // Farbmessung dauert c. 50ms
tcs.getRawData(&red, &green, &blue, &clearcol);

average = (red+green+blue)/3;

r = red/average;
g = green/average;
b = blue/average;

/*
Serial.print(r);
Serial.print(" ");
Serial.print(g);
Serial.print(" ");
Serial.print(b);
Serial.println(" ");
*/

// Colour detection

if ((r < 1.20) && (r > 0.80) && (g < 1.20) && (g > 1.00) && (b > 0.90) && (b < 1.05)) { //no colour
Serial.println("No color detected");
}

if ((r > 1.75) && (r < 1.85) && (g < 0.67) && (g > 0.60) && (b < 0.65) && (b > 0.58)) { //red
Serial.println("red");
}

else if ((r < 0.75) && (r > 0.65) && (g > 1.54) && (g < 1.60) && (b < 0.77) && (b > 0.70)) { //green
Serial.println("green");
}

else if ((r < 0.75) && (r > 0.66) && (g < 1.27) && (g > 1.22) && (b > 1.04) && (b < 1.10)) { //blue
Serial.println("blue");
}

else if ((r > 1) && (r < 1.06) && (g > 1.20) && (g < 1.26) && (b < 0.78) && (b > 0.70)) { //yellow
Serial.println("yellow");
}

else if ((r > 1.8) && (r < 1.94) && (g < 0.72) && (g > 0.65) && (b < 0.5) && (b > 0.42)) { //orange
Serial.println("orange");
}
else if ((r < 0.88) && (r > 0.80) && (g < 1.1) && (g > 1.02) && (b > 1.1) && (b < 1.16)) { //purple
Serial.println("purple");
}

else if ((r > 1.05) && (r < 1.12) && (g > 0.95) && (g > 0.98) && (b < 0.95) && (b > 0.88)) { //brown
Serial.println("brown");
}

else if ((r > 0.83) && (r < 0.88) && (g > 1.09) && (g < 1.16) && (b < 1.04) && (b > 0.98)) { //white
Serial.println("white");
}

else if ((r > 1.12) && (r < 1.19) && (g < 0.97) && (g > 0.91) && (b < 0.94) && (b > 0.87)) { //pink
Serial.println("pink");
}

else if ((r < 0.98) && (r > 0.90) && (g < 1.22) && (g > 1.10) && (b > 0.9) && (b < 1.05)) { //black
Serial.println("black");
}

else if ((r > 0.85) && (r < 0.95) && (g < 1.2) && (g > 1.08) && (b < 1.02) && (b > 0.95)) { //Gray
Serial.println("gray");
}

}

void play(int a){
for(int i = 0; i < a; i++){
playFirst();
playNext();
}
}

void playFirst()
{
execute_CMD(0x3F, 0, 0);

setVolume(20);

execute_CMD(0x11,0,1);

}

void pause()
{
execute_CMD(0x0E,0,0);
delay(500);
}

void play1()
{
execute_CMD(0x0D,0,1);
delay(500);
}

void playNext()
{
execute_CMD(0x01,0,1);
delay(500);
}

void playPrevious()
{
execute_CMD(0x02,0,1);
delay(500);
}

void setVolume(int volume)
{
execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

No, not possible to suggest changes to Your code at this stage.

Question, how does anyone know how to or if your current code can be improved if we cannot see your code?

Hi, my apologies for missing it out, I have added in the code

code tags? read about them use them. Please.

I can see no attempt to connect the colour detected with any of the playXX() routines. In fact the playXX() routines are never called anywhere in the program. So that explains why there is no audio output.

Steve

May I know if there is anything I can look up to work on this? Thank you very much Steve

I suggest instead

mySerial.write( Command_line, 10);

Will this help connect the colour input to audio output?

What we don't know so far... which "mini mp3 module"? There are loads. Are you getting the right messages on the Serial monitor for the different colours? I.e. is the colour sensor part working? Have you written a simple program or found an example that JUST plays some audio? That will tell you if you have the player connected correctly and if your tracks are properly set up on whatever card you are using.

If audio works then all you need to do is put in a play() command whenever a colour is detected e.g. immediately after the Serial.print(colourname).

Steve

Anything that will make the program less confusing will do that.