Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- String inputString = ""; // a string to hold incoming data
- boolean stringComplete = false; // whether the string is complete
- String commandString = "";
- void setup() {
- lcd.begin();
- lcd.setBacklight((uint8_t)1);
- Serial.begin(9600);
- initDisplay();
- }
- void loop() {
- if(stringComplete)
- {
- stringComplete = false;
- getCommand();
- if(commandString.equals("STAR"))
- {
- lcd.clear();
- }
- if(commandString.equals("STOP"))
- {
- lcd.clear();
- lcd.print("Ready to connect");
- }
- else if(commandString.equals("TEXT"))
- {
- String text = getTextToPrint();
- printText(text);
- }
- inputString = "";
- }
- }
- void initDisplay()
- {
- lcd.print("Ready to connect");
- }
- void getCommand()
- {
- if(inputString.length()>0)
- {
- commandString = inputString.substring(1,5);
- }
- }
- String getTextToPrint()
- {
- String value = inputString.substring(5,inputString.length()-2);
- return value;
- }
- void printText(String text)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- int i = 0;
- int dash_pointer = 0;
- for(i=0; i<text.length(); i++){
- if(text[i] == '-' && text[i-1] == ' ' && text[i+1] == ' '){
- dash_pointer = i;
- break;
- }
- }
- if(dash_pointer == 0)
- {
- lcd.print(text);
- }else
- {
- lcd.print(text.substring(0,dash_pointer-1));
- lcd.setCursor(0,1);
- lcd.print(text.substring(dash_pointer+2,dash_pointer+18)); // print 16 letters only (pointer +2 + 16)
- }
- }
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (inChar == '\n') {
- stringComplete = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement