LCD Ic2 Ohm meter without Delay

Hi,
I have a problem with a sketch that I need to modify. I have to combine 2 sketches together, Ohm meter and Nema Motor Controller.
the 2 sketches are:
ohmeter

int analogPin= 0; // Pin Analogico A0
int raw= 0;  
int Vin= 5;  // Volt in uscita dal Pin 5V di Arduino
float Vout= 0;
float R1= 1000; // impostare il valore della resistenza nota in Ohm
float R2= 0;
float buffer= 0;

#include <Wire.h>     // Libreria wire già presente in Arduino ide      
#include <LiquidCrystal_I2C.h> // Libreria display I2C

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

void setup()
{
  lcd.begin(16, 2); //Inizializzazione display
Serial.begin(9600); //Apro la comunicazione seriale
}

void loop()
{
raw= analogRead(analogPin);
if(raw) 
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
lcd.print("OHM METER");
lcd.setCursor(0, 1);
lcd.print("R2:  ");
lcd.print(R2);
delay(1000);
lcd.clear();
}
}

Nema motor step controller

#define step_pin 9  // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8   // Pin 8 connected to Direction pin
#define MS1 10       // Pin 10 connected to MS1 pin
#define MS2 11      // Pin 11 connected to MS2 pin
#define SLEEP 12     // Pin 12 connected to SLEEP pin
                    
volatile boolean TurnDetected;  // need volatile for Interrupts
volatile boolean rotationdirection;  // CW or CCW rotation

// Rotary Encoder Module connections
const int PinCLK=2;   // Generating interrupts using CLK signal
const int PinDT=3;    // Reading DT signal
const int PinSW=4;    // Reading Push Button switch

int StepperPosition=0;    // To store Stepper Motor Position
int StepsToTake=4;      // Controls the speed of the Stepper per Rotary click

int direction;   // Variable to set Rotation (CW-CCW) of stepper


// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect ()  {
delay(4);  // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}


void setup ()  {

   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   
 /* Configure type of Steps on EasyDriver:
 // MS1 MS2
 //
 // LOW LOW = Full Step //
 // HIGH LOW = Half Step //
 // LOW HIGH = A quarter of Step //
 // HIGH HIGH = An eighth of Step //
 */ 
   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
   
  pinMode(PinCLK,INPUT);  // Set Pin to Input
  pinMode(PinDT,INPUT);  
  pinMode(PinSW,INPUT);
  digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
  attachInterrupt (0,rotarydetect,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}


void loop ()  {

  if (!(digitalRead(PinSW))) {   // check if button is pressed
  if (StepperPosition == 0) {  // check if button was already pressed
  } else {
      if (StepperPosition > 0) {  // Stepper was moved CW
        while (StepperPosition != 0){  //  Do until Motor position is back to ZERO
          digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
          for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
            StepperPosition=StepperPosition-StepsToTake;
        }
      }
      else {
        while (StepperPosition != 0){ 
          digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
              for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
           StepperPosition=StepperPosition+StepsToTake;
        }
      }
      StepperPosition=0; // Reset position to ZERO after moving motor back
    }
  }

// Runs if rotation was detected
  if (TurnDetected)  {
        TurnDetected = false;  // do NOT repeat IF loop until new rotation detected

// Which direction to move Stepper motor
        if (rotationdirection) { // Move motor CCW
            digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
            for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
            StepperPosition=StepperPosition-StepsToTake;
        }

        if (!rotationdirection) { // Move motor CW
            digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
            for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW); 
              delay(1);         
            }
            StepperPosition=StepperPosition+StepsToTake;
        }
  }
}

my problem is that with “delay(1000)” in the Ohm meter code, I cannot combine the two codes (ohm meter + Nema controller)
How can I combine the 2 codes to make everything work?

This is a experiment code:

#define step_pin 9  // Pin 9 connected to Steps pin on EasyDriver
#define dir_pin 8   // Pin 8 connected to Direction pin
#define MS1 10       // Pin 10 connected to MS1 pin
#define MS2 11      // Pin 11 connected to MS2 pin
#define SLEEP 12     // Pin 12 connected to SLEEP pin
                   
volatile boolean TurnDetected;  // need volatile for Interrupts
volatile boolean rotationdirection;  // CW or CCW rotation
 
// Rotary Encoder Module connections
const int PinCLK=2;   // Generating interrupts using CLK signal
const int PinDT=3;    // Reading DT signal
const int PinSW=4;    // Reading Push Button switch
 
int StepperPosition=0;    // To store Stepper Motor Position
int StepsToTake=4;      // Controls the speed of the Stepper per Rotary click
 
int direction;   // Variable to set Rotation (CW-CCW) of stepper
 
int analogPin= 0; // Pin Analogico A0
int raw= 0;  
int Vin= 5;  // Volt in uscita dal Pin 5V di Arduino
float Vout= 0;
float R1= 1000; // impostare il valore della resistenza nota in Ohm
float R2= 0;
float buffer= 0;
 
#include <Wire.h>     // Libreria wire già presente in Arduino ide      
#include <LiquidCrystal_I2C.h> // Libreria display I2C
 
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
 
// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect ()  {
delay(4);  // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
 
 
void setup ()  {
 
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);  
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   lcd.begin(16, 2); //Inizializzazione display
   Serial.begin(9600); //Apro la comunicazione seriale
 /* Configure type of Steps on EasyDriver:
 // MS1 MS2
 //
 // LOW LOW = Full Step //
 // HIGH LOW = Half Step //
 // LOW HIGH = A quarter of Step //
 // HIGH HIGH = An eighth of Step //
 */
   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
   
  pinMode(PinCLK,INPUT);  // Set Pin to Input
  pinMode(PinDT,INPUT);  
  pinMode(PinSW,INPUT);
  digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
  attachInterrupt (0,rotarydetect,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}
 
 
void loop ()  {
 
  if (!(digitalRead(PinSW))) {   // check if button is pressed
  if (StepperPosition == 0) {  // check if button was already pressed
  } else {
      if (StepperPosition > 0) {  // Stepper was moved CW
        while (StepperPosition != 0){  //  Do until Motor position is back to ZERO
          digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
          for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
            StepperPosition=StepperPosition-StepsToTake;
        }
      }
      else {
        while (StepperPosition != 0){
          digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
              for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
           StepperPosition=StepperPosition+StepsToTake;
        }
      }
      StepperPosition=0; // Reset position to ZERO after moving motor back
    }
  }
 
// Runs if rotation was detected
  if (TurnDetected)  {
        TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
 
// Which direction to move Stepper motor
        if (rotationdirection) { // Move motor CCW
            digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
            for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);            
            }
            StepperPosition=StepperPosition-StepsToTake;
        }
 
        if (!rotationdirection) { // Move motor CW
            digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
            for (int x = 1; x < StepsToTake; x++) {
              digitalWrite(step_pin, HIGH);
              delay(1);
              digitalWrite(step_pin, LOW);
              delay(1);        
            }
            StepperPosition=StepperPosition+StepsToTake;
        }
  }
  {
raw= analogRead(analogPin);
if(raw)
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
lcd.print("OHM METER");
lcd.setCursor(0, 1);
lcd.print("R2:  ");
lcd.print(R2);
delay(1000);
lcd.clear();
}
}
}

with delay(1000) → all code blocked
Motor not work

Have you considered this fine teaching tool posting https://forum.arduino.cc/index.php?topic=503368.0?

Also, using the DUE. you can use uMT.

Or, using, a STM32, ESP9266, or the ESP32 you can use freeRTOS.

Use millis to control the timing for your meter. As ever, the blink without delay example in the ide will show you how to do it.

I tried to use the “millis” command, but I can’t get it to work.

For example:

int analogPin= 0; // Pin Analogico A0
int raw= 0;  
int Vin= 5;  // Volt in uscita dal Pin 5V di Arduino
float Vout= 0;
float R1= 1000; // impostare il valore della resistenza nota in Ohm
float R2= 0;
float buffer= 0;
int period = 1000;
unsigned long time_now = 0;

#include <Wire.h>     // Libreria wire già presente in Arduino ide      
#include <LiquidCrystal_I2C.h> // Libreria display I2C

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

void setup()
{
  lcd.begin(16, 2); //Inizializzazione display
Serial.begin(9600); //Apro la comunicazione seriale
}

void loop()
{

 time_now = millis();
   
    Serial.println("Hello");
   
    while(millis() < time_now + period){
        //wait approx. [period] ms
    }

raw= analogRead(analogPin);
if(raw) 
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
lcd.print("OHM METER");
lcd.setCursor(0, 1);
lcd.print("R2:  ");
lcd.print(R2);
}
}

I use for example “Elapsed”… but don’t work

int analogPin= 0; // Pin Analogico A0
int raw= 0;  
int Vin= 5;  // Volt in uscita dal Pin 5V di Arduino
float Vout= 0;
float R1= 1000; // impostare il valore della resistenza nota in Ohm
float R2= 0;
float buffer= 0;

