I2C LCD interfering with stepper and encoder

Im making a projects that uses a nema 17 stepper and a rotary encoder to control its speed. When i hook up an LCD with the i2c module, the motor keeps spining but i cant regulate the speed with the enocder and on the serial moniter the numbers for the encoder only go down. In the code the lcd parts are not in connection with the motor and encoder part.

As you have not posted your program it is rather difficult to offer advice.

Do you mean that things work properly with the exact same code but with the LCD disconnected whereas they behave differently when the LCD is connected?

...R
Stepper Motor Basics
Simple Stepper Code

No, I mean that without the lcd code it works properly, its when i paste the lcd code it dosent wrk, even if the lcd isn't connected.

Motor_speed_encoder_lcd.ino (2.27 KB)

mousse_raafat:
No, I mean that without the lcd code it works properly, its when i paste the lcd code it dosent wrk, even if the lcd isn't connected.

Then please also post the working program so we can see the changes.

And what exactly does "doesn't work" mean? Give as much detail as you can about what the non-working program actually does and what you want it to do that is different.

...R

A schematic of your project may be useful.

Robin2:
Then please also post the working program so we can see the changes.

And what exactly does "doesn't work" mean? Give as much detail as you can about what the non-working program actually does and what you want it to do that is different.

...R

When I upload the working code: The stepper starts spinning, I twist the knob of the encoder and on the serial motor the value goes up or down depending on the direction, if the value goes up the motor slows down, if it goes down the motor speeds up.

When i upload the code with the lcd part: The stepper moves at a rather slow speed, and when I turn the encoder the speed dosen't change and the values on the serial moniter only go down even if i spin it the other way.

stepper_speed_encoder.ino (1.59 KB)

groundFungus:
A schematic of your project may be useful.

The wire library has blocking I/O, and the lcd is relatively slow to respond.

Do not write to the lcd every pass through loop, but rather put it on a millis() timer as seen in "Blink without delay".

Updating an lcd display more frequently than twice per second is rarely useful.

cattledog:
The wire library has blocking I/O, and the lcd is relatively slow to respond.

Do not write to the lcd every pass through loop, but rather put it on a millis() timer as seen in "Blink without delay".

Updating an lcd display more frequently than twice per second is rarely useful.

If it's not too much work for you can you write me that part of the code. I dont fully understand the "Blinkwithout delay" sketch yet because I'm fairly new to arduino. Or at least orient me in the right direction (not that you didn't already;))

Here is the code from the attachment in reply #2 with the LCD print moved to a function to print to the LCD that runs every 500ms using the Blink WithOut Delay (BWOD) method. See the beginner's guide to millis() and the several things at a time tutorial for more information on using the BWOD method.

#include <Wire.h> //Library for I2C communication
#include <LiquidCrystal_I2C.h> //Library for LCD
//Wiring: SDA pin is connected to A4 and SCL pin to A5.
//Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd(0x3F, 16, 2); //Change to (0x27,16,2) for 16x2 LCD.

// defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enPin = 6;


//

int val;
int encoder0PinA = 7;
int encoder0PinB = 9;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int maxKnob = 30;
int stepperSpeed = 3000;
int maxSpeed     = 100;
int minSpeed     = 2800;

void setup()
{
   {
      //Initiate the LCD
      lcd.init();
      lcd.backlight();
   }

   Serial.begin(9600);

   pinMode (encoder0PinA, INPUT);
   pinMode (encoder0PinB, INPUT);



   // Sets the two pins as Outputs
   pinMode(stepPin, OUTPUT);
   pinMode(dirPin, OUTPUT);

   pinMode(enPin, OUTPUT);
   digitalWrite(enPin, LOW);

   // Set Dir to Home switch
   digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction


}
void loop()
{
   printToLCD();
   motorStep(1);
   getKnob();
}

void printToLCD()
{
    static unsigned long lcdTimer = 0;
    unsigned long lcdInterval = 500;
    if(millis() - lcdTimer >= lcdInterval)
    {
     lcdTimer = millis();
       //Print 'Hello World!' on the first line of the LCD.
      lcd.setCursor(0, 0); //Set the cursor on the first column and first row.
      lcd.print("Hello World!"); //Print the string "Hello World!"
      lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!.
      lcd.print("LCD tutorial");
    }
}


void motorStep( int MAX)
{

   for (int x = 0; x < MAX; x++)
   {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(stepperSpeed);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(stepperSpeed);
   }

}
void getKnob()
{
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH))
   {
      if (digitalRead(encoder0PinB) == LOW)
      {
         encoder0Pos--;
      }
      else
      {
         encoder0Pos++;
      }
      if ( encoder0Pos > maxKnob )
      {
         encoder0Pos = maxKnob;
      }
      else if ( encoder0Pos < 0 )
      {
         encoder0Pos = 0;
      }

      int r = encoder0Pos * 100;

      r = minSpeed - r;
      r = (r < maxSpeed) ? maxSpeed : r;

      stepperSpeed = r;

      // Serial.print (encoder0Pos);
      //  Serial.println();

      Serial.println ( r );
   }
   encoder0PinALast = n;
}