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;
}