LCD and SD module wont work together please help fix??? ASAP

Im making a project where I collect data and print it both on the lcd and SD card. Im using a sparkfun SD module and the standard LCD 16x2 screen that comes along with the starter kit

When I power the Arduino UNO with the following code the "welcome" message shows on screen but not the rest of the information that are also supposed to be printed in the void loop() function!!

This is my code

#include <SD.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int chipSelect = 8;  //set variables
const int buttonPin = 7;
const int piezoPin = 9;
long duration;
long duration2;
long durationMicros;
long time1;
long time2;
long elapse;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int steps = 0;

void setup(){
  
  Serial.begin(9600);
  lcd.begin(16 , 2);
  pinMode(buttonPin , INPUT);
  pinMode(piezoPin ,OUTPUT);
  pinMode(10 , OUTPUT);
  lcd.print("welcome");
  delay(1000);
  lcd.clear();
  
  Serial.println("Initializing SD card....."); 
  
  if(!SD.begin(chipSelect)){ // check is SD card is present
    
    Serial.println("card failed or not present");
    return;
  }
    Serial.println("card initialized");
    SD.mkdir("datalog.csv"); // create a file named datalog.csv
    
    if(SD.exists("datalog.csv")== true){ //check if the file is created succesfully
      
      Serial.print("file created");
      Serial.println();
    }
    else{
      Serial.print("could not create file succesfully");
      Serial.println();
    }
    
  
}

void loop(){

  buttonState = digitalRead(buttonPin);  //read the current state of the button pin
  
  if (buttonState != lastButtonState && buttonState == HIGH) {    
     
    while(buttonState != lastButtonState && buttonState == HIGH) {  //if the button has changed its state and its pressed then..
    
      time1=millis();   //get the current time in milliseconds
      durationMicros = pulseIn(buttonPin,HIGH,300000000);   // time for how long the button is pressed in milliseconds
      duration = durationMicros/1000000;   //do the correct calculation to convert the time in seconds
      buttonPushCounter++;   //increment the counter by 1
      steps=buttonPushCounter*2;  //every time the button is pressed count two steps
      
      Serial.print("steps: ");   //print data in the serial monitor
      Serial.print(steps);
      Serial.print("  duration: ");
      Serial.print(duration);
      
      if(steps>20){      //if the number of steps exceeds 20 then...
        
        lcd.home();
        lcd.print("I'M LOST! HELP!");
        lcd.setCursor(1, 1);
        lcd.print("HOME:");
        lcd.setCursor(5,1);
        lcd.print(steps*0.7);
        lcd.setCursor(9,1);
        lcd.print("m. AWAY");
      
        tone(8,349);
        delay(1000);
        noTone(8);
        delay(3000);
        tone(3,200);
        delay(1000);
        noTone(8);
        delay(3000);    //print a message on the LCD screen an also trigger a sound     
  
   lcd.clear();  //clear the screen
  
    }

      time2 = millis();  //get the current time
      elapse=time2-time1;    //subtract the previous time from the current time to find how long was it between two button pushes
      duration2 = (elapse/1000)-duration;  //find the duration between the 2nd and 3rd step in seconds by subtracting the duration between the 2 first steps.
      Serial.print( " duration2: ");
      Serial.print(duration2);
      Serial.println();  //print the relevant information in the Serial Monitor
      
    
    File dataFile = SD.open("datalog.csv" , FILE_WRITE );   // check if the file is available if yes write to it…
    if (dataFile == true){
      
      dataFile.print("steps: ");
      dataFile.print(steps);
      dataFile.print(" , ");
      dataFile.print(" duration: ");
      dataFile.print(duration);
      dataFile.print(" , ");
      dataFile.print(" duration2: ");
      dataFile.print(duration2);
      dataFile.println();
      dataFile.close();
      
      Serial.println("data logged!!!");
    }
    
    else { //  …if not pop up an error 
      Serial.println("error opening datalog.csv");
    }
    }
    
    
    
    lastButtonState = buttonState;
    
  }
  
    
  }

I would really appreciate your help

... also the lcd with same setup without the .csv file writing works just fine but when adding the code needed to write to the datalog.csv file on the SD card the lcd prints nothing during the void loop() function

Stop posting same question in all subsections, that is not appreciated on this forum.

One possibility is that the SD shield is using the same pins as your LCD. Which shield is it & what do its docs say about the pins it uses?

... but may be able to move pins around to keep lcd happy

There's no 'may' to worry about here.

If you manage to find this page: --> LiquidCrystal - Arduino Reference it is all spelled out.

It's too bad that you really have to do so much detective work to figure out the significance of the LiquidCrystal lcd(12, 11, 5, 4, 3, 2); statement when a simple comment would really do the trick.

//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      // put your pin numbers here

In other words you can use any available Arduino I/O pin to implement any of the LCD data or control lines. All you have to do is put the pin numbers that you decide to use into the LiquidCrystal lcd( . . . ); statement in the proper order.

