LCD is not working with my Mega 2560 board and a 10k potentiometer and a sg90.

Hi, I coded my Arduino mega 2560 board along with a 4*4 keypad, a LCD, a sg90 servo, and a ten k potentiometer. My servo will not display any values no matter what I do. Any ideas?
Code is posted in the attachments.

next_keypad_test.ino (1.34 KB)

If You use code tags, </> symbol up to the left in this window several more helpers will be able to read the code.
Tablets, smartphones etc. have no IDE. Other helpers don't want their equipment filled up with code from members.

Split the proj into parts. Make the displlay work and print out "Hello World". Then make a piece of code swining the servo up and down. Then the keyboard, then the pot. Tht is called "System intgration".
Throwing all the stuff into the sack, shaking it, is like creating a birdnest of trouble.

What parts work alone?

Sorry about that, I was trying to say something in the LCD, not the servo. Everything else but the LCD works alone.

My program should make the LCD say the customKey whenever a button is pressed but that is not happening.

@OP

Are you expecting your system/project to behave as?:

When the entered password is 123, only then the * and # will do their jobs; L (built-in LED of MEGA connected at DPin-13) and Servo will be activated. If so, upload the following sketch (tested partly using MEGA, Keypad, L and SG90); check the functioning of the system; try to understand the extra program logic that has been placed in your sketch.

#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>

const byte ROWS = 4;
const byte COLS = 4;

int servoPin = 4;
Servo Servo1;

char password[] = "123";
char myPassword[4] = "";
int i = 0;

char hexaKeys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {25, 24, 27, 26};
byte colPins[COLS] = {29, 28, 31, 30};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(50, 51, 46, 47, 48, 49);
bool flag = false;

void setup()
{
  Servo1.attach(4);
  lcd.begin(16, 2);

  lcd.print("test");
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop()
{
  //sets the lcd at column 1, row 1.
  lcd.setCursor(1, 1);
  char customKey = customKeypad.getKey();
  if (customKey)
  {
    Serial.println(customKey);
    lcd.print(customKey);
    if (flag == false)
    {
      myPassword[i] = customKey;
      i++;
      if (i == 3)
      {
        myPassword[i] = '\0'; //insert null charctaer
        if (strcmp(myPassword, password) == 0)
        {
          Serial.println("good job");
          i = 0;
          flag = true;
        }
      }
    }
  }
  if (flag == true)
  {
    if (customKey != '*' || customKey != '#')
    {
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);
    }

    if (customKey == '*' || customKey == '#')
    {
      Servo1.write(125);
      digitalWrite(12, LOW);
      digitalWrite(13, HIGH);
      delay(1000);
    }
  }
}

smh.png

smh.png

golemmostafa,
the program does work the way I wanted to, I just have a couple of questions.

  1. What does the strcmp part of the program do? Like I read the comments just it does not make much sense.
  2. What is the purpose of a null character being inserted into the program?
  3. Also, I added this line of code into the program:
    lcd.print("good job"); into the strcmp if loop statement.
    Essentially I want it to print out good job on the liquid crystal display, but however it is not working. Any ideas?

I see you use the LiquidCrystal library, that with the rest of the code implies a 1602 display.

First thing to check is whether you set the contrast pot correctly. Turn it one way, and you should see dark blocks on the display. Turn it the other way until the dark blocks just disappear - now you should be able to read the text, if any.

So whenever I plug in the Arduino and the LCD turns on, I always turn the potentiometer both left and right, and the LCD does not change. I added a line that should tell the LCD to print out "Access Granted", but that not does work.

Code:

#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal.h>

const byte ROWS = 4;
const byte COLS = 4;

int servoPin = 4;
int myArray[3];
Servo Servo2;
int i = 0;
char password[] = "123";
char myPassword[4] = "";
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {25, 24, 27, 26};
byte colPins[COLS] = {29, 28, 31, 30};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(50, 51, 46, 47, 48, 49);

bool flag = false;

void setup(){
Servo2.attach(4);
lcd.begin(16, 2);

lcd.print("test");
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);

}

void loop(){
//sets the lcd at column 1, row 1.
lcd.setCursor(1, 1);
char customKey = customKeypad.getKey();

if (customKey)
{
Serial.println(customKey);
lcd.print(customKey);
if (flag == false)
{
myPassword = customKey;

  • i++;*
  • if (i ==3)*
  • {*
    _ myPassword = '\0';_
    * if(strcmp(myPassword, password) == 0)*
    * {*

* Serial.println("Access Granted");*
* lcd.print("Access Granted");*
* Servo2.write(125);*
* digitalWrite(12, LOW);*
* digitalWrite(13, HIGH);*
* delay(5000);*
* i = 0;*
* flag = true;*
* }*
* }*
* }*
* }*

if (customKey != '*' || customKey != '#'){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
}

}

I did that and the LCD does not respond.

/*
LiquidCrystal Library - Hello World

Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

This sketch prints "Hello World!" to the LCD
and shows the time.

The circuit:

  • LCD RS pin to digital pin 50
  • LCD Enable pin to digital pin 51
  • LCD D4 pin to digital pin 46
  • LCD D5 pin to digital pin 47
  • LCD D6 pin to digital pin 48
  • LCD D7 pin to digital pin 49
  • LCD R/W pin to ground
  • LCD VSS pin to ground
  • LCD VCC pin to 5V
  • 10K resistor:
  • ends to +5V and ground
  • wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi

This example code is in the public domain.

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 50, en = 51, d4 = 46, d5 = 47, d6 = 48, d7 = 49;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

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!");
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}

Also do post a schematic (no, Fritzing doesn't make schematics, that's wall art, we need a REAL schematic).

@OP

1. Check that the LCD is connected with MEGA as per following diagram (Fig-1).
LCDPMega.png
Figure-1:

2. A setup similar to above works fine with UNO. This is to check that the jumpers wires are OK. This is the sketch that I uploaded (an IDE example with slight change).

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);//rs, en, d4, d5, d6, d7);

void setup() 
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);  //DP0-position of TopLine
  lcd.print("hello, world!");
}

void loop() 
{
  
}

3. Anyway, I could not make the setup of Fig-1 working with MEGA even after changing the DPin connections. (This is the first time I have tried parallel LCD with MEGA.)

4. An I2C LCD works with MEGA just like a clock.

LCDPMega.png

Before I try any of these suggestions, my LCD digital pins are plugged into the digital pin area, should they be plugged into a different area?

No idea; better you go with an I2CLCD if you have.

Guys I am really sorry about you guys taking this time to help me, but I just realized that I hooked my breadboard up wrong. I changed it, and it works perfectly. Sorry, but thanks for the extensive and thorough help.