LCD I2C display serial message

This testing module is used to display message received via serial on LCD with I2C driver. The purpose of this module is when you use 2 Arduino boards for broadcasting, one transmitter and one receiver. This module is used by the receiver to test the sending message. And this is the one-step-ahead for module nRF24L01, LoRa, Bluetooth or GSM testing. I give you 2 ways to display: direct display without buffering and buffering display in case you want to modified the message you receive (transfer message to number, interpreting the data, for instance, analog vaule 512 will equal 28ºC and calculating, subtracting offset …)
direct display without buffering


/*
 * Date: Wed, 03/05/2017
 * Desc: Receive message from serial
 * Display to LCD I2c
 * direct display to LCD without buffering
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F 
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
 Serial.begin(9600);
 lcd.begin(16,2);
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(HIGH);
 lcd.clear();
 lcd.print("Waiting message:");
}

void loop()
{
 // when characters arrive over the serial port...
 if (Serial.available()) {
 // wait a bit for the entire message to arrive
 delay(100);
 // clear the screen
 lcd.clear();
 // read all the available characters
 while (Serial.available() > 0) {
 // display each character to the LCD WITHOUT BUFFERING
 lcd.write(Serial.read());
 }
 }
}

 

buffering display


/*
 * Date: Wed, 03/05/2017
 * Desc: Receive message from serial
 * Display to LCD I2c
 * direct display to LCD with buffering
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F 
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

// used for serial buffering
#define TERM_CHAR '\n'
#define BUF_LEN 16
int i = 0;
char incomingChar; 
char buf[BUF_LEN];

void setup()
{
 Serial.begin(9600);
 lcd.begin(16,2);
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(HIGH);
 lcd.clear();
 lcd.print("Waiting message:");
}

void loop()
{
 // when characters arrive over the serial port...
 if (Serial.available()) {
 // wait a bit for the entire message to arrive
 delay(100);
 // clean buffer and index
 memset(buf,'\0',BUF_LEN); 
 i = 0; 
 // read all the available characters
 while (Serial.available() > 0) {
 incomingChar = (char) Serial.read();
 if(incomingChar != TERM_CHAR && i != BUF_LEN) 
 buf[i++] = incomingChar;
 else
 break;
 delay(1); // wait for another byte 
 }
 // clear the screen and display message
 lcd.clear();
 lcd.print("Serial message:");
 lcd.setCursor(0,1); // 2nd line 
 lcd.write(buf);
 }
}

you can make the different BUF_LEN (but maximum value will be 128)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s