Don

I know that but thats not the case. Im using a sparkfun SD card module which uses all arduino UNO pins as its on top of it. the lcd then uses pins 12,11,5,4,3,2 that are on top of the module.
When using another code again using the same setup(lcd + SD module) it works perfecly fine but when I upload this code it doesnt print the information I need (steps , duration , duration2) , but instead the screen remains in its default state.

Also the lcd works fine on its own ...

but I thing the problem has to do with the code or something because I think it doesnt execute the

if(steps > 20){

......

}

part, but Im not sure and I dont know how to fix the problem

How do you have the button on pin 7 wired up? Your code indicates that it considers the button to be pushed when it is HIGH. Usually a button is wired so that it is LOW when it is pushed.
Have you got a pullup or pulldown on the pin?

Pete

the button pins and piezo pin are all fine....

the button goes as follows 5v to wire to button to resistor + wire to digital pin 7 but the button isn the problem as ive tested it many times

Where is the ground connection?
All the button appears to be doing is connecting the pin to +5V. In which case it is very likely that your code will never see the pin as anything but HIGH.

Pete

the button is connected to ground with a 220 Ohm resistor

OK, so is the pin grounded (LOW) when you push the button? The comments in your code say that it considers the button to be pushed when the pin is HIGH.

    while(buttonState != lastButtonState && buttonState == HIGH) {  //if the button has changed its state and its pressed then..

Note the "and its pressed".

It would also help if you showed what is printed on the serial monitor before you push the button and then what happens, if anything, while the button is pushed and when it is released.

Pete

when the button is pressed and then released information is printed to the Serial monitor once .Information is printed again when this process is repeated

I'm not a dentist, nor am I a psychic.

Pete

what do you mean by that?

the button is pressed when HIGH and not pressed when low (I used the Arduino projects book as a reference to that)

I think the problem is somewhere on the code as I think it doesn run the following part

 if(steps>20){      //if the number of steps exceeds 20 then...
        
        lcd.home();
        lcd.print("I'M LOST! HELP!");
        lcd.setCursor(1, 1);
        lcd.print("HOME:");
        lcd.setCursor(5,1);
        lcd.print(steps*0.7);
        lcd.setCursor(9,1);
        lcd.print("m. AWAY");
      
        tone(8,349);
        delay(1000);
        noTone(8);
        delay(3000);
        tone(3,200);
        delay(1000);
        noTone(8);
        delay(3000);    //print a message on the LCD screen an also trigger a sound     
  
   lcd.clear();  //clear the screen
  
    }

for some reason the lcd doesnt print the information but in this code it does....

#include <LiquidCrystal.h>;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int  buttonPin = 7;
long duration2;
long time1;
long time2;
long elapse;
long duration;
long durationMicros;
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;    
int steps = 0;

void setup()  {
  
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(8, OUTPUT);
  lcd.begin(16, 2);
  lcd.print("welcome");
  delay(1000);
  lcd.clear();
  
}

void loop(){
 
  buttonState = digitalRead(buttonPin);  
  
  if (buttonState != lastButtonState && buttonState == HIGH) {
    while(buttonState != lastButtonState && buttonState == HIGH) {
      
      time1 = millis();
      buttonPushCounter++;
      durationMicros = pulseIn(buttonPin,HIGH,300000000);
      duration = durationMicros/1000000;
      steps=buttonPushCounter*2;
      
      Serial.print("steps: ");
      Serial.print(steps);
      Serial.print("  duration: ");
      Serial.print(duration);
    
      if(steps > 20){
        lcd.clear();
        lcd.print("I'M LOST! HELP!");
        lcd.setCursor(0, 1);
        lcd.print("HOME:");
        lcd.setCursor(5,1);
        lcd.print(steps*0.4);
        lcd.setCursor(9,1);
        lcd.print("m. AWAY");
      
        tone(8,349);
        delay(1000);
        noTone(8);
        delay(3000);       
  
  
  
  lcd.clear();
  
      } 
      
      time2 = millis(); 
      elapse=time2-time1;    
      duration2 = (elapse/1000)-duration;  
      Serial.print( " duration2: ");
      Serial.print(duration2);
      Serial.println();  
      

  }
  }
  
  lastButtonState = buttonState;  
  


}

and Im also using the SD card module but without actually using the SD card

Extracting information from you is like pulling wisdom teeth.
I asked you "what is printed on the serial monitor" and your response was "information is printed to the Serial monitor".
Well DUH!

FWIW: the code you have just posted (which works?) has this statement:

  lastButtonState = buttonState;

outside the first if statement, whereas your original code (which didn't work?) has it inside the if statement.

Pete

I tried what el_supremo said and the lcd worked but..... another problem apppeared the lcd instead of printing what I needed it to print it prints that.....

FullSizeRender.jpg