Need help with running motor for a certain amount of time

Hi,

I want to run a motor for a certain amount of time when the run switch is activated. I took the concept of the blink without delay but the problem is that the motor runs as long as the run switch is activated and not for the certified amount of time. When I look in the serial monitor the difference between the currentMillis and previousMillis is 0 and I believe this is the reason that the motor doesn't stop running after the certified time has passed.

I would very much appreciate if you could help me solve this problem.

Regards,

#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

int value=0;
int cementWeight = 0;
int sandWeight = 0;
int waterWeight = 0;
char selection;

int page_counter=1;   
const int previous = 52;             
const int next = 50;

const int runSwitch = 53;
const int mixerSwitch = 47;
const int conveyorSwitch = 46;
const int augerSwitch = 48;
unsigned long previousMillis = 0;            
unsigned long interval = 50000;


boolean current_previous = LOW;         
boolean last_previous=LOW;           
boolean last_next = LOW;
boolean current_next = LOW;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {14,15,16,17}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {18,19,20,21}; //column pinouts of the keypad (R1, R2, R3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd1(2,3,4,5,6,7);
LiquidCrystal lcd2(2,9,4,5,6,7);


void setup()
{
  Serial.begin(9600);
  lcd1.begin(20, 4);
  lcd2.begin(20, 4);
  lcd1.clear();
  lcd2.clear();

  pinMode(previous, INPUT);
  pinMode(next, INPUT);

  pinMode(runSwitch, INPUT);
  pinMode(mixerSwitch, OUTPUT);
  pinMode(conveyorSwitch, OUTPUT);
  pinMode(augerSwitch, OUTPUT);  

  displayTopMenu();
  displayLiveWeight();
}

boolean debounce(boolean last, int pin)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

void loop()
{
  if (digitalRead(runSwitch) == 1)
  {
   unsigned long currentMillis = millis(); 
   static unsigned long previousMillis;
   if (currentMillis - previousMillis < interval)
   {
    previousMillis = currentMillis;
    digitalWrite (mixerSwitch, HIGH);
    Serial.print ("t = "); 
    Serial.println (currentMillis - previousMillis);
    }
    else 
    {
      digitalWrite (mixerSwitch, LOW);
      Serial.println("OFF");
    }
   }
  else 
   {
    digitalWrite (mixerSwitch, LOW);
    checkKeypad();
    checkScroll();
   } 
}

void checkScroll()
{

  current_previous = debounce(last_previous, previous);
  current_next = debounce(last_next, next);

  //Page previous
  if (last_previous == LOW && current_previous == HIGH)
  {
    if (page_counter < 3)
    { //Page counter never higher than 3(total of pages)
      page_counter = page_counter + 1; //Page previous
    }
    else
    {
      page_counter = 1;               //return to page 1
    }
    displayTopMenu();
  }
  last_previous = current_previous;

  //Page next
  if (last_next == LOW && current_next == HIGH)
  {
    if (page_counter > 1)
    { //Page counter never lower than 1 (total of pages)
      page_counter = page_counter - 1; //Page next
    }
    else
    {
      page_counter = 3;             //return to page 3
    }
    displayTopMenu();
  }
  last_next = current_next;
}

void displayTopMenu()
{
  lcd1.clear();
  switch (page_counter)
  {
    case 1:
      lcd1.setCursor(0, 0);
      lcd1.print("Menu: ");
      lcd1.setCursor(0, 1);
      lcd1.print("For Cement Press A");
      lcd1.setCursor(0, 2);
      lcd1.print("For Sand   Press B");
      lcd1.setCursor(0, 3);
      lcd1.print("For Water  Press C");
      break;

    case 2:  //Design of page 3
      lcd1.setCursor(5, 0);
      lcd1.print("This is");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 3");
      break;

    case 3:   //Design of page 2
      lcd1.setCursor(0, 0);
      lcd1.print("For Settings Press D");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 2");
      break;
  }
}

