Advertisement
Guest User

Mauro

a guest
May 3rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <MemoryFree.h>
  3. #include <SPI.h>
  4. #include "Adafruit_BLE_UART.h"
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7.  
  8. // Connect CLK/MISO/MOSI to hardware SPI
  9. // e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
  10. #define ADAFRUITBLE_REQ 10
  11. #define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
  12. #define ADAFRUITBLE_RST 9
  13.  
  14.  
  15. Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
  16. /**************************************************************************/
  17. /*!
  18. Configure the Arduino and start advertising with the radio
  19. */
  20. /**************************************************************************/
  21. LiquidCrystal_I2C lcd(0x27, 16, 2);
  22.  
  23. long ArrayC[20] ;
  24. String inputString;
  25. String overString;
  26. String Stringa[10];
  27.  
  28. int LunghezzaStringa ;
  29. unsigned long previousMillis = 0;
  30. int Nstring;
  31. int *v;
  32.  
  33.  
  34. void setup() {
  35.  
  36. Serial.begin(115200);
  37. for (int i = 0; i < 20; i++)
  38. ArrayC[i] = 0;
  39. lcd.init();
  40. lcd.backlight();
  41. lcd.setCursor(0, 1);
  42. lcd.clear();
  43. BTLEserial.setDeviceName("MARCO"); /* 7 characters max! */
  44. BTLEserial.begin();
  45.  
  46.  
  47. }
  48.  
  49. aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
  50.  
  51. void loop() {
  52. unsigned long currentMillis = millis();
  53. BTLEserial.pollACI();
  54. aci_evt_opcode_t status = BTLEserial.getState();
  55. if (status != laststatus) {
  56. // print it out!
  57. if (status == ACI_EVT_DEVICE_STARTED) {
  58. lcd.clear();
  59. lcd.setCursor(0, 0);
  60. lcd.print(F("In Attesa di"));
  61. lcd.setCursor(0, 1);
  62. lcd.print(F("Una Connessione"));
  63. }
  64. if (status == ACI_EVT_CONNECTED) {
  65. lcd.clear();
  66. lcd.setCursor(0, 0);
  67. lcd.print(F("Connesso!"));
  68. }
  69. if (status == ACI_EVT_DISCONNECTED) {
  70. lcd.clear();
  71. lcd.setCursor(0, 0);
  72. lcd.print(F("Disconnesso"));
  73. }
  74. // OK set the last status change to this one
  75. laststatus = status;
  76. }
  77.  
  78.  
  79. if (status == ACI_EVT_CONNECTED) {
  80. // Serial.print("freeMemory()=");
  81. // Serial.println(freeMemory());
  82. // Lets see if there's any data for us!
  83. if (BTLEserial.available()) {
  84. Nstring++;
  85.  
  86. if (Nstring == 1)
  87. for (int i = 0; i < 10; i++)
  88. Stringa[i] = "";
  89.  
  90. previousMillis = currentMillis;
  91. lcd.clear();
  92. lcd.print(freeMemory());
  93. Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
  94.  
  95. }
  96. // OK while we still have something to read, get a character and print it out
  97. int it = 0;
  98. while (BTLEserial.available()) {
  99. int c = BTLEserial.read();
  100. ArrayC[it] = c;
  101. inputString += char(c);
  102. it++;
  103. }
  104.  
  105.  
  106.  
  107. overString += inputString;
  108. inputString = "";
  109.  
  110.  
  111. if (currentMillis - previousMillis >= 250) {
  112. previousMillis = currentMillis;
  113. Nstring = 0;
  114. if (overString.length() > 0) {
  115. Serial.println(overString);
  116. Taglio(overString);
  117. for (int i = 0; i < 10; i++)
  118. Serial.println(Stringa[i]);
  119.  
  120. overString = "";
  121. }
  122. }
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133.  
  134.  
  135. void Taglio(String Iniziale)
  136. {
  137. free(v);
  138. Iniziale.reserve(100);
  139. Stringa[0] = Iniziale;
  140. for (int i = 0; i < 10; i++)
  141. {
  142. // v = (int*) malloc(sizeof(Stringa[i])) ;
  143.  
  144. if (Stringa[i].length() > 16) {
  145. Iniziale = Stringa[i];
  146. int StartLength = Iniziale.length();
  147. Stringa[i] = Iniziale.substring(0, 16);
  148. int LastSpace = Stringa[i].lastIndexOf(" ");
  149. Stringa[i + 1] = Iniziale.substring( LastSpace + 1, StartLength );
  150. Stringa[i] = Iniziale.substring(0, LastSpace);
  151.  
  152. }
  153. }
  154. }
  155. /*
  156.  
  157. int StartLength = Iniziale.length();
  158. if (StartLength > 16) {
  159. Stringa[0] = Iniziale.substring(0, 16);
  160. int LastSpace = Stringa[0].lastIndexOf(" ");
  161. Stringa[1] = Iniziale.substring( LastSpace + 1, StartLength );
  162. Stringa[0] = Iniziale.substring(0, LastSpace);
  163. if (Stringa[1].length() > 16) {
  164. Iniziale = Stringa[1];
  165. StartLength = Iniziale.length();
  166. Stringa[1] = Iniziale.substring(0, 16);
  167. LastSpace = Stringa[1].lastIndexOf(" ");
  168. Stringa[2] = Iniziale.substring( LastSpace + 1, StartLength );
  169. Stringa[1] = Iniziale.substring(0, LastSpace);
  170. if (Stringa[2].length() > 16) {
  171. Iniziale = Stringa[2];
  172. StartLength = Iniziale.length();
  173. Stringa[2] = Iniziale.substring(0, 16);
  174. LastSpace = Stringa[2].lastIndexOf(" ");
  175. Stringa[3] = Iniziale.substring( LastSpace + 1, StartLength );
  176. Stringa[2] = Iniziale.substring(0, LastSpace);
  177. }
  178. }
  179. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement