I'm trying to make a color sensor, which when senses the a color (like red) it plays a song. When I run the program and put a color in front of the TCS34725 color sensor, it senses the color, but doesn't play the song until I hit the reset button on the Arduino Genuino R3. I am also using the Serial MP3 player/module. If you can help me figure out a way for the song to play without the resat button that would be great.
Post your code
#include <Wire.h> //include the I2C library to communicate with the sensor
#include "Adafruit_TCS34725.h" //https://www.youtube.com/watch?v=dCnjwxkWZ-w
#include <SimpleSDAudio.h>
#include <SoftwareSerial.h>
#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;
#define CMD_PLAY_W_INDEX 0X03
#define CMD_SET_VOLUME 0X06
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_SINGLE_CYCLE 0X19
#define SINGLE_CYCLE_ON 0X00
#define SINGLE_CYCLE_OFF 0X01
#define CMD_PLAY_W_VOL 0X22
#define redpin 3 //pwm output for RED anode use 1K resistor
#define greenpin 5 //pwm output for GREEN anode use 2K resistor
#define bluepin 6 //pwm output for BLUE anode use 1K resistor
#define commonAnode false // set to false if using a common cathode LED
byte gammatable[256]; // our RGB -> eye-recognized gamma color
//Create an instance of the TCS34725 Sensor
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600); //Sart serial comms @ 9600 (you can change this)
Serial.println("Color View Test"); //Title info
if (tcs.begin()) { //if the sensor starts correctly
Serial.println("Found sensor"); //print the happy message
} else { //if the sensor starts incorrectly
Serial.println("No TCS34725 found ... check your connections");//print the not so happy message
while (1); // halt!
}
pinMode(redpin, OUTPUT); //set redpin for output
pinMode(greenpin, OUTPUT); //set greenpin for output
pinMode(bluepin, OUTPUT); //set bluepin for output
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Serial.println(gammatable[i]);
}
}
void loop() {
uint16_t clear, red, green, blue; //declare variables for the colors
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear); //read the sensor
tcs.setInterrupt(true); // turn off LED
Serial.print("C:\t"); Serial.print(clear); //print color values
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);
if(red > 1000) {
mySerial.begin(9600);
// delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
sendCommand(CMD_PLAY_W_VOL, 0X0F01);//play the first song with volume 15 class
Serial.print("color red");
// if(!SdPlay.setFile("UltraCat_-_02_-_Funk_the_Floor.mp3")); // music name file
}
if(blue > 400) {
mySerial.begin(9600);
// delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
sendCommand(CMD_PLAY_W_VOL, 0X0F01);//play the first song with volume 15 class
Serial.print("color red");
// if(!SdPlay.setFile("UltraCat_-_03_-_Space_Love_Attack")); // music name file
} // Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.print("\n Hello World");
Serial.println();
//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
analogWrite(redpin, gammatable[(int)r]); //light red led as per calculation
analogWrite(greenpin, gammatable[(int)g]); //light green led as per calculation
analogWrite(bluepin, gammatable[(int)b]); //light blue led as per calculation
}
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;
}
}
You didn't read this before posting a programming question did you ?
Otherwise your code would not have smileys in it, be partially in italics and very badly formatted
Sorry for the inconvenience, I have reposted the code in proper format.
mySerial.begin(9600);
Unusual to put this in loop().
Which MP3 player? Perhaps it has some other "start playing" pin that must be high or low?
#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6 //connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
If you are using pins 5 and 6 for your MP3 player, then you can't use them for LEDs as well
#define redpin 3 //pwm output for RED anode use 1K resistor
#define greenpin 5 //pwm output for GREEN anode use 2K resistor
#define bluepin 6 //pwm output for BLUE anode use 1K resistor
pinMode(redpin, OUTPUT); //set redpin for output
pinMode(greenpin, OUTPUT); //set greenpin for output
pinMode(bluepin, OUTPUT); //set bluepin for output
The .mp3 module that I am using is the Aideepen YX5300 UART Control MP3 Music Player Module for Arduino. The module was bought off of amazon.
Here's the Demo Code for the .mp3 Module:
/***********************************************************/
//Demo for the Serial MP3 Player by Catalex
//Hardware: Serial MP3 Player *1
// Touch Sensor *1 http://www.dx.com/p/323904
// Rotary Angle Sensor *1 http://www.dx.com/p/323737
//Board: Arduino UNO R3
//IDE: Arduino-1.0
//Function: In the process that the Rotation Angle Sensor is rotated from
// the 'Min' side to the 'Max' side, the volume is gradually greater.
// If you touch the Touch Sensor,it will play or pause.
//Store: http://www.aliexpress.com/store/1199788
// http://www.dx.com/
#include <SoftwareSerial.h>
#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
unsigned char playmode = 1;
#define PLAY 1
#define PAUSE 0
static int8_t Send_buf[8] = {0} ;
/************Command byte**************************/
#define CMD_NEXT_SONG 0X01
#define CMD_PREV_SONG 0X02
#define CMD_PLAY_W_INDEX 0X03
#define CMD_VOLUME_UP 0X04
#define CMD_VOLUME_DOWN 0X05
#define CMD_SET_VOLUME 0X06
#define CMD_SINGLE_CYCLE_PLAY 0X08
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_SLEEP_MODE 0X0A
#define CMD_WAKE_UP 0X0B
#define CMD_RESET 0X0C
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_PLAY_FOLDER_FILE 0X0F
#define CMD_STOP_PLAY 0X16
#define CMD_FOLDER_CYCLE 0X17
#define CMD_SET_SINGLE_CYCLE 0X19
#define SINGLE_CYCLE_ON 0X00
#define SINGLE_CYCLE_OFF 0X01
#define CMD_SET_DAC 0X1A
#define DAC_ON 0X00
#define DAC_OFF 0X01
#define CMD_PLAY_W_VOL 0X22
/*********************************************************************/
/*macro definitions of Rotary angle sensor and LED pin*/
#define ROTARY_ANGLE_SENSOR A0
#define ADC_REF 5//reference voltage of ADC is 5v
#define VCC 5 //the default value of VCC of the control interface is 5v
#define FULL_ANGLE 280//full value of the rotary angle is 280 degrees
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
delay(500);
attachInterrupt(0, playOrPause, RISING);//pin2 -> INT0, and the Touch Sensor
//is connected with pin2 of Arduino
sendCommand(CMD_SEL_DEV, DEV_TF);
delay(200);
sendCommand(CMD_PLAY_W_VOL, 0X0F01);
}
static int8_t pre_vol = 0x0f;
void loop()
{
int degrees;
degrees = getDegree();
int8_t volume;
/*The degrees is 0~280, should be converted to be 0~255 to control the*/
/*brightness of LED */
volume = map(degrees, 0, 280, 0, 30);
if(volume != pre_vol)
{
sendCommand(CMD_SET_VOLUME, volume);
pre_vol = volume;
}
delay(100);
}
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //
Send_buf[1] = 0xff; //
Send_buf[2] = 0x06; //
Send_buf[3] = command; //
Send_buf[4] = 0x00;//
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;
}
}
/********************************************************************************/
/*Function: Get the angle between the mark on the potentiometer cap and the starting position */
/*Parameter:-void */
/*Return: -int,the range of degrees is 0~280 */
int getDegree()
{
int sensor_value = analogRead(ROTARY_ANGLE_SENSOR);
float voltage;
voltage = (float)sensor_value*ADC_REF/1023;
float degrees = (voltage*FULL_ANGLE)/VCC;
return degrees;
}
/*Interrupt service routine*/
void playOrPause()
{
cli();
if(playmode == PLAY)
{
playmode = PAUSE;
sendCommand(CMD_PAUSE,0);
}
else
{
playmode = PLAY;
sendCommand(CMD_PLAY,0);
}
sei();
}