//----NEOPIXEL SETUP-----
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
static const int PIXEL_PIN = D5;
static const int PIXEL_COUNT = 4;
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
//-----DF PLAYER SETUP-----
# 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]
//--------TIME--------
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 250; // in ms
//-----SERVO PINS-----
static const int servoPin = A5; // Set Servo Pin
Servo myservo; // create servo object to control a servo
int pos;
//-----BUTTON PINS-----
static const int bOne = D2; // button one
static const int bTwo = D3; // button two
static const int bThree = D4; // button three
//-----VARIABLES BUTTON ONE-----
int bStateOne = 0;
int lbsOne = 0;
int bCountOne = 0;
int switchOne;
//-----VARIABLES BUTTON TWO-----
int bStateTwo = 0;
int lbsTwo = 0;
int bCountTwo = 0;
int switchTwo;
//-----VARIABLES BUTTON THREE-----
int bStateThree = 0;
int lbsThree = 0;
int bCountThree = 0;
int switchThree;
bool currentPlatformState = false;
void setup (){
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
Serial1.begin(9600);
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(bOne, INPUT);
pinMode(bTwo, INPUT);
pinMode(bThree, INPUT);
pixels.begin();
pixels.setBrightness(90);
pixels.show();
execute_CMD(0x3F, 0, 0); // Send request for initialization parameters
while (Serial1.available()<10) // Wait until initialization parameters are received (10 bytes)
delay(30); // Pretty long delays between succesive commands needed (not always the same)
// Initialize sound to very low volume. Adapt according used speaker and wanted volume
execute_CMD(0x06, 0, 0x30); // Set the volume (0x00~0x30)
delay(500);
execute_CMD(0x07, 0, 2); // Sets the equializer
delay(500);
execute_CMD(0x16,0,0);
delay(500);
// buttonOne();
// buttonTwo();
// buttonThree();
//setSong();
}
void loop (){
pOff(0);
pOff(1);
pOff(2);
pOff(3);
delay(50);
unsigned long now = millis();
if (lastSampleTime + sampleInterval < now) {
lastSampleTime = now;
bStateOne = digitalRead(bOne);
bStateTwo = digitalRead(bTwo);
bStateThree = digitalRead(bThree);
if (bStateOne != lbsOne) {
if (bStateOne == HIGH) {
bCountOne++;
Serial.print("Button One: ");
Serial.println(bCountOne);
buttonOne();
}
}
if (bStateTwo != lbsTwo) {
if (bStateTwo == HIGH) {
bCountTwo++;
Serial.print("Button Two: ");
Serial.println(bCountTwo);
buttonTwo();
}
}
if (bStateThree != lbsThree) {
if (bStateThree == HIGH) {
bCountThree++;
Serial.print("Button Three: ");
Serial.println(bCountThree);
buttonThree();
}
}
delay(50);
lbsOne = bStateOne;
lbsTwo = bStateTwo;
lbsThree = bStateThree;
}
}
void buttonOne() {
if (bCountOne % 2 == 0) { // SECOND PRESS
switchOne = 2;
} else if (bCountOne % 2 == 1){ // FIRST PRESS
switchOne = 1;
}
switch(switchOne){
case 1:
pBlink();
delay(50);
execute_CMD(0x03,0,1); // set new song
delay(500);
break;
case 2:
if (currentPlatformState == true){
pBlink();
servoDOWN();
delay(50);
} else {
execute_CMD(0x03,0,1); // set new song
delay(500);
}
break;
}
// execute_CMD(0x03,0,1); // set new song
// delay(500);
}
void buttonTwo() {
// servoUP();
// delay(50);
currentPlatformState = true;
execute_CMD(0x03,0,3); // set new song
delay(500);
if (bCountTwo % 2 == 0) { // SECOND PRESS
switchTwo = 2;
} else if (bCountTwo % 2 == 1){ // FIRST PRESS
switchTwo = 1;
}
switch(switchTwo){
case 1:
servoUP();
delay(75);
break;
case 2:
servoDOWN();
delay(75);
break;
}
// switch(switchTwo){
// case 1:
// pOn(0);
// pOn(1);
// pOn(2);
// pOn(3);
// delay(50);
// execute_CMD(0x03,0,3); // set new song
// delay(500);
// break;
// case 2:
// pOn(0);
// pOn(1);
// pOn(2);
// pOn(3);
// if (currentPlatformState == false){
// servoUP();
// delay(50);
// } else {
// execute_CMD(0x03,0,3); // set new song
// delay(500);
// }
// break;
// }
}
void buttonThree() {
myservo.write(125);
delay(50);
execute_CMD(0x03,0,5); // set new song
delay(500);
}
void pOn(int n){
pixels.setPixelColor(n, pixels.Color(0, 255, 0));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(10);
}
void pBlink(){
for (int i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, pixels.Color(0, 255, 0));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(750);
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show(); // This sends the updated pixel color to the hardware.
delay(750);
}
}
void pOff(int n){
pixels.setPixelColor(n, pixels.Color(0, 0, 0));
pixels.show();
delay(10);
}
void pixelsSOLID(){
pixels.setPixelColor(0, 200, 0, 150);
pixels.setPixelColor(2, 200, 0, 150);
pixels.setPixelColor(3, 200, 0, 150);
pixels.setPixelColor(4, 200, 0, 150);
pixels.show();
delay(10);
}
void fadeIn() {
int i, j;
for (j = 0; j < 255; j++) {
for (i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, 0, 0, j);
}
pixels.show();
delay(10);
}
}
// 255 to 0
void fadeOut() {
int i, j, h;
for (j = 255; j > 0; j--) {
for (i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, 0, 0, j);
}
pixels.show();
delay(10);
}
delay(500);
}
void servoUP() {
for (pos = 0; pos <= 35; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void servoDOWN() {
for (pos = 35; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
// By Ype Brada 2015-04-06
// https://community.particle.io/t/a-great-very-cheap-mp3-sound-module-without-need-for-a-library/20111
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
int16_t 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, checksum >> 8, checksum & 0xFF, End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
Serial1.write( Command_line[k]);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .