Happy Birthday Small Basic - The 8th Anniversary
Can you read the message on the LCD in this photo? Happy Birthday, Small Basic!
Today's Small Basic program is
' Send Text to Serial Port
' Version 0.1
' Copyright © 2016 Nonki Takahashi. The MIT License.
TextWindow``.`` Title `` = ``"Send Text to Serial Port"
Init``(``)
While ``"True"
`` txt `` = ``TextWindow``.``Read``(``)
``LDCommPort``.``TXString``(`` txt `` + ``LF``)
EndWhile
Sub ``Init
`` LF `` = ``Text``.``GetCharacter``(``10``)
`` status `` = ``LDCommPort``.``OpenPort``(``"COM3"``,``9600``)
``LDCommPort``.``SetEncoding``(``"Ascii"``)
EndSub
And the Arduino sketch is
// LCD control via I2C from serial port
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Variable definition
const int r = 2; // rows
const int c = 16; // columns
LiquidCrystal_I2C lcd(0x3F, c, r); // set the LCD address to 0x3F
String inputString = ""; // received string
boolean stringComplete = false; // received?
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
}
void loop() {
if (stringComplete) {
int len = inputString.length();
lcd.clear();
if (len <= c) {
lcd_print(inputString);
} else {
lcd_print(inputString.substring(0, c));
lcd.setCursor(0, 1);
lcd_print(inputString.substring(c));
}
inputString = "";
stringComplete = false;
}
}
// Print string to LCD
void lcd_print(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
lcd.print(str.charAt(i));
}
}
// Serial event handler
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read(); // read 1 byte
if (inChar == '\n') {
// flag on if reseived newline
stringComplete = true;
} else {
inputString += inChar; // append it to the string
}
}
}