[SOLVED] Problem merging two working sketches

I have 2 sketches that work independently that I’m trying to combine and haven’t been successful. The first allows me to enter a number via keypad, which displays on the lcs screen, then enter that number into the runSize variable. The second uses a runSize that is coded into the program to run the designated number of cycles then stop. When I try to put the two together, so that I can change the runSize without having to upload the program again, it’s getting “stuck” after accepting the runSize and doesn’t run the cycles. I can’t fit multiple programs per post so I’m putting each one in it’s own post. This is the working keypad entry sketch:

#include <Keypad.h>



#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int t = 0;

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void lcdDisplay();

int runSize = 0;

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int GetNumber();

void setup() {


  //LCD Start
  lcd.begin (20, 4);

  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.print("AUTOWELD 5000Keytest");
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("Enter Run Size:  ");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Run Count:  ");
}

void loop() {


  lcd.setCursor (16, 1);       // go col 16 of line 2

  runSize = GetNumber();

  lcd.setCursor (16, 1);       // go col 16 of line 2
  lcd.print(runSize, DEC);

  lcd.setCursor (0, 1);       // go col 16 of line 2
  lcd.print("Run Size:      ");
}



//close main loop


int GetNumber()
{
  int num = 0;
  char key = kpd.getKey();
  while (key != '#')
  {
    switch (key)
    {
      case NO_KEY:
        break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        lcd.print(key);
        num = num * 10 + (key - '0');
        break;

      case '*':
        num = 0;
        lcd.clear();
        break;
    }

    key = kpd.getKey();
  }

  return num;
}

This is the working main program:

//Welder Version1 Rev0
//Includes relay closure to activate welding cycle
//Includes Serial Counting and Auto Shut off when runSize is reached
//Rev1A  pushbutton start and emergency stop
//With Gpop1 corrections added
//Rev2 merging lcd sketch
//Renamed Sketch, LCD now displays that info
//V1B-Cleaned up code, moved lcd displays to a function

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int t = 0;

LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);



const int SolRelayA        = 14;   // Tip Loading Plate
const int SolRelayB        = 15;   // Activate Loading Cylinder
const int SolRelayC        = 16;   // Welder Clamps
const int SolRelayD        = 17;   // Activate Blade Lift Cyinder
const int WeldRelayA        = 13;   // Activate Welder Circuit
const int pushButtonStart        = 12;  //NO Momentary pushbutton to start
const int pushButtonStop        = 9;  //NO Latching pushbutton Estop Button
unsigned long previousMillis;
unsigned long timeDelay = 2000;
unsigned long printMillis = 0;
unsigned long currentMillis = 0;
int CycleStage = 0;
int startButton = 1;
int estopPushButton = 1;
int CycleCount = 0;
int runSize = 25;//number of cycles to run before stopping
int runState = 0; //has run been turned on. 0=Off, 1=O

void lcdDisplay();

void setup() {
  Serial.begin(9600);
  digitalWrite (SolRelayA,   HIGH); //Set Relay Pins High
  digitalWrite (SolRelayB,   HIGH);
  digitalWrite (SolRelayC,   HIGH);
  digitalWrite (SolRelayD,   HIGH);
  digitalWrite (WeldRelayA,   HIGH);
  pinMode (SolRelayA,      OUTPUT); //Set Relay Pins as Outputs
  pinMode (SolRelayB,      OUTPUT);
  pinMode (SolRelayC,      OUTPUT);
  pinMode (SolRelayD,      OUTPUT);
  pinMode (WeldRelayA,      OUTPUT);
  pinMode (pushButtonStart, INPUT_PULLUP);
  pinMode (pushButtonStop, INPUT_PULLUP);

  //LCD Start
    lcd.begin (20,4);
  
// Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.print("AUTOWELD 5000 Ver.1B");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("Run Size:  ");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Run Count:  ");
}