#include <Wire.h>     // Libreria wire già presente in Arduino ide      
#include <LiquidCrystal_I2C.h> // Libreria display I2C
#include "avdweb_VirtualDelay.h"


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 
VirtualDelay singleDelay; // default = millis

void setup()
{
  lcd.begin(16, 2); //Inizializzazione display
Serial.begin(9600); //Apro la comunicazione seriale
}

void loop()
{
raw= analogRead(analogPin);
if(raw) 
singleDelay.start(400);
  if(singleDelay.elapsed && raw)


{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("R2: ");
Serial.println(R2);
lcd.print("OHM METER");
lcd.setCursor(0, 1);
lcd.print("R2:  ");
lcd.print(R2);
}
}

where am I wrong? :frowning: :frowning:

Start here

  while(millis() < time_now + period){
        //wait approx. [period] ms
    }

That’s pretty much delay() written (badly) in longhand.

I was reading the link, I recently returned to the Arduino world and it is very difficult for me

Don't try to combine the two sketches now. First make the ohm meter sketch work without the delay. Then combine the sketches.

PaulRB: Don't try to combine the two sketches now. First make the ohm meter sketch work without the delay. Then combine the sketches.

I'm trying this morning... :( I can not make it

Here is the Ohmeter sketch to illustrate using millis() for timing (ala the beginner millis() tutorial linked above). Also the Ohms reading is done in a function (see the several things at a time tutorial) and the lcd.clear() function is not used as it can cause flickering of the display.

Not tested or compiled because I do not have the requisite lcd library. Code formatted with auto format(ctrl-t or, in the IDE, Tools, Auto Format).

Reference for the static keyword.

int analogPin = 0; // Pin Analogico A0
int raw = 0;
int Vin = 5; // Volt in uscita dal Pin 5V di Arduino
float Vout = 0;
float R1 = 1000; // impostare il valore della resistenza nota in Ohm
float R2 = 0;
float buffer = 0;

unsigned long sampleInterval = 1000; // ******** set the interval to sample and print

#include <Wire.h>     // Libreria wire già presente in Arduino ide     
#include <LiquidCrystal_I2C.h> // Libreria display I2C

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
   lcd.begin(16, 2); //Inizializzazione display
   lcd.clear();
   lcd.print("OHM METER"); // *******only needs to be written once.
   Serial.begin(9600); //Apro la comunicazione seriale
}

void loop()
{
   getSample();
}

void getSample()
{
   
   unsigned long sampleTimer = 0;     
   if (millis() - sampleTimer >= sampleInterval)
   {
      sampleTimer = millis();
      raw = analogRead(analogPin);
      if (raw)
      {
         buffer = raw * Vin;
         Vout = (buffer) / 1024.0;
         buffer = (Vin / Vout) - 1;
         R2 = R1 * buffer;
         Serial.print("Vout: ");
         Serial.println(Vout);
         Serial.print("R2: ");
         Serial.println(R2);

         lcd.setCursor(0, 1);
         lcd.print("R2:             "); // ******* spaces overwrite old data
         lcd.setCursor(0, 4); // ********* reset cursor
         lcd.print(R2);
         //delay(1000);  // *********  no more delay
         //lcd.clear();  // *********no more clear()
      }
   }
}

Thanks a lot! I will try as soon as possible. I hope this code is both in my studies

Not work :(

Not work

Bummer.

Orso_Bruno: Not work :(

Did you receive a compiling error? If so please post it.

fnb111: Did you receive a compiling error? If so please post it.

No, the compiling is ok the LCD characters seem to overlap. r2 and numer

the LCD characters seem to overlap. r2 and numer

What code are you running? Your original codes appeared to have appropriate cursor management.

The combined code from Groundfungus is wrong for a 16x2 display.

lcd.setCursor(0, 1);
         lcd.print("R2:             "); // ******* spaces overwrite old data
         lcd.setCursor(0, 4); // ********* reset cursor
         lcd.print(R2);

I use groundfungus Ohm meter code. I uploaded your code without modifying anything. LCD flickers and values ​​overlap with r2

Lcd :frowning:

lcd.setCursor(0, 4); // ********* reset cursor

Should be:

lcd.setCursor(4, 1); // ********* reset cursor

My bad, sorry.

Thanks a lot, but the problem persists on the ohm value
I tried to change “sampleinterval = 1000”, without result