Advertisement
Schupp

testinocomment

Oct 15th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.34 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include <Crc16.h>
  4. #include <EEPROM.h>
  5. #include <WTV020SD16P.h>
  6.  
  7. Crc16 crc;
  8. SoftwareSerial BTSerial(8, 7);
  9.  
  10. uint8_t configs[18] =
  11. {
  12.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  13. };
  14. uint8_t configsbuf[18] =
  15. {
  16.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  17. };
  18.  
  19. int ledpin = 3;
  20. int bwmPin = 2;
  21. int resetPin = 10; // The pin number of the reset pin.
  22. int clockPin = 6; // The pin number of the clock pin.
  23. int dataPin = 12; // The pin number of the data pin.
  24. int busyPin = 11; // The pin number of the busy pin.
  25. WTV020SD16P WTV020SD16P(clockPin, dataPin, busyPin, resetPin);
  26. unsigned short crcrx = 0;
  27. boolean shot = true;
  28. String inputString = ""; //Serial Buffer
  29. boolean stringComplete = false; //Serial Helper
  30. boolean block = false;
  31. boolean receiveconfig = 0;
  32. int minlight = 0;
  33. int maxlight = 0;
  34. String worker = "";
  35. unsigned long previousMillis = millis();
  36. unsigned long previousbwmMillis = millis();
  37. unsigned long previoussoundMillis = millis();
  38. int lastbwmstate = LOW;
  39. unsigned long lastlow = millis();
  40. boolean lowtime = LOW;
  41.  
  42. int fract = 0;
  43. int fractdelay = 0;
  44.  
  45. int itlast = 0;
  46. int itcnt = 0;
  47.  
  48. //sound
  49. int soundstart = 0;
  50. //delay
  51. int it = 0;
  52. unsigned int fordelay = 0;
  53. int delayer = 0;
  54. int steps = 0;
  55. unsigned long currentMillis = millis();
  56. int randermin = 0;
  57. int randermax = 0;
  58. int busyPinstate = LOW;
  59. unsigned long lastserial = millis();
  60. int bwmstate = LOW;
  61.  
  62. void setup()
  63. {
  64.   pinMode(13, OUTPUT);
  65.   digitalWrite(13, HIGH);
  66.   Serial.begin(57600);
  67.   BTSerial.begin(9600); //BTserial
  68.   inputString.reserve(100);
  69.   pinMode(ledpin, OUTPUT);
  70.   pinMode(bwmPin, INPUT);
  71.   pinMode(busyPin, INPUT);
  72.   analogWrite(ledpin, 255);
  73.  
  74.   load_config();
  75.   delay(1000);
  76.   WTV020SD16P.reset();
  77.   delay(1000);
  78.   bwmstate = digitalRead(bwmPin);
  79.   if (bwmstate == HIGH)
  80.   {
  81.     Serial.println("HIGH");
  82.     minlight = configs[0];
  83.     maxlight = configs[1];
  84.     fract = configs[2]; /// 4;
  85.     fractdelay = configs[3];
  86.   }
  87.   else
  88.   {
  89.     minlight = configs[4];
  90.     maxlight = configs[5];
  91.     fract = configs[6]; // / 4;
  92.     fractdelay = configs[7];
  93.   }
  94. }
  95.  
  96. boolean firststart = true;
  97.  
  98. void loop()
  99. {
  100.   currentMillis = millis(); //Set currentMillis for this loop
  101.   MyserialEvent(); //Check Bluetooth Data
  102.   serialmenu(); //Parse DataString
  103.   busyPinstate = digitalRead(busyPin); //BusyPin MP3 Module
  104.   bwmstate = digitalRead(bwmPin); //Motion Sensor
  105.   if (bwmstate == HIGH) //Check last Motion
  106.     lastlow = currentMillis; //Set Last Time
  107.  
  108.   unsigned long temp = configs[14] * 1000;
  109.  
  110.   if (currentMillis - lastlow >= temp) //Check last Time for Delay Switch Active
  111.   {
  112.     lowtime = HIGH;
  113.   }
  114.  
  115.   if (configs[12] == 1)
  116.   {             //check Switch Button from app On
  117.     blinking(); //Blinking Function
  118.   }
  119.   else
  120.     analogWrite(ledpin, 0); //Otherwise Leds off
  121.   if (configs[13] != 0 && configs[12] != 0) // Sound in App On/off?
  122.     soundcheck(); //Sound Function
  123.  
  124.   if (bwmstate != lastbwmstate)
  125.   { //check Last Motion Sensor state
  126.     lastbwmstate = bwmstate;
  127.   }
  128. }
  129.  
  130. int bwmsteps = 0;
  131.  
  132. boolean clearinput = false;
  133. unsigned long lastcall = 0;
  134. int i = 0;
  135. int lastbwm = LOW;
  136.  
  137. boolean startsound = true;
  138.  
  139. unsigned long lastsound = millis();
  140. unsigned long delaysound = millis();
  141. unsigned long lastloww = millis();
  142.  
  143. boolean lowwtime = LOW;
  144. boolean ledstate = LOW;
  145. int lastledstate = LOW;
  146.  
  147. void soundcheck() //Check Sound
  148. {
  149.   if (currentMillis - lastsound >= (configs[10] * 2000) && configs[8] != 0 && lowtime)
  150.   {
  151.     if (bwmstate == HIGH && lastbwmstate == LOW)
  152.     {
  153.       lastbwmstate = HIGH;
  154.       startsound = true;
  155.       delaysound = currentMillis;
  156.     }
  157.  
  158.     if (startsound && currentMillis - delaysound >= configs[11] * 2000
  159.         && startsound)
  160.     {
  161.       Serial.println("Sound");
  162.       delay(200);
  163.       WTV020SD16P.setVolume(configs[8]);
  164.       delay(300);
  165.       WTV020SD16P.asyncPlayVoice(configs[9]);
  166.  
  167.       startsound = false;
  168.       lastsound = currentMillis;
  169.       delaysound = currentMillis;
  170.       lastlow = currentMillis;
  171.       lowtime = LOW;
  172.     }
  173.   }
  174. }
  175.  
  176. void MyserialEvent()
  177. {
  178.   if (millis() - lastserial > 1000)
  179.   {
  180.     inputString = "";
  181.     stringComplete = false;
  182.     lastserial = millis();
  183.   }
  184.  
  185.   while (BTSerial.available())
  186.   {
  187.  
  188.     char inChar = (char) BTSerial.read();
  189.     inputString += inChar;
  190.  
  191.     if (inChar == '\n')
  192.     {
  193.       stringComplete = true;
  194.       worker = inputString;
  195.  
  196.       inputString = "";
  197.     }
  198.   }
  199. }
  200.  
  201. void serialmenu()
  202. {
  203.   if (stringComplete)
  204.   {
  205.     Serial.println(worker);
  206.     if (worker == "XXCHECKXX\r\n")
  207.     {
  208.       String temp = "";
  209.       stringComplete = false;
  210.       block = true;
  211.  
  212.       for (int i = 0; i < 17; i++)
  213.       {
  214.         temp = temp + configs[i];
  215.         if (i != 17)
  216.           temp = temp + ",";
  217.       }
  218.  
  219.       uint16_t value = 0;
  220.       byte buf[temp.length() + 1];
  221.       temp.getBytes(buf, sizeof(buf));
  222.       value = calc_crc(buf, sizeof(buf) - 1, 0xffff);
  223.       temp = temp + ",";
  224.       temp = temp + value;
  225.       temp = temp + "\r\n";
  226.       Serial.println(temp);
  227.       BTSerial.print(temp);
  228.       block = false;
  229.  
  230.     }
  231.     else if (worker == "XXSETXX\r\n")
  232.     {
  233.       worker = "";
  234.       receiveconfig = true;
  235.       inputString = "";
  236.       stringComplete = false;
  237.       block = true;
  238.  
  239.     }
  240.     else if (receiveconfig == true && stringComplete)
  241.     {
  242.       block = true;
  243.       Serial.println(worker);
  244.       int lastcomma = worker.lastIndexOf(",");
  245.       String rxcrc = worker.substring(0, lastcomma);
  246.       String rxval = worker.substring(lastcomma, -2);
  247.  
  248.       uint16_t rxvalue = 0;
  249.  
  250.       byte buff[rxcrc.length() + 1];
  251.       rxcrc.getBytes(buff, sizeof(buff));
  252.       rxvalue = calc_crc(buff, sizeof(buff) - 1, 0xffff);
  253.       char buf[worker.length() + 1];
  254.       worker.toCharArray(buf, sizeof(buf));
  255.       int i = 0;
  256.       char *ptr;
  257.  
  258.       ptr = strtok(buf, ",");
  259.       while (ptr != NULL)
  260.       {
  261.         if (i < 16)
  262.         {
  263.           String tmpstr = ptr;
  264.           uint8_t tempint = tmpstr.toInt();
  265.           configsbuf[i] = tempint;
  266.           i++;
  267.         }
  268.         else
  269.         {
  270.           String tmpstr = ptr;
  271.           crcrx = (unsigned short) tmpstr.toInt();
  272.         }
  273.         ptr = strtok(NULL, ",");
  274.       }
  275.       //Serial.println(crcrx);
  276.       //Serial.println(rxvalue);
  277.       if (crcrx == rxvalue)
  278.       {
  279.         memcpy(configs, configsbuf, 18);
  280.         if (bwmstate == HIGH || busyPinstate == HIGH)
  281.         {
  282.           minlight = configs[0];
  283.           maxlight = configs[1];
  284.           fract = configs[2];
  285.           fractdelay = configs[3];
  286.         }
  287.         else
  288.         {
  289.           minlight = configs[4];
  290.           maxlight = configs[5];
  291.           fract = configs[6];
  292.           fractdelay = configs[7];
  293.         }
  294.  
  295.         if (fract == 0)
  296.           fract = 1;
  297.         if (fractdelay == 0)
  298.           fractdelay = 1;
  299.         it = random(minlight, maxlight);
  300.         fordelay = fract;
  301.         delayer = fractdelay;
  302.       }
  303.       else
  304.       {
  305.       }
  306.  
  307.       block = false;
  308.       receiveconfig = false;
  309.       worker = "";
  310.       stringComplete = false;
  311.     }
  312.     else if (worker == "XXSAVEXX\r\n")
  313.     {
  314.       save_config();
  315.       worker = "";
  316.       stringComplete = false;
  317.     }
  318.     else if (worker == "XXSTARTXX\r\n")
  319.     {
  320.       WTV020SD16P.setVolume(configs[8]);
  321.       delay(200);
  322.       WTV020SD16P.asyncPlayVoice(configs[9]);
  323.  
  324.       worker = "";
  325.       stringComplete = false;
  326.     }
  327.     else
  328.     {
  329.       clearinput = true;
  330.       stringComplete = false;
  331.     }
  332.   }
  333. }
  334.  
  335. void save_config()
  336. {
  337.   for (int i = 0; i < 17; i++)
  338.     EEPROM.update(i, configs[i]);
  339. }
  340.  
  341. void load_config()
  342. {
  343.   for (int i = 0; i < 17; i++)
  344.     configs[i] = EEPROM.read(i);
  345. }
  346. boolean first = false;
  347. int oldbwmstate = LOW;
  348. boolean action=false;
  349. void blinking()
  350. {
  351.  
  352.   if (bwmstate == HIGH && lowtime)
  353.   {
  354.     ledstate = HIGH;
  355.     lastloww = currentMillis;
  356.     steps=0;
  357.   }
  358.  
  359.   if (currentMillis - lastloww >= (configs[15] * 1000))
  360.   {
  361.     ledstate = LOW;
  362.   }
  363.  
  364.   switch (steps)
  365.   {
  366.     case 0:
  367.  
  368.       if (lastledstate != ledstate)
  369.         if (ledstate)
  370.         {
  371.           minlight = configs[0];
  372.           maxlight = configs[1];
  373.           fract = configs[2]; /// 4;
  374.           fractdelay = configs[3];
  375.  
  376.         }
  377.         else
  378.         {
  379.           minlight = configs[4];
  380.           maxlight = configs[5];
  381.           fract = configs[6]; // / 4;
  382.           fractdelay = configs[7];
  383.         }
  384.  
  385.       if (lastledstate != ledstate)
  386.       {
  387.         it = random(minlight, maxlight);
  388.         lastledstate = ledstate;
  389.       }
  390.  
  391.       if (fract == 0)
  392.         fract = 1;
  393.       if (fractdelay == 0)
  394.         fractdelay = 1;
  395.  
  396.       fordelay = fractdelay*4;
  397.       delayer = fractdelay;
  398.  
  399.       steps = 1;
  400.       break;
  401.  
  402.     case 1:
  403.  
  404.       if (currentMillis - previousMillis >= fordelay)
  405.       {
  406.         previousMillis = currentMillis;
  407.         it = it + fract;
  408.         action=true;
  409.       }
  410.  
  411.       if (it > randermax)
  412.       {
  413.         steps = 2;
  414.         randermin = random(minlight, it);
  415.         if ((it - randermin) < fract)
  416.           it = it + fract;
  417.       }
  418.       if(action){
  419.       analogWrite(ledpin, it);
  420.       action=false;
  421.      //  Serial.println(it);
  422.       }
  423.       break;
  424.  
  425.     case 2:
  426.  
  427.       if (currentMillis - previousMillis >= fordelay)
  428.       {
  429.         previousMillis = currentMillis;
  430.         it = it - fract;
  431.       action=true;
  432.       }
  433.       if (it < randermin)
  434.       {
  435.         steps = 3;
  436.         randermax = random(it, maxlight);
  437.         if ((randermax - it) < fract)
  438.         {
  439.           if (it < fract)
  440.             it = 0;
  441.           else
  442.             it = it - fract;
  443.         }
  444.       }
  445.       if(action){
  446.       analogWrite(ledpin, it);
  447.       action=false;
  448.      //  Serial.println(it);
  449.       }
  450.       break;
  451.     case 3:
  452.       steps = 0;
  453.       break;
  454.     default:
  455.       steps = 0;
  456.       break;
  457.   }
  458. }
  459.  
  460. uint16_t calc_crc(unsigned char *msg, int n, uint16_t init)
  461. {
  462.   uint16_t x = init;
  463.  
  464.   while (n--)
  465.   {
  466.     x = crc_xmodem_update(x, (uint16_t) * msg++);
  467.   }
  468.  
  469.   return (x);
  470. }
  471.  
  472. uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
  473. {
  474.   int i;
  475.  
  476.   crc = crc ^ ((uint16_t) data << 8);
  477.   for (i = 0; i < 8; i++)
  478.   {
  479.     if (crc & 0x8000)
  480.       crc = (crc << 1) ^ 0x1021; //(polynomial = 0x1021)
  481.     else
  482.       crc <<= 1;
  483.   }
  484.   return crc;
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement