carduinoarduino-idelcdmotordriver

Why is code for Arduino LCD messing up code for Stepper Motor?


I have a basic code for a stepper motor which is shown below, but for some reason whenever I try to add the code for my 2 LCDs it messes everything up. The output to the motor just keeps firing over and over again with no delay, and the LCDs dont even print anything out. What I am doing wrong?

Simple Stepper code (This is the working one)

#define IN1  52
#define IN2  50
#define IN3  48
#define IN4  46
int Steps = 4096; //4096 or 768
int cstep = 0;

void setup()
{
    Serial.begin(9600);
    pinMode(IN1, OUTPUT); 
    pinMode(IN2, OUTPUT); 
    pinMode(IN3, OUTPUT); 
    pinMode(IN4, OUTPUT); 
}

void loop()
{
    for(int x=0; x<Steps; x++)
    {
        step1();
        delayMicroseconds(2500);
    }
    Serial.println("Boom!!");
    delay(1000);
}

void step1()
{
    //stepp
    switch(cstep)
    {
        case 0:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, HIGH);
             break; 

        case 1:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, HIGH);
             break; 

        case 2:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, LOW);
             break; 

        case 3:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, HIGH);
             digitalWrite(IN4, LOW);
             break; 

        case 4:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 5:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, HIGH);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 6:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 

        case 7:
             digitalWrite(IN1, HIGH); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, HIGH);
             break; 

        default:
             digitalWrite(IN1, LOW); 
             digitalWrite(IN2, LOW);
             digitalWrite(IN3, LOW);
             digitalWrite(IN4, LOW);
             break; 
    }
    cstep=cstep+1;
    if (cstep==8)
    {
       cstep=0;
    }
}

Stepper with LCD Code (The Broken One)

#include <LiquidCrystal.h>

#define IN1  52
#define IN2  50
#define IN3  48
#define IN4  46
int Steps = 4096; //4096 or 768
int cstep = 0;

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
LiquidCrystal lcd2(14, 9, 10, 11, 12, 13);

void setup(){
  lcd.begin(16,2);
  lcd2.begin(16,2);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
  Serial.begin(9600);
}

void loop()
{
    for (int x=0;x< Steps; x++)
    {
        step1();
        //delay(1);
        delayMicroseconds(2500);
    }
    Serial.println("Boom!!");
    delay(1000);
}

    void step1()
    {
        switch(cstep)
        {
            case 0:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 1");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 1:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 2");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 2:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 3");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print(".");
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 3:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 4");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print("."); 
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 4:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 5");
                 lcd2.print("Working");
                 digitalWrite(IN1, LOW);
                 lcd2.print(".") ;
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 5:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 6");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print(".") ;
                 digitalWrite(IN2, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 6:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 7");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print(".");
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, LOW);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

            case 7:
                 lcdClear();
                 lcd2Clear();
                 lcd.print("Step 8");
                 lcd2.print("Working");
                 digitalWrite(IN1, HIGH);
                 lcd2.print("."); 
                 digitalWrite(IN2, LOW);
                 lcd2.print(".");
                 digitalWrite(IN3, LOW);
                 lcd2.print(".");
                 digitalWrite(IN4, HIGH);
                 lcd2Clear();
                 lcd2.print("Completed");
                 break; 

           default:
                 digitalWrite(IN1, LOW); 
                 digitalWrite(IN2, LOW);
                 digitalWrite(IN3, LOW);
                 digitalWrite(IN4, LOW);
                 break; 
       }
       cstep = cstep+1;
       if (cstep==8)
       {
           cstep=0;
       }
   }

   void lcdClear(){
       lcd.setCursor(0,0);
       lcd.clear();
   }

   void lcd2Clear(){
       lcd2.setCursor(0,0);
       lcd2.clear();
   }

Don't know if this helps but my board is a Mega 2560-R3


Solution

  • PIN 1 is comunication pin, not Digital. It's TX0. I think you shouldn't use it. PIN 14 for the second display is TX1. It should be again Digital. Try changing them:)