lcd i2c 16x4 dosen't work with rotary encoder

hi all
i meet some problem to code with an arduino 2560 ,i2c lcd ,rotary encoder,and stepper driver(easy stepper v4.4) .it’s a projet for a filament extruder

so i explain my prob
in my code i can’t display on lcd screen the rotary encoder value
someone can help me ,please?

#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>

#include "rotary_encoder.h"

const int pinSensor = 6;
const int pinRelay = 4;
const int delayAttente = 3000;  //Delay d'attente après avoir détecté le sensor EN MILLISECONDE !!!

AccelStepper spool(AccelStepper::DRIVER, 10, 11); // 10-PUL, 11-DIR
AccelStepper rubber(AccelStepper::DRIVER, 12, 13); // 12-PUL, 13-DIR
unsigned long oldTimeMotor = 0;

unsigned speedSpool = 500;
unsigned speedRubber = 500;

long current_position;
unsigned long oldTime = 0; 
bool relache = true;

RotaryEncoder encoderSpool( 52, 50, 48 );
RotaryEncoder encoderRubber( 7, 8, 9 );
unsigned long oldTimeEncoder = 0;

LiquidCrystal_I2C lcd( 0x20, 16, 4 );
unsigned long oldTimeScreen = 0;

void incrSpool()
{
  if( speedSpool < 2048 )
    speedSpool += 10;
}

void decrSpool()
{
  if( speedSpool > 0 )
    speedSpool -= 10;
}

void stopSpool()
{
  speedSpool = 0;
}

void incrRubber()
{
  if( speedRubber < 2048 )
    speedRubber += 10;
}

void decrRubber()
{
  if( speedRubber > 0 )
    speedRubber -= 10;
}

void stopRubber()
{
  speedRubber = 0;
}

void setup()
{  
  pinMode(pinSensor, INPUT); // top sensor pin 8
  pinMode( pinRelay, OUTPUT );
  digitalWrite( pinRelay, LOW );
  
  spool.setAcceleration(500);
  spool.setCurrentPosition(0);
  spool.setMaxSpeed(1024*2);
  
  rubber.setAcceleration(500);
  rubber.setCurrentPosition(0);
  rubber.setMaxSpeed(1024*2);

  Serial.begin( 115200 );

  encoderSpool.init();
  encoderSpool.setOnIncrement( incrSpool );
  encoderSpool.setOnDecrement( decrSpool );
  encoderSpool.setOnSwitchPush( stopSpool );

  encoderRubber.init();
  encoderRubber.setOnIncrement( incrRubber );
  encoderRubber.setOnDecrement( decrRubber );
  encoderRubber.setOnSwitchPush( stopRubber );

  lcd.init();
  lcd.backlight();
}

void loop() 
{ 
  encoder();
  motor();

  //screen();
}

void motor()
{
  if( micros() - oldTimeMotor < 30 )
    return;
  
  spool.setSpeed( speedSpool * 2 );
  rubber.setSpeed( speedRubber * 2 );
    
  if ( digitalRead(pinSensor) == LOW ) {     
    relache = false;
    oldTime = millis();
    digitalWrite( pinRelay, HIGH );
  }

  if( !relache && digitalRead( pinSensor ) == HIGH )
  {
    relache = true;
  }
  
  if( (millis() - oldTime) > delayAttente )
  {
    spool.run();
    digitalWrite( pinRelay, LOW );
  }
  
  rubber.run();

  oldTimeMotor = micros();
}

void encoder()
{
  if( millis() - oldTimeEncoder < 10 )
    return;
    
  encoderSpool.run();
  encoderRubber.run();

  oldTimeEncoder = millis();
}

void screen()
{
  if( millis() - oldTimeScreen < 300 )
    return;

  Serial.print( speedRubber );
  Serial.print( " - " );
  Serial.println( speedSpool );
  return;
  
  lcd.setCursor( 0, 0 );
  lcd.print( "Rubber : " );
  lcd.print( speedRubber );
  lcd.print( "    " );
  lcd.setCursor( 0, 2 );
  lcd.print( "Spool  : " );
  lcd.print( speedSpool );
  lcd.print( "    " );

  oldTimeScreen = millis();
}

THX
ps: genuino is the best ,so forget the rest…

filawinder_encoder.zip (1.79 KB)

filawinder_encoder.ino (2.95 KB)

Do the words "Rubber : and Spool :" print?

The screen function is commented out...

void loop() 
{ 
  encoder();
  motor();

  //screen();
}

hi carlw

all lcd's code are commented out (// before each line lcd 's code)

so nothing caracters are display on lcd

it is the right position to commetn lcd ? if the lcd code are commented in code the 2 stepper motor are not controlled by the rotary encoder when the arduino is on

