Hey Guys
Newbie!
I am beginning a project to have my uno control an aquarium. I plan on doing this by learning each component and writing code to drive that compnent. Then hopefully by the end i would of learnt enough to tie it all together. The first part of this is using an sainsmart I2C LCD and a sparkfun ds1307 clock.
I have found example code for each device and i am trying to understand the code in its entirety. I have some questions on the RTC code that is tied to this code, but i will start with this one!
I think i understand most of this code, apart from the bit starting with define and the line
" LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin)"
So its a standard screen with the sainsmart i2c adaptor on the back. i have a non i2c version of this screen and have got that working no problem, what is the define part of this code exactly doing. Is it simply renaming pin numbers with a name? I understand the above line is defining what pins are used on the lcd. the same as if the screen was connected directly to the arduino without the i2c adaptor. Infact i cant see much difference between the code connecting the lcd directly to the uno with all the wires and using the i2c.
/*
** Example Arduino sketch for SainSmart I2C LCD2004 adapter for HD44780 LCD screens
** Readily found on eBay or http://www.sainsmart.com/
** The LCD2004 module appears to be identical to one marketed by YwRobot
**
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)
**
** sain_lcd_2.ino
** Simplified and modified by Andrew Scott for Arudino 1.0.1, Arudino Uno R3.
** NOTE: I2C pins are A4 SDA, A5 SCL
** Don't forget to use the new LiquidCrystal Library.
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Define I2C Address where the SainSmart LCD is
#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()
{
lcd.begin (20,4);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
// Position cursor and write some text
lcd.home (); // go to the first line, first character
lcd.print("SainSmart I2C tester");
lcd.setCursor ( 0, 1 ); // go to the 2nd line
lcd.print("It's Working!");
lcd.setCursor ( 0, 2 ); // go to the third line
lcd.print("Sainsmarts docs suck");
lcd.setCursor ( 0, 3 ); // go to the fourth line
lcd.print("Nice LCD Though. ");
}
void loop()
{
}
I have been researching I2C and in particular the RTC mentioned above. an example code looks like this:
#include <Wire.h>
int hour;
int minute;
int second;
int month;
int day_of_week;
int day;
int year;
char* dow[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
// Below required to reset the register address to 0.
Wire.beginTransmission(104); // transmit to device #104, the ds 1307
Wire.send(0x00);
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(104, 7); // request 7 bytes from slave ds1307, we'll assume it'll send them all even though it doesn't have to
second = Wire.receive();
minute = Wire.receive();
hour = Wire.receive();
day_of_week=Wire.receive();
day = Wire.receive();
month = Wire.receive();
year = Wire.receive();
// Convert all the BCD values that might have "tens" to decimal. Most arduino folks do this w/shifts but this just looks easier to me.
hour=hour/16 * 10 + hour % 16;
minute=minute/16 * 10 + minute % 16;
second=second/16 * 10 + second % 16;
day=day/16 * 10 + day % 16;
month=month/16 * 10 + month % 16;
year=2000 + year/16 * 10 + year % 16;
Serial.print(hour);
Serial.print(":");
if (minute < 10) { Serial.print("0"); }
Serial.print(minute);
Serial.print(":");
if (second < 10) { Serial.print("0"); }
Serial.print(second);
Serial.print(" ");
Serial.print(dow[day_of_week-1]); // array is 0-6, but the dow register holds 1-7, so subtract 1.
Serial.print(", ");
Serial.print(month);
Serial.print("/");
Serial.print(day);
Serial.print("/");
Serial.print(year);
Serial.print("\n");
delay(1000);
}
i again think i understand most of this code, except the first part of the loop upto the part endtransmission. Why do we not use wire.write for the LCD screen?. its seems we use alot of code to communicate with the clock, ie begintranmission etc, but why do we not use such commands with the lcd?
Kr
Craig