void displayLiveWeight()
{
  lcd2.clear();
  lcd2.print("  Weight  Settings");
  lcd2.setCursor(0, 1);
  lcd2.print("Cement  Sand  Water");
  lcd2.setCursor(0, 2);
  lcd2.print(cementWeight);
  lcd2.print(" kg");
  lcd2.setCursor(8, 2);
  lcd2.print(sandWeight);
  lcd2.print(" kg");
  lcd2.setCursor(14, 2);
  lcd2.print(waterWeight);
  lcd2.print(" kg");
}

void displaySetWeight()
{
  lcd1.clear();
  lcd1.print("Cement:");
  lcd1.setCursor(7, 0);
  lcd1.print(cementWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 1);
  lcd1.print("Sand  :");
  lcd1.setCursor(7, 1);
  lcd1.print(sandWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 2);
  lcd1.print("Water :");
  lcd1.setCursor(7, 2);
  lcd1.print(waterWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 3);
  lcd1.print("Hold # to reset");
}

void checkKeypad()
{
  // based on the keypad implementation for getKey() but now for all states
  if (keypad.getKeys() && keypad.key[0].stateChanged) {
    const char key = keypad.key[0].kchar;
    const KeyState keyState = keypad.key[0].kstate;

    Serial.print("kchar: ");
    Serial.print(key);
    Serial.print(", kstate: ");
    Serial.println((keyState == PRESSED) ? "PRESSED"
                   : (keyState == HOLD) ? "HOLD"
                   : (keyState == RELEASED) ? "RELEASED"
                   : "IDLE");

    switch (key)
    {
      case 'A':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Cement Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          cementWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'B':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Sand Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          sandWeight = getTheNumber();
          Serial.print (sandWeight);
          displayLiveWeight();
        }
        break;

      case 'C':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Water Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          waterWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'D':
        if (keyState == PRESSED)
        {
          displaySetWeight();
        }
        break;

      case '#':
        if (keyState == HOLD)
        {
          cementWeight = 0;
          sandWeight = 0;
          waterWeight = 0;
          displaySetWeight();
          displayLiveWeight();
        }
        break;

      case '*':
        if (keyState == PRESSED)
        {
          displayTopMenu();
        }
        break;

      case NO_KEY:  // fall through to default
      default: ; // ignore
    }
  }
}

int getTheNumber()
{
  char buffer[4];

  // Input previous to 3 numbers until we find a * or #
  int i = 0;
  while (1)
  {
    if (keypad.getKeys() && keypad.key[0].stateChanged && keypad.key[0].kstate == PRESSED) {
      const char key = keypad.key[0].kchar;

      // If it's a number AND we have space left, add to our string
      if ('0' <= key && key <= '9' && i < 3)
      {
        buffer[i] = key;
        i++;
        Serial.print(key);
        lcd1.setCursor(i, 2);
        lcd1.print(key);

      }
      // If it's a * or #, end
      else if ('#' == key && i > 0)
      {
        // Null terminate
        buffer[i] = 0;
        int value = atoi(buffer);  // Convert to an integer
        i = 0;
        return value;
        break;
      }
    }
  }
}

When I look in the serial monitor the difference between the currentMillis and previousMillis is 0

You have two different variables named previousMillis in your program. Which one do you think that you are using ?

UKHeliBob:
You have two different variables named previousMillis in your program. Which one do you think that you are using ?

Oh, thats my bad. I was trying something different and forgot to remove one variable. However, even deleting either one of the two variables, the same problem still exists.

Post updated code.

#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

int value=0;
int cementWeight = 0;
int sandWeight = 0;
int waterWeight = 0;
char selection;

int page_counter=1;   
const int previous = 52;             
const int next = 50;

const int runSwitch = 53;
const int mixerSwitch = 47;
const int conveyorSwitch = 46;
const int augerSwitch = 48;
unsigned long previousMillis = 0;            
const long interval = 50000;


boolean current_previous = LOW;         
boolean last_previous=LOW;           
boolean last_next = LOW;
boolean current_next = LOW;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {14,15,16,17}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {18,19,20,21}; //column pinouts of the keypad (R1, R2, R3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd1(2,3,4,5,6,7);
LiquidCrystal lcd2(2,9,4,5,6,7);


void setup()
{
  Serial.begin(9600);
  lcd1.begin(20, 4);
  lcd2.begin(20, 4);
  lcd1.clear();
  lcd2.clear();

  pinMode(previous, INPUT);
  pinMode(next, INPUT);

  pinMode(runSwitch, INPUT);
  pinMode(mixerSwitch, OUTPUT);
  pinMode(conveyorSwitch, OUTPUT);
  pinMode(augerSwitch, OUTPUT);  

  displayTopMenu();
  displayLiveWeight();
}

boolean debounce(boolean last, int pin)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

void loop()
{
  if (digitalRead(runSwitch) == 1)
  {
   unsigned long currentMillis = millis(); 
   if (currentMillis - previousMillis < interval)
   {
    previousMillis = currentMillis;
    digitalWrite (mixerSwitch, HIGH);
    Serial.print ("t = "); 
    Serial.println (currentMillis - previousMillis);
    }
    else 
    {
     digitalWrite (mixerSwitch, LOW);
     Serial.println("OFF");
    }
   }
  else 
   {
    digitalWrite (mixerSwitch, LOW);
    checkKeypad();
    checkScroll();
   } 
}

void checkScroll()
{

  current_previous = debounce(last_previous, previous);
  current_next = debounce(last_next, next);

  //Page previous
  if (last_previous == LOW && current_previous == HIGH)
  {
    if (page_counter < 3)
    { //Page counter never higher than 3(total of pages)
      page_counter = page_counter + 1; //Page previous
    }
    else
    {
      page_counter = 1;               //return to page 1
    }
    displayTopMenu();
  }
  last_previous = current_previous;

  //Page next
  if (last_next == LOW && current_next == HIGH)
  {
    if (page_counter > 1)
    { //Page counter never lower than 1 (total of pages)
      page_counter = page_counter - 1; //Page next
    }
    else
    {
      page_counter = 3;             //return to page 3
    }
    displayTopMenu();
  }
  last_next = current_next;
}

void displayTopMenu()
{
  lcd1.clear();
  switch (page_counter)
  {
    case 1:
      lcd1.setCursor(0, 0);
      lcd1.print("Menu: ");
      lcd1.setCursor(0, 1);
      lcd1.print("For Cement Press A");
      lcd1.setCursor(0, 2);
      lcd1.print("For Sand   Press B");
      lcd1.setCursor(0, 3);
      lcd1.print("For Water  Press C");
      break;

    case 2:  //Design of page 3
      lcd1.setCursor(5, 0);
      lcd1.print("This is");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 3");
      break;

    case 3:   //Design of page 2
      lcd1.setCursor(0, 0);
      lcd1.print("For Settings Press D");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 2");
      break;
  }
}

void displayLiveWeight()
{
  lcd2.clear();
  lcd2.print("  Weight  Settings");
  lcd2.setCursor(0, 1);
  lcd2.print("Cement  Sand  Water");
  lcd2.setCursor(0, 2);
  lcd2.print(cementWeight);
  lcd2.print(" kg");
  lcd2.setCursor(8, 2);
  lcd2.print(sandWeight);
  lcd2.print(" kg");
  lcd2.setCursor(14, 2);
  lcd2.print(waterWeight);
  lcd2.print(" kg");
}

void displaySetWeight()
{
  lcd1.clear();
  lcd1.print("Cement:");
  lcd1.setCursor(7, 0);
  lcd1.print(cementWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 1);
  lcd1.print("Sand  :");
  lcd1.setCursor(7, 1);
  lcd1.print(sandWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 2);
  lcd1.print("Water :");
  lcd1.setCursor(7, 2);
  lcd1.print(waterWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 3);
  lcd1.print("Hold # to reset");
}

