Hello all. First, being a newbie, I appreciate all the patience and info given lately. I create one issue, ask for guidance and eventually solve the problem. I am trying to add a 20x4 LCD display to my project. I have it wired as 5v, grd, SDA to pin 20 and SDL to pin 21 on a mega 2560 board. If I download the test code as shown here- the LCD display works as it should.
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Hello, world!");
lcd.setCursor(2,1);
lcd.print("Ywrobot Arduino!");
lcd.setCursor(0,2);
lcd.print("Arduino LCM IIC 2004");
lcd.setCursor(2,3);
lcd.print("Power By Ec-yuan!");
}
void loop()
{
}
However, in my project the LCD display does not work. I've looked at the coding until I'm blind and can't see why it would not work. Any ideas? Here is the project coding below.
``
#include <LiquidCrystal_I2C.h> // LiquidCrystal I2C - Version: Latest
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,20,4); // double check your LCD address first using: i2C_scanner
int analogOutPin1 = A1; // finish line beam lane 1 on A1 PIN
int analogOutPin2 = A2; // finish line beam lane 2 on A2 PIN
const int ledPin1 = 11; //lane 1 winning LED on D11 PIN
const int ledPin2 = 12; //Lane 2 winning LED in D12 PIN
int StartSwitch = 2;
int sensorValueFinish1 = 0;
int sensorValueFinish2 = 0;
int StartState = 0;
int sensorThresh = 500; //Sets the trigger sensing threshold of the IR receivers. ~1000 = high
unsigned long timeLane1; //finish line lane 1
unsigned long timeLane2; //finish line lane 2
float StartTime = 0;
float ET1 = 0;
float ET2 = 0;
void setup()
{
Serial.begin(9600);
pinMode(StartSwitch, INPUT);
pinMode(ledPin1, OUTPUT); //lane 1
pinMode(ledPin2, OUTPUT); //lane 2
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
StartTime = 0;
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("HotWheelsTimerV2");
lcd.setCursor(0,1);
lcd.print("Ready to race...");
}
void loop()
{
StartState = digitalRead(StartSwitch); //Check state of start gate. 0 = closed & 1 = open.
if(StartState == 0)
{
StartTime = 0;
ET1 = 0;
ET2 = 0;
}
StartState = digitalRead(StartSwitch); //if start gate is trigger, race started. 1 = open.
if(StartState == 1 && StartTime == 0)
{
StartTime = micros(); //use micro seconds since the races can be that close!
}
//read the analog in value of IR's:
sensorValueFinish1 = analogRead(analogOutPin1);
sensorValueFinish2 = analogRead(analogOutPin2);
// remove "//" from below for Debugging and checking actual sensor values.
// You may have to adjust sensorThresh value for better accuracy.
// The sensors should read around 900-1000 then go "low" (less than 400) when a car passes through the beam.
//Serial.println(StartTime);
// Serial.println(StartState);
// Serial.print("Sensor Finish1 = " );
// Serial.println(sensorValueFinish1);
// Serial.print("Sensor Finish2 = " );
// Serial.println(sensorValueFinish2);
// delay(50);
// wait/check for the Finish Line sensors to be triggered
if(sensorValueFinish1 < sensorThresh && ET1 == 0)
{
timeLane1 = micros();
ET1 = timeLane1 - StartTime;
ET1 = ET1 / 1000000;
}
if(sensorValueFinish2 < sensorThresh && ET2 == 0)
{
timeLane2 = micros();
ET2 = timeLane2 - StartTime;
ET2 = ET2 / 1000000;
}
if (ET1 < ET2 && ET1 != 0 && ET2 != 0)// Set winner Lane 1, turn on winner LED
{
digitalWrite(ledPin1, HIGH);
}
if(ET1 > ET2 && ET2 != 0 && ET1 != 0) // Set winner Lane 2, turn on winner LED
{
digitalWrite(ledPin2, HIGH);
}
if(ET1 > 0 && ET2 > 0) // Race is over, display times for both lanes. // both cars have to finish to display times.
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("L1" );
lcd.setCursor(4,0);
lcd.print("ET:");
lcd.setCursor(6,0);
lcd.print(ET1, 4);
if (digitalRead(ledPin1) == HIGH)
{
lcd.setCursor(12,0);
lcd.print("*WIN*");
}
lcd.setCursor(0,1);
lcd.print("L2" );
lcd.setCursor(4,1);
lcd.print("ET:");
lcd.setCursor(6,1);
lcd.print(ET2, 4);
if (digitalRead(ledPin2) == HIGH)
{
lcd.setCursor(12,1);
lcd.print("*WIN*");
}
delay(8000);
digitalWrite(ledPin1, LOW); //turn off lane winner LED
digitalWrite(ledPin2, LOW); //turn off lane winner LED
timeLane1= 0;
timeLane2= 0;
StartTime = 0;
ET1 = 0;
ET2 = 0;
}
}