/* * print chars received through BLE into display * HC-08 3.3v */ #include #include #include #include LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); SoftwareSerial mySerial(8, 9); // RX, TX String maneuver = ""; String distance = ""; String street = ""; String readString; String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } void setup() { mySerial.begin(9600); lcd.begin(16, 2); lcd.clear(); lcd.print(" Power On "); } void loop() { while (mySerial.available()) { char c = mySerial.read(); //gets one byte from serial buffer readString += c; //makes the String readString delay(2); //slow looping to allow buffer to fill with next character } if (readString.length() >0) { // parses the string maneuver = getValue(readString, '\t', 0); distance = getValue(readString, '\t', 1); street = getValue(readString, '\t', 2); readString=""; // determine length for centering int maneuverPosition = (16 - maneuver.length())/2; int distancePosition = (16 - distance.length())/2; // determine if arrived if (street == "You have arrived"){ lcd.clear(); lcd.print("You have arrived"); } else{ lcd.clear(); lcd.setCursor(maneuverPosition,0); lcd.print(maneuver); lcd.setCursor(distancePosition,1); lcd.print(distance); } } }