My group is having a project to make a lamp. Not really a lamp as we'll only be using common cathode rgb led. We used a button to turn the led on/off then another button to change its color.
This is the schematics:
This is the sketch:
const int buttonOO = 3;
const int buttonCC = 2;
const int ledR = 9;
const int ledG = 11;
const int ledB = 10;
//int counterOO = 0;
//int counterCC = 0;
int mode = 0;
byte lastButtonStateOO = LOW;
byte ledState = LOW;
void setup()
{
pinMode(buttonOO, INPUT);
pinMode(buttonCC, INPUT);
//pinMode(buttonCC, INPUT_PULLUP);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
Serial.begin(9600);
}
void loop() {
//turn on-off
byte buttonState = digitalRead(buttonOO);
if (buttonState != lastButtonStateOO) {
lastButtonStateOO = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(ledR, ledState);
digitalWrite(ledG, ledState);
digitalWrite(ledB, ledState);
}
}
> //change color
if(digitalRead(buttonCC) == LOW){
mode = mode + 1;
delay(400);
}
// red
if(mode == 1 && ledState == HIGH){
digitalWrite(ledR, 255);
digitalWrite(ledG, 0);
digitalWrite(ledB, 0);
}
// orange
if(mode == 2 && ledState == HIGH){
digitalWrite(ledR, 255);
digitalWrite(ledG, 75);
digitalWrite(ledB, 0);
}
// yellow
if(mode == 3 && ledState == HIGH){
digitalWrite(ledR, 255);
digitalWrite(ledG, 220);
digitalWrite(ledB, 0);
}
// green
if(mode == 4 && ledState == HIGH){
digitalWrite(ledR, 0);
digitalWrite(ledG, 255);
digitalWrite(ledB, 0);
}
// blue
if(mode == 5 && ledState == HIGH){
digitalWrite(ledR, 0);
digitalWrite(ledG, 0);
digitalWrite(ledB, 255);
}
// cyan
if(mode == 6 && ledState == HIGH){
digitalWrite(ledR, 0);
digitalWrite(ledG, 100);
digitalWrite(ledB, 100);
}
// violet
if(mode == 7 && ledState == HIGH){
digitalWrite(ledR, 100);
digitalWrite(ledG, 0);
digitalWrite(ledB, 255);
}
// pink
if(mode == 8 && ledState == HIGH){
digitalWrite(ledR, 255);
digitalWrite(ledG, 192);
digitalWrite(ledB, 203);
}
// white
if(mode == 9 && ledState == HIGH){
digitalWrite(ledR, 255);
digitalWrite(ledG, 255);
digitalWrite(ledB, 255);
}
if(mode == 10 && ledState == HIGH){
mode = 1;
}
else if(mode > 0 && ledState == LOW){
mode = 9;
}
// turn off
// if(mode > 0 && lastButtonStateOO == HIGH){
// digitalWrite(ledR, 0);
// digitalWrite(ledG, 0);
// digitalWrite(ledB, 0);
// }
// serial print
Serial.println(buttonState);
}
(sorry if it's messy)
We're done with programming the led, like turning it off/on and changing color, but we're having a problem with understanding the sketches for mp3 player and including it in the sketch.
Frankly speaking, we only need three functions: play the music, change to another music, then of course, stop the music.
1 play the music when the led is turned on
2 change to another music with the other button simultaneously with the color
3 stop the music when the led is turned off
I did look for the said-manual of what serial mp3 player we used but just trying out the sketch they uploaded didn't work.
This is the serial player:
This is the guide for how it was supposed to be used:
Of which, we took these lines:
#include <SoftwareSerial.h>
#define MP3_RX 4 // to TX
#define MP3_TX 5 // to RX
static int8_t select_SD_card[] = {0x7e, 0x03, 0X35, 0x01, 0xef};
static int8_t play_first_song[] = {0x7e, 0x04, 0x41, 0x00, 0x01, 0xef};
static int8_t play_second_song[] = {0x7e, 0x04, 0x41, 0x00, 0x02, 0xef};
static int8_t play_second_song[] = {0x7e, 0x04, 0x41, 0x00, 0x02, 0xef};
static int8_t play[] = {0x7e, 0x02, 0x01, 0xef};
static int8_t pause[] = {0x7e, 0x02, 0x02, 0xef};
SoftwareSerial MP3(MP3_RX, MP3_TX);
void setup() {
Serial.begin(9600);
MP3.begin(9600);
send_command_to_MP3_player(select_SD_card, 7);
}
void loop() {
// Play the second song.
send_command_to_MP3_player(play_second_song, 8);
}
void send_command_to_MP3_player(int8_t command[], int len){
Serial.print("\nMP3 Command => ");
for(int i=0;i<len;i++){ MP3.write(command[i]); Serial.print(command[i], HEX); }
delay(1000);
}
Then another one:
https://acoptex.com/project/250/basics-project-051b-serial-mp3-player-module-with-1w-speaker-by-open-smart-at-acoptexcom/
We also thought of using other sketches disregarding the module (e.g. DFPlayer Mini) since there were similarities but it's not really working, by that I meant it's uploaded but not really doing anything.
At first we tried to combine the code with the led, then the program didn't worked out and even the led stopped working. So we separated it and tried to just run the initial sketch for the mp3 player (copy and pasted as is) and it also didn't work.
The goal is to have the music change with the color of the led so I was thinking of including the [play next song] command within here:
//change color
if(digitalRead(buttonCC) == LOW){
mode = mode + 1;
delay(400);
}
... and so on... (basically inside the change color lines of code)
Thanks for the help.
I will also be trying out some more finds from the internet so there might be changes, but I'll try to keep it neat.

