LCD contrast, gibberish and auto vertical scroll issues. I2c with RTC clock.

Hello fellow Forumers,

I have got two sketches.
the first one is running fine and I dont have any gibberish or contrast problems or what so ever.

// constants won't change. They're used here to 
// set pin numbers:
#include <Servo.h>
#include <LiquidCrystal.h>

const int upButtonPin = 9;     // the number of the pushbutton pin
const int downButtonPin =  8;
Servo myServo;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int gearNumber = 0;
int upButtonState = 0;
int downButtonState = 0;
int pos = 0;

void setup() {
  // initialize the LED pin as an output:
  lcd.begin(16, 2);
  myServo.attach(10);

  // initialize the pushbutton pin as an input:
  pinMode(upButtonPin, INPUT);     
  pinMode(downButtonPin, INPUT); 
}

void loop(){
  // read the state of the pushbutton value:
  upButtonState = digitalRead(upButtonPin);
  downButtonState = digitalRead(downButtonPin);
  lcd.setCursor(0, 1);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (upButtonState == HIGH) {
    if ( (gearNumber >= 0) && gearNumber <= 5){
      gearNumber = gearNumber + 1;
      delay(400);
//      gearNumber = gearNumber + 1;

      switch(gearNumber){
       case 1: 
          lcd.print("gear 1");
          myServo.write(0);
          break;
         
       case 2:
          lcd.print("gear 2");
          myServo.write(36);
          break;
         
       case 3:
           lcd.print("gear 3");    
           myServo.write(72);
           break;
         
       case 4:
          lcd.print("gear 4");
          myServo.write(108);
          break;
         
       case 5:
          lcd.print("gear 5");
          myServo.write(144);
          break;
         
       case 6:
          lcd.print("gear 6");
          myServo.write(180);
          break;
      }

    }
  }
  
  //DOWN SHIFT
  if (downButtonState == HIGH) {
    if (gearNumber >= 1){
      gearNumber = gearNumber - 1;
      delay(400);
//      gearNumber = gearNumber + 1;

      switch(gearNumber){
       case 6: 
         lcd.print("gear 6");
         myServo.write(180);
         break;
         
       case 5:
          lcd.print("gear 5");
          myServo.write(144);
         break;
         
       case 4:
          lcd.print("gear 4");
          myServo.write(108);
         break;
         
       case 3:
           lcd.print("gear 3");
           myServo.write(72);
         break;
         
       case 2:
           lcd.print("gear 2");
           myServo.write(36);
         break;
         
       case 1:
           lcd.print("gear 1");
           myServo.write(0);
         break;
      }
    }
  }
}

The second one is tutorial on this website: http://www.dummies.com/how-to/content/how-to-display-the-time-for-your-arduino-clock-pro.html

The sketch I made with this tutorial:

// Chapter 7: Arduino Alarm Clock
// An alarm clock that uses the Adafruit Industries DS1307 RTC Breakout board
// and a 16x2 Parallel LCD Display
#include <Wire.h>  // I2C Wire Library for communicating with the DS1307 RTC
#include "RTClib.h" // Date and time functions for the DS1307 RTC connected
#include <LiquidCrystal.h> // Display functions for the LCD Display
RTC_DS1307 rtc;       // Create a realtime clock called rtc
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an LCD called lcd
DateTime now;

