Go Down

Topic: RESOLVED I2C instead of serial (Read 1 time) previous topic - next topic

kd7eir

Feb 19, 2013, 05:56 am Last Edit: Feb 20, 2013, 12:54 am by kd7eir Reason: 1
Hello,

I am trying to port print commands for a serial LCD to an I2C LCD.  Here is the code I am using that has the serial LCD print commands.  I need to know how to print the results using an I2C LCD using the LiquidCrystal_I2C library.  I know how to #include the libraries for the I2C, I just do not know how to convert the LCD printing related commands such as "sprintf(databuff,"CV:%3d Cnm:%3d",sensorValueTest,i);" to print to my I2C LCD.  Replacing the sprintf with lcd.print obviously does not work.

Code: [Select]
#include <EEPROM.h>
#include <SoftwareSerial.h>

//Set up the Liquid Crystal Display  
#define LCDIn 3
#define LCDOut 2
SoftwareSerial mySerialPort(LCDIn, LCDOut);

//LCD Display buffers
char databuff[16];
char dispbuff[16];

// Variables needed for RGB calculations
float Gamma = 1.00;
int MaxIntensity = 255;
float fBlue;
float fGreen;
float fRed;
float Factor;

int iR;
int iG;
int iB;

//Our eyes can generally see light wavelengths between 350 and 700 nanometers.
//Here, we start the RGB Led with 350
int i = 350;

//RGB is plugged into these arduino digital pins
const int redOutPin = 8;
const int greenOutPin = 6;
const int blueOutPin = 4;

// LED to be tested is plugged into A0
int testPin = A0;    

// variables to store the value coming from the sensor
int sensorValueTest =0;  
int oldTest =0;
int peaknm =0;

//EEPROM start data
int addr=0;

//Music
int notelen = 90;
int dlx = 130;

void setup()
{
 pinMode(LCDOut, OUTPUT);
 pinMode(LCDIn, INPUT);

//Set the RGB LED pins to output

 pinMode(redOutPin, OUTPUT);
 pinMode(greenOutPin, OUTPUT);
 pinMode(blueOutPin, OUTPUT);


// Initialize the LCD display
 mySerialPort.begin(9600);
 mySerialPort.write(0xFE);
 mySerialPort.write(0x01);

 // test to see if the RGB LED works
 makeColor(i);
 analogWrite(redOutPin,255-iR);
 analogWrite(greenOutPin, 255-iG);
 analogWrite(blueOutPin, 255-iB);
 delay(5000);
}

void loop()
{

// set the RGB LED to a specific color
 makeColor(i);
 analogWrite(redOutPin, 255-iR);
 analogWrite(greenOutPin, 255-iG);
 analogWrite(blueOutPin, 255-iB);
 delay(500);
 
// read the sensitivity of the Test LED
 sensorValueTest= analogRead(testPin);

 if (sensorValueTest > oldTest)
 {
   oldTest = sensorValueTest;
   peaknm = i;
 }

// Display the values on the LCD
 sprintf(databuff,"CV:%3d Cnm:%3d",sensorValueTest,i);  
 sprintf(dispbuff,"%-16s",databuff);
 mySerialPort.print(dispbuff);

 sprintf(databuff,"XV:%3d Xnm:%3d",oldTest, peaknm);
 sprintf(dispbuff,"%-16s",databuff);
 mySerialPort.print(dispbuff);



 writeData();
 i++;

 // If we've reached the upper limit of 700 nm, play a little melody  
 if (i>700)
 {
   for (int f = 0; f<=100; f++)
   {
     tone(7,196,notelen);
     delay(dlx);

     tone(7,131,notelen);
     delay(dlx);

     tone(7,261,notelen);
     delay(dlx);

     tone(7,330,notelen);
     delay(dlx);

     tone(7,294,notelen);
   }
   delay(10000);
 }
}



void writeData()
{
 int quotient = i/256;
 int mod = i % 256;

 EEPROM.write(addr++,quotient);
 EEPROM.write(addr++,mod);

 quotient = sensorValueTest/256;
 mod = sensorValueTest % 256;
 EEPROM.write(addr++,quotient);
 EEPROM.write(addr++,mod);
}



void makeColor(int lambda)
{
 if (lambda >= 350 && lambda <= 439)
 {
   fRed    = -(lambda - (float)440.0) / ((float)440.0 - (float)350.0);
   fGreen = (float)0.0;
   fBlue = (float)1.0;
 }
 else if (lambda >= (float)440.0 && lambda <= (float)489.0)
 {
   fRed = 0.0;
   fGreen = (lambda - (float)440.0) / ((float)490.0 - (float)440.0);
   fBlue = 1.0;
 }
 else if (lambda >= (float)490.0 && lambda <= (float)509.0)
 {
   fRed = 0.0;
   fGreen = 1.0;
   fBlue = -(lambda - (float)510.0) / ((float)510.0 - (float)490.0);

 }
 else if (lambda >= (float)510.0 && lambda <= (float)579.0)
 {
   fRed = (lambda - (float)510.0) / ((float)580.0 - (float)510.0);
   fGreen = 1.0;
   fBlue = 0.0;
 }
 else if (lambda >= (float)580.0 && lambda <= (float)644.0)
 {
   fRed = 1.0;
   fGreen = -(lambda - (float)645.0) / ((float)645.0 - (float)580.0);
   fBlue = 0.0;
 }
 else if (lambda >= 645.0 && lambda <= 780.0)
 {
   fRed = 1.0;
   fGreen = 0.0;
   fBlue = 0.0;
 }
 else
 {
   fRed = 0.0;
   fGreen = 0.0;
   fBlue = 0.0;
 }

 if (lambda >= 350 && lambda <= 419)
 {
   Factor = 0.3 + 0.7*(lambda - (float)350.0) / ((float)420.0 - (float)350.0);
 }
 else if (lambda >= 420 && lambda <= 700)
 {
   Factor = 1.0;
 }
 else if (lambda >= 701 && lambda <= 780)
 {
   Factor = 0.3 + 0.7*((float)780.0 - lambda) / ((float)780.0 - (float)700.0);
 }
 else
 {
   Factor = 0.0;
 }
 iR = factorAdjust(fRed, Factor, MaxIntensity, Gamma);
 iG = factorAdjust(fGreen, Factor, MaxIntensity, Gamma);
 iB = factorAdjust(fBlue, Factor, MaxIntensity, Gamma);
}

int factorAdjust(float C, float Factor, int MaxIntensity, float Gamma)
{
 if(C == 0.0)
 {
   return 0;
 }
 else
 {
   return (int) round(MaxIntensity * pow(C * Factor, Gamma));
 }
}


UKHeliBob

The sprintf commands are not actually doing the printing, they are putting data into a buffer ready to be printed.

Can you print text to your LCD with command such as
Code: [Select]
  lcd.print("Hello, world!");
If so, then all you need to do in the sketch is to initialise the LCD for use with I2C and replace the
Code: [Select]
  mySerialPort.print(dispbuff); command with
Code: [Select]
lcd.print(dispbuff);

Sample code for use with an I2C LCD
Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd

  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
}

void loop()
{
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

kd7eir

#2
Feb 19, 2013, 04:15 pm Last Edit: Feb 19, 2013, 04:56 pm by kd7eir Reason: 1
Yes, lcd.print("Hello, world!"); works just fine.  I will make the changes that you recommend and let you know how it goes.  Thank you.

kd7eir

That worked perfectly, thank you!  Always a good day when I get help making something work and learn why in the process.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy