Advertisement
tburton

Untitled

Jun 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5.  
  6. String inputString = "";         // a string to hold incoming data
  7. boolean stringComplete = false;  // whether the string is complete
  8. String commandString = "";
  9.  
  10. void setup() {
  11.   lcd.begin();
  12.   lcd.setBacklight((uint8_t)1);
  13.   Serial.begin(9600);
  14.   initDisplay();
  15. }
  16.  
  17. void loop() {
  18.  
  19.   if(stringComplete)
  20.   {
  21.     stringComplete = false;
  22.     getCommand();
  23.    
  24.     if(commandString.equals("STAR"))
  25.     {
  26.       lcd.clear();
  27.     }
  28.     if(commandString.equals("STOP"))
  29.     {
  30.       lcd.clear();
  31.       lcd.print("Ready to connect");    
  32.     }
  33.     else if(commandString.equals("TEXT"))
  34.     {
  35.       String text = getTextToPrint();
  36.       printText(text);
  37.     }
  38.    
  39.     inputString = "";
  40.   }
  41. }
  42.  
  43. void initDisplay()
  44. {
  45.   lcd.print("Ready to connect");
  46. }
  47.  
  48. void getCommand()
  49. {
  50.   if(inputString.length()>0)
  51.   {
  52.      commandString = inputString.substring(1,5);
  53.   }
  54. }
  55.  
  56. String getTextToPrint()
  57. {
  58.   String value = inputString.substring(5,inputString.length()-2);
  59.   return value;
  60. }
  61.  
  62. void printText(String text)
  63. {
  64.   lcd.clear();
  65.   lcd.setCursor(0,0);
  66.  
  67.   int i = 0;
  68.   int dash_pointer = 0;
  69.  
  70.   for(i=0; i<text.length(); i++){
  71.     if(text[i] == '-' && text[i-1] == ' ' && text[i+1] == ' '){
  72.       dash_pointer = i;
  73.       break;
  74.     }
  75.   }
  76.  
  77.     if(dash_pointer == 0)
  78.     {
  79.       lcd.print(text);
  80.     }else
  81.     {
  82.       lcd.print(text.substring(0,dash_pointer-1));
  83.       lcd.setCursor(0,1);
  84.       lcd.print(text.substring(dash_pointer+2,dash_pointer+18)); // print 16 letters only (pointer +2 + 16)
  85.     }
  86. }
  87.  
  88. void serialEvent() {
  89.   while (Serial.available()) {
  90.     // get the new byte:
  91.     char inChar = (char)Serial.read();
  92.     // add it to the inputString:
  93.     inputString += inChar;
  94.     // if the incoming character is a newline, set a flag
  95.     // so the main loop can do something about it:
  96.     if (inChar == '\n') {
  97.       stringComplete = true;
  98.     }
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement