Go Down

Topic: Can an LCD on a Mega work on theses pins? (Read 88 times) previous topic - next topic

Kinetic

hi can any one tel me if this should work.

Code: [Select]
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(45, 44, 6, 43, 9, 42);  // here is where you tell it what pins you want to use
void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.print("hello, world!");
}


Unfortunatly i am already using the normal pins for an SD card. my full code for my project is below. It is a data logger.

In my full code all i am trying to do is print some test text as the start for the time being.

Code: [Select]

#include <LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
#include <RTClib.h>
#include <Wire.h>


const int analogIn = A0;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
LiquidCrystal lcd(45, 44, 6, 43, 9, 42);
int switchState = 0;

                                  // how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000        // mills between entries (reduce to take more/faster data)

                                  // how many milliseconds before writing the logged data permanently to disk
                                  // set it to the LOG_INTERVAL to write each time (safest)
                                  // set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
                                  // the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000        // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0;            // time of last sync()

#define ECHO_TO_SERIAL   1         // echo data to serial port
#define WAIT_TO_START    0         // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

// The analog pins that connect to the sensors
#define gateOpenpin 22                  // digital pin 22
#define gatePhotopin 23                // digital pin 23
#define gateMotoropen 24                // digital pin 24
#define gateMotorClose 25              // digital pin 25


//int lastPhotoState = LOW;
//int lastOpenState = LOW;



RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println();
  lcd.print("LCD Test");
 
  // use debugging LEDs
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
  pinMode(gateOpenpin, INPUT);
  pinMode(gatePhotopin, INPUT);
  pinMode(gateMotoropen, INPUT);
  pinMode(gateMotorClose, INPUT);
 
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(10, 11, 12, 13)) {
    error("Card failed, or not present");
  }
 
  Serial.println("card initialized.");
  Serial.println("GateLogger By Kinetic Automation"); // company branding
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
 
  logfile.println("Site Address:,7 Ha'penny Dell Waterlooville "); // Type in the site address hear
  logfile.println("Site ID, 001:");
  logfile.println(" Date,  Time,  LOG ENTRY");   
#if ECHO_TO_SERIAL
  Serial.println("  Date  ,   Time  , LOG ENTRY");
#endif //ECHO_TO_SERIAL
 
}


void loop(void)
{
  DateTime now;

  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 
  digitalWrite(greenLEDpin, HIGH);
 
  // fetch the time
  now = RTC.now();
  logfile.print(now.day(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.year(), DEC);
  logfile.print(" ");
  logfile.print(", ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
 
 


#if ECHO_TO_SERIAL
 
  Serial.print(now.day(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.year(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);

 
#endif //ECHO_TO_SERIAL

   switchState = digitalRead(gateOpenpin);
 
  if (switchState == HIGH)
  {
    logfile.print(", ");
    logfile.print("-OPEN-");
    Serial.print("-OPEN-");
  }
 
   switchState = digitalRead(gatePhotopin);
 
  if (switchState == HIGH)
  {
    logfile.print(", ");
    logfile.print("-Photo Cell-");
    Serial.print("-Photo Cell-");
  }
 
  switchState = digitalRead(gateMotoropen);
 
   if (switchState == HIGH)
  {
    logfile.print(", ");
    logfile.print("-Gate Opening-");
    Serial.print("-Gate Opening-");
  }
 
static int timerRunning = 0;
static int startTime = 0;
static int endTime = 0;
static int elapsedTime = 0;
   
   if (timerRunning == 0 && digitalRead(gateMotoropen) == HIGH){
startTime = millis(); //  micros() for really short events
timerRunning = 1;
 }

if (timerRunning == 1 && digitalRead (gateMotoropen) == LOW){
endTime = millis();
timerRunning = 0; // reset for next capture
elapsedTime = endTime - startTime;

if (elapsedTime >= 10000)
{
Serial.print ("Gate Fully Open");
}
else
{
Serial.print ("-Obstacle Detect on Opening ");
}
Serial.print ("(Time for Gate to Open = ");
Serial.print (elapsedTime);
Serial.print ("mS)");
logfile.print(", ");
logfile.print(elapsedTime);
}


switchState = digitalRead(gateMotorClose);
   
   if (switchState == HIGH)
  {
    logfile.print(", ");
    logfile.print("-Gate Closing-");
    Serial.print("-Gate Closing-");
  }
   
static int timer1Running = 0;
static int startTime1 = 0;
static int endTime1 = 0;
static int elapsedTime1 = 0;

   if (timer1Running == 0 && digitalRead(gateMotorClose) == HIGH){
startTime1 = millis(); //  micros() for really short events
timer1Running = 1;
 }

if (timer1Running == 1 && digitalRead (gateMotorClose) == LOW){
endTime1 = millis();
timer1Running = 0; // reset for next capture
elapsedTime1 = endTime1 - startTime1;

if (elapsedTime1 >= 10000)
{
Serial.print ("Gate Fully Closed");
}
else
{
Serial.print ("-Obstacle Detect on Closing ");
}
Serial.print ("(Time for Gate to Close = ");
Serial.print (elapsedTime1);
Serial.print ("mS)");
logfile.print(", ");
logfile.print(elapsedTime1);
}

 RawValue = analogRead(analogIn);
 Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);
 
 Serial.print("-(Aux Current = "); // shows the voltage measured
 Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
 Serial.print("Amps)");
 delay(1000);
 
 
 logfile.print(", ");
 logfile.print(Voltage,3);
 logfile.print(", ");
 logfile.print(Amps,3);
 
  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
 
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
 
  }



Grumpy_Mike

It should do. There is nothing special about an LCD interface, you can use any pins.

Kinetic

The LCD lights up and i can see the segments that would display the text but no text. I now this should just work but it dosnt. Im stumped.


Grumpy_Mike

In which case you are doing something wrong. What is hard to tell because you tell us little apart from the pins you are using.

Kinetic

Hi Grumpy Mike,

Yes i am clearly doing something wrong, i do appreciate the help but how about telling me what infomation you would like. i have supplied the code, pins and a small bit of background.

Im not sure what else i can tell you, like you said "There is nothing special about an LCD interface"

Delta_G

Hi Grumpy Mike,

Yes i am clearly doing something wrong, i do appreciate the help but how about telling me what infomation you would like. i have supplied the code, pins and a small bit of background.

Im not sure what else i can tell you, like you said "There is nothing special about an LCD interface"

You could show us how you have it wired.  You could give us the specifics on which LCD you have.  Tell us what else is connected.  Basically, you want to give us enough information that we could exactly replicate what you have there so we would be able to test it ourselves. 
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

Kinetic

hi Delta_G,

Thank you for your reply I will sort that infomation out now thank you.

Kinetic

Hi all,

The LCD is a LCM1602C, I have attached pictures below.

The project is a data logger that uses an adafruit sheild with a built in SD card logging, this sheild used pins 10,11,12,13 as you can see the the pins conflict with the LCD default pins this is why they have been changed to 45, 44, 6, 43, 9, 42.

I have tried this basic code and it also dose not work.
Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(45, 44, 6, 43, 9, 42);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println();
  lcd.print("LCD Test");
}


My full project is above. pictures to come as post is to big for upload.

Kinetic

Please see pictures

Grumpy_Mike

Quote
You could show us how you have it wired.
I was hoping for a schematic.  :(
With a schematic we can instantly see how your software relates to your hardware.

You are best removing all the other stuff and just connecting the LCD on these pins and write some simple software that just prints out a message to the LCD.

Have you read How to use this forum it will tell you how to ask a question correctly.

cattledog

I'm not sure about the complete wiring, but I can see that the four data lines from mega pins 6,43,9,42 are connected to the wrong pins on the lcd. They should go to D4-5-6-7 on the lcd. You have them connect to D0-1-2-3. Take a look at the wiring diagram in the "Hello World"tutorial.

Your 4 data wires should be going to the side of the display closer to the backlight connection and not the side with the gnd/power/contrast/RS/E.

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