void checkKeypad()
{
  // based on the keypad implementation for getKey() but now for all states
  if (keypad.getKeys() && keypad.key[0].stateChanged) {
    const char key = keypad.key[0].kchar;
    const KeyState keyState = keypad.key[0].kstate;

    Serial.print("kchar: ");
    Serial.print(key);
    Serial.print(", kstate: ");
    Serial.println((keyState == PRESSED) ? "PRESSED"
                   : (keyState == HOLD) ? "HOLD"
                   : (keyState == RELEASED) ? "RELEASED"
                   : "IDLE");

    switch (key)
    {
      case 'A':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Cement Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          cementWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'B':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Sand Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          sandWeight = getTheNumber();
          Serial.print (sandWeight);
          displayLiveWeight();
        }
        break;

      case 'C':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Water Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          waterWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'D':
        if (keyState == PRESSED)
        {
          displaySetWeight();
        }
        break;

      case '#':
        if (keyState == HOLD)
        {
          cementWeight = 0;
          sandWeight = 0;
          waterWeight = 0;
          displaySetWeight();
          displayLiveWeight();
        }
        break;

      case '*':
        if (keyState == PRESSED)
        {
          displayTopMenu();
        }
        break;

      case NO_KEY:  // fall through to default
      default: ; // ignore
    }
  }
}

int getTheNumber()
{
  char buffer[4];

  // Input previous to 3 numbers until we find a * or #
  int i = 0;
  while (1)
  {
    if (keypad.getKeys() && keypad.key[0].stateChanged && keypad.key[0].kstate == PRESSED) {
      const char key = keypad.key[0].kchar;

      // If it's a number AND we have space left, add to our string
      if ('0' <= key && key <= '9' && i < 3)
      {
        buffer[i] = key;
        i++;
        Serial.print(key);
        lcd1.setCursor(i, 2);
        lcd1.print(key);

      }
      // If it's a * or #, end
      else if ('#' == key && i > 0)
      {
        // Null terminate
        buffer[i] = 0;
        int value = atoi(buffer);  // Convert to an integer
        i = 0;
        return value;
        break;
      }
    }
  }
}
      previousMillis = currentMillis;
      digitalWrite (mixerSwitch, HIGH);
      Serial.print ("t = ");
      Serial.println (currentMillis - previousMillis);

When I look in the serial monitor the difference between the currentMillis and previousMillis is 0

No real surprise as you just set them equal to one another

UKHeliBob:
No real surprise as you just set them equal to one another

Ok, so how can the problem be solved?

I removed the serial print and placed it before the loop. However, the readings are still not correct.

void loop()
{
  if (digitalRead(runSwitch) == 1)
  {
   unsigned long currentMillis = millis(); 
   Serial.print ("t = "); 
   Serial.println (currentMillis - previousMillis);
   if (currentMillis - previousMillis < interval)
   {
    previousMillis = currentMillis;
    digitalWrite (mixerSwitch, HIGH);
    
    }
    else 
    {
     digitalWrite (mixerSwitch, LOW);
     Serial.println("OFF");
    }
   }
  else 
   {
    digitalWrite (mixerSwitch, LOW);
    checkKeypad();
    checkScroll();
   } 
}

You want the motor to run for a period once the button becomes pressed, not when it is, pressed.

When the button becomes pressed save the value of millis() with a sensible name such as startTime, set a boolean variable, let's name it running, to true and turn on the motor. Now, each time through loop(), if running is true then test whether the required running time has elapsed by subtracting startTime from millis(). If so then turn off the motor and set running to false. If not then keep going round loop() until the period has elapsed.

Or put another way, take the correct working code from blinkWithoutDelay and don't completely break it!

One thing its easy to do is make several changes, find its stopped working, and have no working version
to go back to. Take backups, or only make one change at a time.

UKHeliBob:
You want the motor to run for a period once the button becomes pressed, not when it is, pressed.

When the button becomes pressed save the value of millis() with a sensible name such as startTime, set a boolean variable, let's name it running, to true and turn on the motor. Now, each time through loop(), if running is true then test whether the required running time has elapsed by subtracting startTime from millis(). If so then turn off the motor and set running to false. If not then keep going round loop() until the period has elapsed.

I tried to do as you advised but i think i have made some mistake. I am new at this and not very experienced. Please have a look at the code and advise.

#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

int value=0;
int cementWeight = 0;
int sandWeight = 0;
int waterWeight = 0;
char selection;

int page_counter=1;   
const int previous = 52;             
const int next = 50;

int mixerState = LOW;

const int runSwitch = 53;
const int mixerSwitch = 47;
const int conveyorSwitch = 46;
const int augerSwitch = 48;
unsigned long previousMillis = 0;            
const long interval = 10000;

boolean running = true;

boolean current_previous = LOW;         
boolean last_previous=LOW;           
boolean last_next = LOW;
boolean current_next = LOW;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {14,15,16,17}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {18,19,20,21}; //column pinouts of the keypad (R1, R2, R3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd1(2,3,4,5,6,7);
LiquidCrystal lcd2(2,9,4,5,6,7);


void setup()
{
  Serial.begin(9600);
  lcd1.begin(20, 4);
  lcd2.begin(20, 4);
  lcd1.clear();
  lcd2.clear();

  pinMode(previous, INPUT);
  pinMode(next, INPUT);

  pinMode(runSwitch, INPUT);
  pinMode(mixerSwitch, OUTPUT);
  pinMode(conveyorSwitch, OUTPUT);
  pinMode(augerSwitch, OUTPUT);  

  displayTopMenu();
  displayLiveWeight();
}

boolean debounce(boolean last, int pin)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

void loop()
{
 if (digitalRead(runSwitch) == 1)
 {  
   unsigned long startTime = millis(); 
   if (running = true)
   {
   if (millis() - startTime < interval)
   {
    running = true;
    digitalWrite (mixerSwitch, HIGH);
    }
    else 
    {
     digitalWrite (mixerSwitch, LOW);
     running == false;
     Serial.println("OFF");
    }
   }
   }
  else 
   {
    digitalWrite (mixerSwitch, LOW);
    checkKeypad();
    checkScroll();
   } 
}


void checkScroll()
{

  current_previous = debounce(last_previous, previous);
  current_next = debounce(last_next, next);

  //Page previous
  if (last_previous == LOW && current_previous == HIGH)
  {
    if (page_counter < 3)
    { //Page counter never higher than 3(total of pages)
      page_counter = page_counter + 1; //Page previous
    }
    else
    {
      page_counter = 1;               //return to page 1
    }
    displayTopMenu();
  }
  last_previous = current_previous;

  //Page next
  if (last_next == LOW && current_next == HIGH)
  {
    if (page_counter > 1)
    { //Page counter never lower than 1 (total of pages)
      page_counter = page_counter - 1; //Page next
    }
    else
    {
      page_counter = 3;             //return to page 3
    }
    displayTopMenu();
  }
  last_next = current_next;
}

void displayTopMenu()
{
  lcd1.clear();
  switch (page_counter)
  {
    case 1:
      lcd1.setCursor(0, 0);
      lcd1.print("Menu: ");
      lcd1.setCursor(0, 1);
      lcd1.print("For Cement Press A");
      lcd1.setCursor(0, 2);
      lcd1.print("For Sand   Press B");
      lcd1.setCursor(0, 3);
      lcd1.print("For Water  Press C");
      break;

    case 2:  //Design of page 3
      lcd1.setCursor(5, 0);
      lcd1.print("This is");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 3");
      break;

    case 3:   //Design of page 2
      lcd1.setCursor(0, 0);
      lcd1.print("For Settings Press D");
      lcd1.setCursor(4, 1);
      lcd1.print("Page 2");
      break;
  }
}