void loop() {//open loop


lcdDisplay();
   
  estopPushButton = digitalRead (pushButtonStop);
  startButton = digitalRead(pushButtonStart);
  currentMillis = millis();//update every loop



  if (estopPushButton == 0) {
    runState = 0;
  } else if ((startButton == 0) && (estopPushButton == 1)) {
    runState = 1;
  }

  if (runState == 1) {
    if (currentMillis - previousMillis > timeDelay) {
      CycleStage++;
      previousMillis = currentMillis;//reset timer
    }
    if (CycleCount >= runSize) {
      CycleStage = 0;
  
    }

    switch (CycleStage) {
      case 0:
        break;
      case 1: //Tip Loading Plate to Vertical
        digitalWrite (SolRelayA,   LOW);
        timeDelay = 1500;
        break;
      //Insert Tube into Welder
      case 2://if CycleStage==1 kinda of arguement
        digitalWrite (SolRelayB,   LOW);
        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 3://Activate Welder Clamps
        digitalWrite (SolRelayC,   LOW);
        // delay (?);
        timeDelay = 200;//update timeDelay for next step
        break;
      case 4://Start Weld Cycle
        digitalWrite (WeldRelayA,   LOW);
        // delay (?);
        timeDelay = 300;//update timeDelay for next step
        break;
      case 5://Extend Insertion Cylinder
        digitalWrite (SolRelayB,   HIGH);
        timeDelay = 500;//update timeDelay for next step
        //delay(?);
        break;
      case 6://Tip Loading Plate to Horizontal
        digitalWrite (SolRelayA,   HIGH);
        // delay(?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 7://Extend Hopper Blade
        digitalWrite (SolRelayD,   LOW);
        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 8://Stop Weld Cycle,Retract Hopper Blade
        digitalWrite (SolRelayC,   HIGH);
        digitalWrite (SolRelayD,   HIGH);
        digitalWrite (WeldRelayA,   HIGH);

        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 9:
        digitalWrite (SolRelayA,   HIGH);
        digitalWrite (SolRelayB,   HIGH);
        digitalWrite (SolRelayC,   HIGH);
        digitalWrite (SolRelayD,   HIGH);
        digitalWrite (WeldRelayA,   HIGH);
        CycleCount = CycleCount + 1;
        Serial.print("CycleCount ");
    //Serial.println(CycleCount);  //replaced with lcd
        timeDelay = 100;//9 is now going to loop back to 1
        CycleStage = 1; //go back to step one
        break;

    }//close switch

  } else { //runState == 0
    digitalWrite (SolRelayA,   HIGH);
    digitalWrite (SolRelayB,   HIGH);
    digitalWrite (SolRelayC,   HIGH);
    digitalWrite (SolRelayD,   HIGH);
    digitalWrite (WeldRelayA,   HIGH);
    previousMillis = currentMillis;//reset timer
 
  }
  //back in the main loop
}//close main loop


  void lcdDisplay() {
  
       int t = ((runSize-CycleCount)/9 + 1);
  lcd.setCursor (16,1);        // go col 16 of line 2
  lcd.print(runSize,DEC);
  lcd.setCursor (16,2);        // go col 16 of line 3
  lcd.print(CycleCount,DEC);

if (CycleCount < runSize) {
  lcd.setCursor ( 0, 3 );        // go to the fourth line
  lcd.print("Minutes Remain:   ");
  lcd.setCursor (16,3);        // go col 16 of line 4
  lcd.print(t,DEC);
  lcd.print(" ");
} else if (CycleCount == runSize) {
 lcd.setCursor ( 0, 3 );        // go to the fourth line
  lcd.print("Run Is Complete !!!");
}
  }

And this is the partially working combination sketch. Any suggestions to resolve this would be appreciated, thank you!

//Welder Version1 Rev0
//Includes relay closure to activate welding cycle
//Includes Serial Counting and Auto Shut off when runSize is reached
//Rev1A  pushbutton start and emergency stop
//With Gpop1 corrections added
//Rev2 merging lcd sketch
//Renamed Sketch, LCD now displays that info
//V1B-Cleaned up code, moved lcd displays to a function
//New pin assignments to prepare for keypad
//Added Keypad
#include <Keypad.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int t = 0;

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);



const int SolRelayA        = 14;   // Tip Loading Plate
const int SolRelayB        = 15;   // Activate Loading Cylinder
const int SolRelayC        = 16;   // Welder Clamps
const int SolRelayD        = 17;   // Activate Blade Lift Cyinder
const int WeldRelayA        = 13;   // Activate Welder Circuit
const int pushButtonStart        = 12;  //NO Momentary pushbutton to start
const int pushButtonStop        = 9;  //NO Latching pushbutton Estop Button
unsigned long previousMillis;
unsigned long timeDelay = 2000;
unsigned long printMillis = 0;
unsigned long currentMillis = 0;
int CycleStage = 0;
int startButton = 1;
int estopPushButton = 1;
int CycleCount = 0;
int runSize = 1;//number of cycles to run before stopping
int runState = 0; //has run been turned on. 0=Off, 1=O

//begin keypad
const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int GetNumber();

//End Keypad

void lcdDisplay();

void setup() {
  Serial.begin(9600);
  digitalWrite (SolRelayA,   HIGH); //Set Relay Pins High
  digitalWrite (SolRelayB,   HIGH);
  digitalWrite (SolRelayC,   HIGH);
  digitalWrite (SolRelayD,   HIGH);
  digitalWrite (WeldRelayA,   HIGH);
  pinMode (SolRelayA,      OUTPUT); //Set Relay Pins as Outputs
  pinMode (SolRelayB,      OUTPUT);
  pinMode (SolRelayC,      OUTPUT);
  pinMode (SolRelayD,      OUTPUT);
  pinMode (WeldRelayA,      OUTPUT);
  pinMode (pushButtonStart, INPUT_PULLUP);
  pinMode (pushButtonStop, INPUT_PULLUP);

  //LCD Start
  lcd.begin (20, 4);

  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.print("AUTOWELD 5000 Ver.1C");
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("Enter Run Size:  ");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Run Count:  ");
}

void loop() {//open loop

  lcdDisplay();

  estopPushButton = digitalRead (pushButtonStop);
  startButton = digitalRead(pushButtonStart);
  currentMillis = millis();//update every loop



  if (estopPushButton == 0) {
    runState = 0;
  } else if ((startButton == 0) && (estopPushButton == 1)) {
    runState = 1;
  }

  if (runState == 1) {
    if (currentMillis - previousMillis > timeDelay) {
      CycleStage++;
      previousMillis = currentMillis;//reset timer
    }
    if (CycleCount >= runSize) {
      CycleStage = 0;

    }



    switch (CycleStage) {
      case 0:
        break;
      case 1: //Tip Loading Plate to Vertical
        digitalWrite (SolRelayA,   LOW);
        timeDelay = 1500;
        break;
      //Insert Tube into Welder
      case 2://if CycleStage==1 kinda of arguement
        digitalWrite (SolRelayB,   LOW);
        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 3://Activate Welder Clamps
        digitalWrite (SolRelayC,   LOW);
        // delay (?);
        timeDelay = 200;//update timeDelay for next step
        break;
      case 4://Start Weld Cycle
        digitalWrite (WeldRelayA,   LOW);
        // delay (?);
        timeDelay = 300;//update timeDelay for next step
        break;
      case 5://Extend Insertion Cylinder
        digitalWrite (SolRelayB,   HIGH);
        timeDelay = 500;//update timeDelay for next step
        //delay(?);
        break;
      case 6://Tip Loading Plate to Horizontal
        digitalWrite (SolRelayA,   HIGH);
        // delay(?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 7://Extend Hopper Blade
        digitalWrite (SolRelayD,   LOW);
        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 8://Stop Weld Cycle,Retract Hopper Blade
        digitalWrite (SolRelayC,   HIGH);
        digitalWrite (SolRelayD,   HIGH);
        digitalWrite (WeldRelayA,   HIGH);

        //delay (?);
        timeDelay = 1000;//update timeDelay for next step
        break;
      case 9:
        digitalWrite (SolRelayA,   HIGH);
        digitalWrite (SolRelayB,   HIGH);
        digitalWrite (SolRelayC,   HIGH);
        digitalWrite (SolRelayD,   HIGH);
        digitalWrite (WeldRelayA,   HIGH);
        CycleCount = CycleCount + 1;
        Serial.print("CycleCount ");
        //Serial.println(CycleCount);  //replaced with lcd
        timeDelay = 100;//9 is now going to loop back to 1
        CycleStage = 1; //go back to step one
        break;

    }//close switch

  } else { //runState == 0
    digitalWrite (SolRelayA,   HIGH);
    digitalWrite (SolRelayB,   HIGH);
    digitalWrite (SolRelayC,   HIGH);
    digitalWrite (SolRelayD,   HIGH);
    digitalWrite (WeldRelayA,   HIGH);
    previousMillis = currentMillis;//reset timer

  }
  //back in the main loop
}//close main loop



int GetNumber()
{
  int num = 0;
  char key = kpd.getKey();
  while (key != '#')
  {
    switch (key)
    {
      case NO_KEY:
        break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        lcd.print(key);
        num = num * 10 + (key - '0');
        break;

      case '*':
        num = 0;
        lcd.clear();
        break;
    }

    key = kpd.getKey();
  }

  return num;
}


void lcdDisplay() {

  int t = ((runSize - CycleCount) / 9 + 1);


  lcd.setCursor (16, 1);       // go col 16 of line 2
  runSize = GetNumber();

  lcd.setCursor (16, 1);       // go col 16 of line 2
  lcd.print(runSize, DEC);

  lcd.setCursor (0, 1);       // go col 16 of line 2
  lcd.print("Run Size:      "); //Clears input digits from lcd

  lcd.setCursor (16, 2);       // go col 16 of line 3
  lcd.print(CycleCount, DEC);

  if (CycleCount < runSize) {
    lcd.setCursor ( 0, 3 );        // go to the fourth line
    lcd.print("Minutes Remain:   ");
    lcd.setCursor (16, 3);       // go col 16 of line 4
    lcd.print(t, DEC);
    lcd.print(" ");
  } else if (CycleCount == runSize) {
    lcd.setCursor ( 0, 3 );        // go to the fourth line
    lcd.print("Run Is Complete !!!");
  }
}

It seems to me, getting a new value of runSize every time you run loop( ), isn't what you really want to be doing.

Thank you!!!!!!! What you said just cleared the cobwebs out of my brain. I've moved both the functions out of the loop and into setup and it appears to almost be working. The run count and est. time obviously don't work correctly if they are in setup so now I'll have to move them back to the loop but I think I'm over the hump. Thanks again.

The run count and est. time obviously don't work correctly if they are in setup

That's not obvious to me.

What you should have is something like what TinyGPS has - a method that takes a character read from the GPS and returns true or false, to indicate that the data is, or is not ready to be prsed and used.

You would determine, on each pass through loop(), if a key was pressed. If so, call the function. That function determines whether to store the key or to convert to stored data to an integer value that it stores somewhere else. If it does the conversion, it should clear the stored key data, reset the count, and return true. If not, it should return false.

When the function returns true, you'd copy the stored value to runSize.

You'd also, on each pass through loop(), determine if the current value of runSize was to be used (for whatever it is used for).

BillMurphy:
I've moved both the functions out of the loop and into setup and it appears to almost be working.

Please post the latest version of your code when you make changes. It would be a waste of time commenting on yesterday's problem.

...R

PaulS:
That’s not obvious to me.

Paul, I think what I should have said, and maybe I’m wrong even on this, is that since the run count and est. time remaining are calculations where the results change as the program progresses they wouldn’t be recalculated because they are in setup. Or am I wrong that things in setup only are looked at once? Regardless I did get the combined sketch to query me for the run size and allow me to enter it, and then I was able to press the start button and it began cycling. It did not, however, count the cycles or adjust the time. At that point I called it a night, planning to fine tune this morning but unfortunately the wheels fell off the wagon today and I’m pretty much going around in circles with no progress and actually managed to lose the progress I made last night. My programming knowledge is very limited so I’m not really following the GPS example but the code I am using for number input does suit my needs if I can just get the 2 sketches to merge. I only need to set the runSize one time at the beginning of each run. Once the run is complete the operator will press a reset button to clear the info and get back to beginning to enter the next runSize.

Robin below is the latest code which, although it’s slightly different due to my experimenting, is functionally the same as yesterday’s combined code in that it allows the data entry but does not respond to the start button press.

//Welder Version1 Rev0
//Includes relay closure to activate welding cycle
//Includes Serial Counting and Auto Shut off when runSize is reached
//Rev1A  pushbutton start and emergency stop
//With Gpop1 corrections added
//Rev2 merging lcd sketch
//Renamed Sketch, LCD now displays that info
//V1B-Cleaned up code, moved lcd displays to a function
#include <Keypad.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int t = 0;

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);



const int SolRelayA        = 14;   // Tip Loading Plate
const int SolRelayB        = 15;   // Activate Loading Cylinder
const int SolRelayC        = 16;   // Welder Clamps
const int SolRelayD        = 17;   // Activate Blade Lift Cyinder
const int WeldRelayA        = 13;   // Activate Welder Circuit
const int pushButtonStart        = 12;  //NO Momentary pushbutton to start
const int pushButtonStop        = 9;  //NO Latching pushbutton Estop Button
unsigned long previousMillis;
unsigned long timeDelay = 2000;
unsigned long printMillis = 0;
unsigned long currentMillis = 0;
int CycleStage = 0;
int startButton = 1;
int estopPushButton = 1;
int CycleCount = 0;
int runSize = 0;//number of cycles to run before stopping
int runState = 0; //has run been turned on. 0=Off, 1=O

void lcdDisplay();


const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int GetNumber();

void setup() {
  Serial.begin(9600);
  digitalWrite (SolRelayA,   HIGH); //Set Relay Pins High
  digitalWrite (SolRelayB,   HIGH);
  digitalWrite (SolRelayC,   HIGH);
  digitalWrite (SolRelayD,   HIGH);
  digitalWrite (WeldRelayA,   HIGH);
  pinMode (SolRelayA,      OUTPUT); //Set Relay Pins as Outputs
  pinMode (SolRelayB,      OUTPUT);
  pinMode (SolRelayC,      OUTPUT);
  pinMode (SolRelayD,      OUTPUT);
  pinMode (WeldRelayA,      OUTPUT);
  pinMode (pushButtonStart, INPUT_PULLUP);
  pinMode (pushButtonStop, INPUT_PULLUP);

  //LCD Start
  lcd.begin (20, 4);

  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.print("AUTOWELD 5000 Ver.1C");
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("Enter Run Size:  ");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Use Keypad To Enter");
   lcd.setCursor ( 0, 3 );        // go to the fourth line
  lcd.print("Then press # To Set");
}

void loop() {//open loop








  lcd.setCursor (16, 1);       // go col 16 of line 2

  runSize = GetNumber();


 estopPushButton = digitalRead (pushButtonStop);
  startButton = digitalRead(pushButtonStart);
  currentMillis = millis();//update every loop

 lcdDisplay();


if (estopPushButton == 0) {
  runState = 0;
} else if ((startButton == 0) && (estopPushButton == 1)) {
  runState = 1;
}

if (runState == 1) {
  if (currentMillis - previousMillis > timeDelay) {
    CycleStage++;
    previousMillis = currentMillis;//reset timer
  }
  if (CycleCount >= runSize) {
    CycleStage = 0;

  }

  switch (CycleStage) {
    case 0:
      break;
    case 1: //Tip Loading Plate to Vertical
      digitalWrite (SolRelayA,   LOW);
      timeDelay = 1500;
      break;
    //Insert Tube into Welder
    case 2://if CycleStage==1 kinda of arguement
      digitalWrite (SolRelayB,   LOW);
      //delay (?);
      timeDelay = 1000;//update timeDelay for next step
      break;
    case 3://Activate Welder Clamps
      digitalWrite (SolRelayC,   LOW);
      // delay (?);
      timeDelay = 200;//update timeDelay for next step
      break;
    case 4://Start Weld Cycle
      digitalWrite (WeldRelayA,   LOW);
      // delay (?);
      timeDelay = 300;//update timeDelay for next step
      break;
    case 5://Extend Insertion Cylinder
      digitalWrite (SolRelayB,   HIGH);
      timeDelay = 500;//update timeDelay for next step
      //delay(?);
      break;
    case 6://Tip Loading Plate to Horizontal
      digitalWrite (SolRelayA,   HIGH);
      // delay(?);
      timeDelay = 1000;//update timeDelay for next step
      break;
    case 7://Extend Hopper Blade
      digitalWrite (SolRelayD,   LOW);
      //delay (?);
      timeDelay = 1000;//update timeDelay for next step
      break;
    case 8://Stop Weld Cycle,Retract Hopper Blade
      digitalWrite (SolRelayC,   HIGH);
      digitalWrite (SolRelayD,   HIGH);
      digitalWrite (WeldRelayA,   HIGH);

      //delay (?);
      timeDelay = 1000;//update timeDelay for next step
      break;
    case 9:
      digitalWrite (SolRelayA,   HIGH);
      digitalWrite (SolRelayB,   HIGH);
      digitalWrite (SolRelayC,   HIGH);
      digitalWrite (SolRelayD,   HIGH);
      digitalWrite (WeldRelayA,   HIGH);
      CycleCount = CycleCount + 1;
      Serial.print("CycleCount ");
      //Serial.println(CycleCount);  //replaced with lcd
      timeDelay = 100;//9 is now going to loop back to 1
      CycleStage = 1; //go back to step one
      break;

  }//close switch

} else { //runState == 0
  digitalWrite (SolRelayA,   HIGH);
  digitalWrite (SolRelayB,   HIGH);
  digitalWrite (SolRelayC,   HIGH);
  digitalWrite (SolRelayD,   HIGH);
  digitalWrite (WeldRelayA,   HIGH);
  previousMillis = currentMillis;//reset timer

}
//back in the main loop
}//close main loop


void lcdDisplay() {

  int t = ((runSize - CycleCount) / 9 + 1);
   lcd.setCursor (0, 1);       // go col 1 of line 2
  lcd.print("Run Size:           ");
  lcd.setCursor (16, 1);       // go col 16 of line 2
  lcd.print(runSize, DEC);
  lcd.setCursor (0, 2);       // go col 1 of line 3
  lcd.print("Run Total:          ");
  lcd.setCursor (16, 2);       // go col 16 of line 3
  lcd.print(CycleCount, DEC);

  if (CycleCount < runSize) {
    lcd.setCursor ( 0, 3 );        // go to the fourth line
    lcd.print("Minutes Remain:   ");
    lcd.setCursor (16, 3);       // go col 16 of line 4
    lcd.print(t, DEC);
    lcd.print(" ");
  } else if (CycleCount == runSize) {
    lcd.setCursor ( 0, 3 );        // go to the fourth line
    lcd.print("Run Is Complete !!!");
  }
}


int GetNumber()
{
  int num = 0;
  char key = kpd.getKey();
  while (key != '#')
  {
    switch (key)
    {
      case NO_KEY:
        break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
        lcd.print(key);
        num = num * 10 + (key - '0');
        break;

      case '*':
        num = 0;
        lcd.clear();
        break;
    }

    key = kpd.getKey();
  }

  return num;
}

it looks like you keep updating the runsize

runSize = GetNumber();

every time the main loop gets to this line it trys to update the runsize which may result in the runsize becoming zero so it thinks its finished.

you need to look at GetNumber when you want to update then when you exit that loop you do not need to call it again. This could be a simple "if".

If runsize ==0 run the getnumber loop but skip the code in the main loop

if runsize now does not equal zero or the # key was pressed set a flag and skip the getnumber() and run the code In the main loop.

The lcd is being used to display runSize so the lcd may need to be written in both loops

Try replacing this

  runSize = GetNumber();

with

  int tempSize = GetNumber();
  if (tempSize > 0) {
     runSize = tempSize;
  }

...R

Gpop you have saved me again! I changed this:

runSize = GetNumber();

to this:

if (runSize == 0) {
  runSize = GetNumber();

and I'm running! Thank you!!!! Off to do some testing but I think I'm where I need to be.

Robin, I'm wondering if your code is an improvement or just another way of accomplishing the same thing?

BillMurphy:
Robin, I'm wondering if your code is an improvement or just another way of accomplishing the same thing?

It's just another way.

I prefer to use an intermediate variable because it allows me to print the intermediate value for debugging purposes.

...R

Got it and thanks!

Robin2:
It's just another way.

I prefer to use an intermediate variable because it allows me to print the intermediate value for debugging purposes.

...R

I don't think its just another way.
The only time runsize is going to be zero is after a reboot (unless extra code is added at the end of the run to reset runsize to zero) . tempsize will be zero after every scan thus it can be edited with out rebooting. (I think)

Try both and see which one suits your application

Hmmmmm, I see what you are saying but since my "Enter Run Size:" display message is in void setup I need a reboot to access it anyways. Maybe I'll work on that at some point but for now I'm going to go enjoy what's left of my holiday weekend. LOL

BillMurphy:
for now I'm going to go enjoy what's left of my holiday weekend. LOL

More programming, then :slight_smile:

...R