Playing different sounds with different colour

The sketch is very easy to understand and if you still think it's hard then you can read more about on all arduino posts page.

The sketch
#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_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22
#define COMMON_ANODE
int redPin = 9;
int greenPin = 10;
int bluePin = 11;

const int buttonPin1 = 8;
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 12;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
      mySerial.begin(9600);
        Serial.begin(9600);
      delay(500);//Wait chip initialization is complete
        sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
      delay(200);//wait for 200ms
}




void loop() {
buttonState1 = digitalRead(buttonPin1);
   if (buttonState1 == HIGH) {
setColor(0, 0, 255); // blue
sendCommand(CMD_PLAY_W_VOL, 0X0F01);//This is file you want to play.
      delay(700);//waiting time for the file above.
  } else {
setColor(0, 0, 0);
  }

buttonState2 = digitalRead(buttonPin2);
   if (buttonState2 == HIGH) {
setColor(0, 255, 0); // green
sendCommand(CMD_PLAY_W_VOL, 0X0F02);
      delay(700);
  } else {
setColor(0, 0, 0);
  }

buttonState3 = digitalRead(buttonPin3);
   if (buttonState3 == HIGH) {
 setColor(255, 0, 0); // red
sendCommand(CMD_PLAY_W_VOL, 0X0F03);
      delay(700);
  } else {
setColor(0, 0, 0);
  }

buttonState4 = digitalRead(buttonPin4);
   if (buttonState4 == HIGH) {
setColor(76, 4, 163); // dark violet
sendCommand(CMD_PLAY_W_VOL, 0X0F04);
      delay(700);
  } else {
setColor(0, 0, 0);
  }
 
buttonState5 = digitalRead(buttonPin5);
   if (buttonState5 == HIGH) {
setColor(255, 255, 0);// yellow
sendCommand(CMD_PLAY_W_VOL, 0X0F05);
      delay(700);
  } else {
setColor(0, 0, 0);
  }
}

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;//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]) ;
  }
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

No comments:

Post a Comment



Newly posted:
Reason why rich getting richer and poor getting poorer