void setup () {
 Wire.begin();  // Enables the communication for the LCD
 rtc.begin();  // Enables the RTC
 lcd.begin(16, 2); // Enables the LCD


}
void loop(){
  now = rtc.now(); //Get the current time
  updateDisplay();
}
void updateDisplay(){
   int h = now.hour(); // Get the hours right now and store them in an integer called h
 int m = now.minute(); // Get the minutes right now and store them in an integer called m
 int s = now.second(); // Get the seconds right now and store them in an integer called s
 lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row...
 lcd.print(" The time is: "); // ...with spaces to clear characters from setting alarm.
 lcd.setCursor(4, 1); // Move the cursor to column four, lower row
 if (h<10){   // Add a zero, if necessary, as above
  lcd.print(0);
 }
 lcd.print(h);   // Display the current hour
 lcd.setCursor(6, 1); // Move to the next column
 lcd.print(":");  // And print the colon
 lcd.setCursor(7, 1); // Move to the next column
 if (m<10){   // Add a zero, if necessary, as above
  lcd.print(0);
 }
 lcd.print(m);   // Display the current minute
 lcd.setCursor(9, 1); // Move to the next column
 lcd.print(":");  // And print the colon
 lcd.setCursor(10, 1); // Move to the next column
 if (s<10){   // Add a zero, if necessary, as above
  lcd.print(0);
 }
 lcd.print(s);   // Display the current second
 }

The problem with this sketch is that I can see “The time is” and the “current time” being written on my lcd. The problem is that the letters are not in white (white on black lcd screen) but stay dark but visible looking in a angle on the screen. And there is a bar going from bot to top erasing the dark letters like a lcd screen out of sink with 25 fps.

The wiring from the lcd and the rtc are all correct because I double checked them and with the upper sketch it works fine.

Also what I notice between the two sketches is that the contrast potentiometer for each sketch is different. I mean that I hav to rotate the potentiometer nearly 360 degrees to get in the right contrastratio.

How is this possible and how do I fixx this?

You might be writing new characters to your LCD too fast. Loop() is quite short and is repeated over and over again. Reading the RTC is probably what takes the most time. To get rid of that, and only for this debugging, add a delay to loop(). Something like this:

void loop(){
  now = rtc.now(); //Get the current time
  updateDisplay();
  delay(500);
}

This way your display will be updated less than twice per second. In case your display does what you expected it to do, you can work out a more elegant solution. In that case i would only write a new value to the display if that value has changed from the previous value (which is the correct way to get rid of any blocking delays).

Which Arduino board do you have? If you have a Leonardo, you will need to use pins other than 2 and 3 as those are the i2c pins on that board.

--- bill

Hi mas3

The delay worked a little bit thank you. But the screen stays gibberish and it does print the time but it's not counting. Also if I upload the "it's a live" sketch. then It's is only displayed on the far right of the screen.

I think the code is to complex or something because my other sketch works fine.

bperrybap: I have a mega and a uno.

tnx for the help so far:)

JK

I uploaded our sketch onto an UNO I have set up with a ds1307 rtc and a parallel lcd display and it runs fine.

My lcd constructor was different (LiquidCrystal lcd(8, 9, 4, 5, 6, 7):wink: because of how my board is wired.

I don’t think your issue is software, but likely to be some sort of pin conflict. The first sketch does not use the rtc, and it runs. How is the rtc connected. I’d follow Bill’s suggestion and take a close look for pin conflicts.

Also, you can check out the RTC through serial using the ds1307 example in the RTClibrary.

After trying an alternative display wiring,

It still might be a timing issue. When you are printing to the screen, you are constantly setting the cursor to the next position, and that takes some time. There is no need to do that. Only if you are skipping positions or go back, you need to set the cursor. If you print a single character to the screen, the cursor automatically jumps to the next position. So the setting of the cursor in your sketch is in most instances unnecessary, and just costs some time. I'd remove all of these setCursors that are not absolutely need-be. If only for the cleaner code.

Also, check for a potential hardware problem. The display is held to place with a (most of the time black) bezel. Gently press that bezel with your fingers on some different spots, while the sketch is running. If you see any improvement while doing that, try to find the "sweet spot". Once you found that, remember it and turn the display around to see the back of it. You will find probably 6 tabs of that bezel coming through the PCB. Gently rotate the one that is closest to the "sweet spot" you just found, and rotate it inward. See if you problem can be solved this way. If not, but you did see improvement while pressing the bezel, get yourself a new display they aren't that expensive. Once you got that, you can do some closer study to the old display (no matter if that totally destroys it, as you already have the new one.