void displayLiveWeight()
{
  lcd2.clear();
  lcd2.print("  Weight  Settings");
  lcd2.setCursor(0, 1);
  lcd2.print("Cement  Sand  Water");
  lcd2.setCursor(0, 2);
  lcd2.print(cementWeight);
  lcd2.print(" kg");
  lcd2.setCursor(8, 2);
  lcd2.print(sandWeight);
  lcd2.print(" kg");
  lcd2.setCursor(14, 2);
  lcd2.print(waterWeight);
  lcd2.print(" kg");
}

void displaySetWeight()
{
  lcd1.clear();
  lcd1.print("Cement:");
  lcd1.setCursor(7, 0);
  lcd1.print(cementWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 1);
  lcd1.print("Sand  :");
  lcd1.setCursor(7, 1);
  lcd1.print(sandWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 2);
  lcd1.print("Water :");
  lcd1.setCursor(7, 2);
  lcd1.print(waterWeight);
  lcd1.print(" kg");
  lcd1.setCursor(0, 3);
  lcd1.print("Hold # to reset");
}

void checkKeypad()
{
  // based on the keypad implementation for getKey() but now for all states
  if (keypad.getKeys() && keypad.key[0].stateChanged) {
    const char key = keypad.key[0].kchar;
    const KeyState keyState = keypad.key[0].kstate;

    Serial.print("kchar: ");
    Serial.print(key);
    Serial.print(", kstate: ");
    Serial.println((keyState == PRESSED) ? "PRESSED"
                   : (keyState == HOLD) ? "HOLD"
                   : (keyState == RELEASED) ? "RELEASED"
                   : "IDLE");

    switch (key)
    {
      case 'A':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Cement Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          cementWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'B':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Sand Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          sandWeight = getTheNumber();
          Serial.print (sandWeight);
          displayLiveWeight();
        }
        break;

      case 'C':
        if (keyState == PRESSED)
        {
          lcd1.clear();
          lcd1.print("Enter Water Weight");
          lcd1.setCursor(0, 1);
          lcd1.print("& Then Press #");
          lcd1.setCursor(5, 2);
          lcd1.print("kg");
          waterWeight = getTheNumber();
          displayLiveWeight();
        }
        break;

      case 'D':
        if (keyState == PRESSED)
        {
          displaySetWeight();
        }
        break;

      case '#':
        if (keyState == HOLD)
        {
          cementWeight = 0;
          sandWeight = 0;
          waterWeight = 0;
          displaySetWeight();
          displayLiveWeight();
        }
        break;

      case '*':
        if (keyState == PRESSED)
        {
          displayTopMenu();
        }
        break;

      case NO_KEY:  // fall through to default
      default: ; // ignore
    }
  }
}

int getTheNumber()
{
  char buffer[4];

  // Input previous to 3 numbers until we find a * or #
  int i = 0;
  while (1)
  {
    if (keypad.getKeys() && keypad.key[0].stateChanged && keypad.key[0].kstate == PRESSED) {
      const char key = keypad.key[0].kchar;

      // If it's a number AND we have space left, add to our string
      if ('0' <= key && key <= '9' && i < 3)
      {
        buffer[i] = key;
        i++;
        Serial.print(key);
        lcd1.setCursor(i, 2);
        lcd1.print(key);

      }
      // If it's a * or #, end
      else if ('#' == key && i > 0)
      {
        // Null terminate
        buffer[i] = 0;
        int value = atoi(buffer);  // Convert to an integer
        i = 0;
        return value;
        break;
      }
    }
  }
}

MarkT:
Or put another way, take the correct working code from blinkWithoutDelay and don't completely break it!

One thing its easy to do is make several changes, find its stopped working, and have no working version
to go back to. Take backups, or only make one change at a time.

I tried to break the the code from blinkWithoutDelay step by step, but couldn't achieve what I want. It would either blink or stay on forever.

 if (digitalRead(runSwitch) == 1)

It would be better if you checked whether the button had become pressed rather than if it is pressed
but

  if (running = true)
     running == false;

with bugs like these in the code nothing is going to work anyway.

Can I suggest that you start by writing a small program that simply detects when the button becomes pressed (StateChangeExample in the IDE) and runs the motor for a short period