Pages: [1]   Go Down
Author Topic: RESOLVED I2C instead of serial  (Read 1574 times)
0 Members and 1 Guest are viewing this topic.
Tucson, AZ
Offline Offline
Sr. Member
****
Karma: 8
Posts: 317
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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));
  }
}
« Last Edit: February 19, 2013, 06:54:16 pm by kd7eir » Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 116
Posts: 4313
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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:
  mySerialPort.print(dispbuff);
command with
Code:
lcd.print(dispbuff);

Sample code for use with an I2C LCD
Code:
#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()
{
}
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Tucson, AZ
Offline Offline
Sr. Member
****
Karma: 8
Posts: 317
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 19, 2013, 10:56:28 am by kd7eir » Logged

Tucson, AZ
Offline Offline
Sr. Member
****
Karma: 8
Posts: 317
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: