Arduino-Code:

#include <SoftwareSerial.h>
#include <Crc16.h>
#include <EEPROM.h>
#include <WTV020SD16P.h>

Crc16 crc;
SoftwareSerial BTSerial(8, 7);

uint8_t   configs[18]     =  {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  };
uint8_t   configsbuf[18]  =  {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  };

int ledpin = 3;
int bwmPin = 2;
int resetPin = 10;  // The pin number of the reset pin.
int clockPin = 6;  // The pin number of the clock pin.
int dataPin = 12;  // The pin number of the data pin.
int busyPin = 11;  // The pin number of the busy pin.
WTV020SD16P WTV020SD16P(clockPin, dataPin, busyPin, resetPin);
unsigned short crcrx = 0;
boolean shot = true;
String inputString = ""; //Serial Buffer
boolean stringComplete = false; //Serial Helper
boolean block = false;
boolean receiveconfig = 0;
int minlight = 0;
int maxlight = 0;
String worker = "";
unsigned long previousMillis = millis();
unsigned long previousbwmMillis = millis();
unsigned long previoussoundMillis = millis();
int lastbwmstate = LOW;
unsigned long lastlow = millis();
boolean lowtime = LOW;


int fract = 0;
int fractdelay = 0;

int itlast = 0;
int itcnt = 0;


//sound
int soundstart = 0;
//delay
int it = 0;
unsigned int fordelay = 0;
int delayer = 0;
int steps = 0;
unsigned long currentMillis = millis();
int randermin = 0;
int randermax = 0;
int busyPinstate = LOW;
unsigned long lastserial = millis();
int bwmstate = LOW;

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.begin(57600);
  BTSerial.begin(9600); //BTserial
  inputString.reserve(100);
  pinMode(ledpin, OUTPUT);
  pinMode(bwmPin, INPUT);
  pinMode(busyPin, INPUT);
  analogWrite(ledpin, 255);

  load_config();
  delay(1000);
  WTV020SD16P.reset();
  delay(1000);
  bwmstate = digitalRead(bwmPin);
  if (bwmstate == HIGH)
  {
    Serial.println("HIGH");
    minlight = configs[0];
    maxlight = configs[1];
    fract = configs[2] ;/// 4;
    fractdelay = configs[3];
  } else {
    minlight = configs[4];
    maxlight = configs[5];
    fract = configs[6];// / 4;
    fractdelay = configs[7];
  }
}

boolean firststart = true;
void loop() {
  MyserialEvent();
  serialmenu();
  busyPinstate = digitalRead(busyPin);
  bwmstate = digitalRead(bwmPin);
  if (bwmstate == HIGH)
    lastlow = currentMillis;
  unsigned long temp = configs[14] * 1000;

  if ( currentMillis - lastlow >= temp)
  {
    lowtime = HIGH;
  } else {}

  currentMillis = millis();
  if (configs[12] == 1) {
    blinking();
  } else  analogWrite(ledpin, 0 );
  if (configs[13] != 0 && configs[12] != 0 )
    soundcheck();
  if (bwmstate != lastbwmstate) {
    lastbwmstate = bwmstate;
    Serial.println("toggle");
  }
}

int bwmsteps = 0;

boolean clearinput = false;
unsigned long lastcall = 0; int i = 0;
int lastbwm = LOW;

boolean startsound = true;

unsigned long lastsound = millis();
unsigned long delaysound = millis();
unsigned long lastloww = millis();


boolean lowwtime = LOW;
boolean ledstate = LOW;
int lastledstate = LOW;
void soundcheck() {
  if ( currentMillis - lastsound >= (configs[10] * 2000) && configs[8] != 0 && lowtime)
  {
    if ( bwmstate == HIGH && lastbwmstate == LOW ) {
      lastbwmstate = HIGH;
      startsound = true;
      delaysound = currentMillis;
      Serial.println("SOUNDTIME");
    }

    if ( startsound && currentMillis - delaysound >= configs[11] * 2000  && startsound )
    {
      Serial.println("Sound");
      delay(200);
      WTV020SD16P.setVolume(configs[8]);
      delay(300);
      WTV020SD16P.asyncPlayVoice(configs[9]);

      startsound = false;
      lastsound = currentMillis;
      delaysound = currentMillis;
      lastlow = currentMillis;
      lowtime = LOW;
    }
  }
}

void MyserialEvent() {
  if (millis() - lastserial > 1000)
  {
    inputString = "";
    stringComplete = false;
    lastserial = millis();
  }

  while (BTSerial.available()) {

    char inChar = (char)BTSerial.read();
    inputString += inChar;

    if (inChar == '\n') {
      stringComplete = true;
      worker = inputString;

      inputString = "";
    }
  }
}

void serialmenu() {
  if (stringComplete)
  {
    Serial.println(worker);
    if (worker == "XXCHECKXX\r\n")
    {
      String temp = "";
      stringComplete = false;
 block...
Read more »