if th lcd are not commented in code // // // are controlled by the rotary encoder when the arduino is on

i want just display the % value of rotarys on screen 0 to max motor example:rubber :25 % spool :35 %

do you understand? sorry for my poor english regards fredaxe

Your code will first run the setup function (once), then will repeatedly run the loop function.

None of your other functions will run unless they are called in one of these two functions.

Your only function you have printing to your LCD is your screen function and it will never run because it is commented out.

Hope that helps?

Hi carlW could you write correctly code in my code ,please ? i need your help,please? regards fredaxe THX

Difficult for me to test. I don’t have any of the hardware and missing libraries…

#include <Makeblock.h> // I don't have this
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <AccelStepper.h> // I don't have this
#include <LiquidCrystal_I2C.h> // I don't have this

#include "rotary_encoder.h" // I don't have this

const int pinSensor = 6;
const int pinRelay = 4;
const int delayAttente = 3000;  //Delay d'attente après avoir détecté le sensor EN MILLISECONDE !!!

AccelStepper spool(AccelStepper::DRIVER, 10, 11); // 10-PUL, 11-DIR
AccelStepper rubber(AccelStepper::DRIVER, 12, 13); // 12-PUL, 13-DIR
unsigned long oldTimeMotor = 0;

unsigned speedSpool = 500;
unsigned speedRubber = 500;

long current_position;
unsigned long oldTime = 0;
bool relache = true;

RotaryEncoder encoderSpool( 52, 50, 48 );
RotaryEncoder encoderRubber( 7, 8, 9 );
unsigned long oldTimeEncoder = 0;

LiquidCrystal_I2C lcd( 0x20, 16, 4 );
unsigned long oldTimeScreen = 0;


void setup()
{ 
  pinMode(pinSensor, INPUT); // top sensor pin 8
  pinMode( pinRelay, OUTPUT );
  digitalWrite( pinRelay, LOW );
 
  spool.setAcceleration(500);
  spool.setCurrentPosition(0);
  spool.setMaxSpeed(1024*2);
 
  rubber.setAcceleration(500);
  rubber.setCurrentPosition(0);
  rubber.setMaxSpeed(1024*2);

  Serial.begin( 115200 );

  encoderSpool.init();
  encoderSpool.setOnIncrement( incrSpool );
  encoderSpool.setOnDecrement( decrSpool );
  encoderSpool.setOnSwitchPush( stopSpool );

  encoderRubber.init();
  encoderRubber.setOnIncrement( incrRubber );
  encoderRubber.setOnDecrement( decrRubber );
  encoderRubber.setOnSwitchPush( stopRubber );

  lcd.init();
  lcd.backlight();
}

void loop()
{
  encoder();
  motor();
  screen();
}

void motor()
{
  if( micros() - oldTimeMotor < 30 )
    return;
 
  spool.setSpeed( speedSpool * 2 );
  rubber.setSpeed( speedRubber * 2 );
   
  if ( digitalRead(pinSensor) == LOW ) {     
    relache = false;
    oldTime = millis();
    digitalWrite( pinRelay, HIGH );
  }

  if( !relache && digitalRead( pinSensor ) == HIGH )
  {
    relache = true;
  }
 
  if( (millis() - oldTime) > delayAttente )
  {
    spool.run();
    digitalWrite( pinRelay, LOW );
  }
 
  rubber.run();

  oldTimeMotor = micros();
  return;
}

void encoder()
{
  if( millis() - oldTimeEncoder < 10 )
    return;
   
  encoderSpool.run();
  encoderRubber.run();

  oldTimeEncoder = millis();
  return;
}

void screen()
{
  if( millis() - oldTimeScreen < 300 )
    return;

  Serial.print( speedRubber );
  Serial.print( " - " );
  Serial.println( speedSpool );
  return;
 
  lcd.setCursor( 0, 0 );
  lcd.print( "Rubber : " );
  lcd.print( speedRubber );
  lcd.print( "    " );
  lcd.setCursor( 0, 2 );
  lcd.print( "Spool  : " );
  lcd.print( speedSpool );
  lcd.print( "    " );

  oldTimeScreen = millis();
  return;
}

// Don't know about the following functions ???
void incrSpool()
{
  if( speedSpool < 2048 )
    speedSpool += 10;
    return;
}

void decrSpool()
{
  if( speedSpool > 0 )
    speedSpool -= 10;
    return;
}

void stopSpool()
{
  speedSpool = 0;
  return;
}

void incrRubber()
{
  if( speedRubber < 2048 )
    speedRubber += 10;
    return;
}

void decrRubber()
{
  if( speedRubber > 0 )
    speedRubber -= 10;
    return;
}

void stopRubber()
{
  speedRubber = 0;
  return;
}