Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #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()
- {
- currentMillis = millis(); //Set currentMillis for this loop
- MyserialEvent(); //Check Bluetooth Data
- serialmenu(); //Parse DataString
- busyPinstate = digitalRead(busyPin); //BusyPin MP3 Module
- bwmstate = digitalRead(bwmPin); //Motion Sensor
- if (bwmstate == HIGH) //Check last Motion
- lastlow = currentMillis; //Set Last Time
- unsigned long temp = configs[14] * 1000;
- if (currentMillis - lastlow >= temp) //Check last Time for Delay Switch Active
- {
- lowtime = HIGH;
- }
- if (configs[12] == 1)
- { //check Switch Button from app On
- blinking(); //Blinking Function
- }
- else
- analogWrite(ledpin, 0); //Otherwise Leds off
- if (configs[13] != 0 && configs[12] != 0) // Sound in App On/off?
- soundcheck(); //Sound Function
- if (bwmstate != lastbwmstate)
- { //check Last Motion Sensor state
- lastbwmstate = bwmstate;
- }
- }
- 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() //Check Sound
- {
- if (currentMillis - lastsound >= (configs[10] * 2000) && configs[8] != 0 && lowtime)
- {
- if (bwmstate == HIGH && lastbwmstate == LOW)
- {
- lastbwmstate = HIGH;
- startsound = true;
- delaysound = currentMillis;
- }
- 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 = true;
- for (int i = 0; i < 17; i++)
- {
- temp = temp + configs[i];
- if (i != 17)
- temp = temp + ",";
- }
- uint16_t value = 0;
- byte buf[temp.length() + 1];
- temp.getBytes(buf, sizeof(buf));
- value = calc_crc(buf, sizeof(buf) - 1, 0xffff);
- temp = temp + ",";
- temp = temp + value;
- temp = temp + "\r\n";
- Serial.println(temp);
- BTSerial.print(temp);
- block = false;
- }
- else if (worker == "XXSETXX\r\n")
- {
- worker = "";
- receiveconfig = true;
- inputString = "";
- stringComplete = false;
- block = true;
- }
- else if (receiveconfig == true && stringComplete)
- {
- block = true;
- Serial.println(worker);
- int lastcomma = worker.lastIndexOf(",");
- String rxcrc = worker.substring(0, lastcomma);
- String rxval = worker.substring(lastcomma, -2);
- uint16_t rxvalue = 0;
- byte buff[rxcrc.length() + 1];
- rxcrc.getBytes(buff, sizeof(buff));
- rxvalue = calc_crc(buff, sizeof(buff) - 1, 0xffff);
- char buf[worker.length() + 1];
- worker.toCharArray(buf, sizeof(buf));
- int i = 0;
- char *ptr;
- ptr = strtok(buf, ",");
- while (ptr != NULL)
- {
- if (i < 16)
- {
- String tmpstr = ptr;
- uint8_t tempint = tmpstr.toInt();
- configsbuf[i] = tempint;
- i++;
- }
- else
- {
- String tmpstr = ptr;
- crcrx = (unsigned short) tmpstr.toInt();
- }
- ptr = strtok(NULL, ",");
- }
- //Serial.println(crcrx);
- //Serial.println(rxvalue);
- if (crcrx == rxvalue)
- {
- memcpy(configs, configsbuf, 18);
- if (bwmstate == HIGH || busyPinstate == HIGH)
- {
- minlight = configs[0];
- maxlight = configs[1];
- fract = configs[2];
- fractdelay = configs[3];
- }
- else
- {
- minlight = configs[4];
- maxlight = configs[5];
- fract = configs[6];
- fractdelay = configs[7];
- }
- if (fract == 0)
- fract = 1;
- if (fractdelay == 0)
- fractdelay = 1;
- it = random(minlight, maxlight);
- fordelay = fract;
- delayer = fractdelay;
- }
- else
- {
- }
- block = false;
- receiveconfig = false;
- worker = "";
- stringComplete = false;
- }
- else if (worker == "XXSAVEXX\r\n")
- {
- save_config();
- worker = "";
- stringComplete = false;
- }
- else if (worker == "XXSTARTXX\r\n")
- {
- WTV020SD16P.setVolume(configs[8]);
- delay(200);
- WTV020SD16P.asyncPlayVoice(configs[9]);
- worker = "";
- stringComplete = false;
- }
- else
- {
- clearinput = true;
- stringComplete = false;
- }
- }
- }
- void save_config()
- {
- for (int i = 0; i < 17; i++)
- EEPROM.update(i, configs[i]);
- }
- void load_config()
- {
- for (int i = 0; i < 17; i++)
- configs[i] = EEPROM.read(i);
- }
- boolean first = false;
- int oldbwmstate = LOW;
- boolean action=false;
- void blinking()
- {
- if (bwmstate == HIGH && lowtime)
- {
- ledstate = HIGH;
- lastloww = currentMillis;
- steps=0;
- }
- if (currentMillis - lastloww >= (configs[15] * 1000))
- {
- ledstate = LOW;
- }
- switch (steps)
- {
- case 0:
- if (lastledstate != ledstate)
- if (ledstate)
- {
- 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];
- }
- if (lastledstate != ledstate)
- {
- it = random(minlight, maxlight);
- lastledstate = ledstate;
- }
- if (fract == 0)
- fract = 1;
- if (fractdelay == 0)
- fractdelay = 1;
- fordelay = fractdelay*4;
- delayer = fractdelay;
- steps = 1;
- break;
- case 1:
- if (currentMillis - previousMillis >= fordelay)
- {
- previousMillis = currentMillis;
- it = it + fract;
- action=true;
- }
- if (it > randermax)
- {
- steps = 2;
- randermin = random(minlight, it);
- if ((it - randermin) < fract)
- it = it + fract;
- }
- if(action){
- analogWrite(ledpin, it);
- action=false;
- // Serial.println(it);
- }
- break;
- case 2:
- if (currentMillis - previousMillis >= fordelay)
- {
- previousMillis = currentMillis;
- it = it - fract;
- action=true;
- }
- if (it < randermin)
- {
- steps = 3;
- randermax = random(it, maxlight);
- if ((randermax - it) < fract)
- {
- if (it < fract)
- it = 0;
- else
- it = it - fract;
- }
- }
- if(action){
- analogWrite(ledpin, it);
- action=false;
- // Serial.println(it);
- }
- break;
- case 3:
- steps = 0;
- break;
- default:
- steps = 0;
- break;
- }
- }
- uint16_t calc_crc(unsigned char *msg, int n, uint16_t init)
- {
- uint16_t x = init;
- while (n--)
- {
- x = crc_xmodem_update(x, (uint16_t) * msg++);
- }
- return (x);
- }
- uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
- {
- int i;
- crc = crc ^ ((uint16_t) data << 8);
- for (i = 0; i < 8; i++)
- {
- if (crc & 0x8000)
- crc = (crc << 1) ^ 0x1021; //(polynomial = 0x1021)
- else
- crc <<= 1;
- }
- return crc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement