Hmph. Could you post your compete code again, including functions and interrupts?Ummm i've put them back into the place they were and now all resets zero on the second pass
here is the outputCode:ENC_SWstate =digitalRead(ENC_SW); if (ENC_SWstate == LOW && prevENC_SWstate == HIGH) { delay(50); //debounce period ENC_SWstate =digitalRead(ENC_SW); if (ENC_SWstate == LOW) //stable LOW { Counter1 = w1Value /10<<1; Serial.print("Pass:"); Serial.println(Counter); Serial.print("Counter:"); Serial.println(Counter1); Serial.print("W1:"); Serial.println(w1Value); Serial.print("W2:"); Serial.println(WeldTime); Serial.print("pValue:"); Serial.println(pValue); Serial.println(" "); confMenu =1; Counter++; } } prevENC_SWstate = ENC_SWstate; while (confMenu ==1) { ENC_SWstate =digitalRead(ENC_SW); if (ENC_SWstate != prevENC_SWstate) { if (ENC_SWstate == LOW) { select +=1; select = select %4; if (select ==0) { noInterrupts(); Counter1 = w1Value /10<<1; interrupts(); } if (select ==1) { noInterrupts(); Counter1 = WeldTime /10<<1; interrupts(); } if (select ==2) { noInterrupts(); Counter1 = pValue /10<<1; interrupts(); } } prevENC_SWstate = ENC_SWstate; } switch (select) { case0: //W1 clearSelectPointer(1); lcd.setCursor(0, 0); lcd.print(">"); w1Value =readEncoder() %6*10; if (w1Value <=0) { w1Value =0; } lcd.setCursor(1, 0); lcd.print("W1:"); lcd.print(w1Value); lcd.print(" "); break; case1: //W2 clearSelectPointer(2); lcd.setCursor(0, 1); lcd.print(">"); WeldTime =readEncoder() %51*10; if (WeldTime <=0) { WeldTime =0; } lcd.setCursor(1, 1); lcd.print("W2:"); lcd.print(WeldTime); //lcd.setCursor(5, 1); lcd.print(" "); break; case2: //P clearSelectPointer(3); lcd.setCursor(8, 1); lcd.print(">"); pValue =readEncoder() %11*10; if (pValue <=0) { pValue =0; } lcd.setCursor(9, 1); lcd.print("P:"); lcd.print(pValue); lcd.print("%"); lcd.print(" "); break; default: clearSelectPointer(4); confMenu =0; select =0; break; } }
byte resetFlag = false;
****************************************************************************
attachPCINT(digitalPinToPCINT(ENC_SW), resetENC, RISING);
****************************************************************************
void resetENC()
{
if (resetFlag == false)
{
Counter1 = 0;
}
}
Begin main loop
Pass: 0
Encoder: 0
W1: 0
W2: 0
pValue: 0
Press =/- to change W1. W1 set to 20
Press =/- to change W2. W2 set to 10
Press =/- to change P. P set to 30
Pass: 1
Encoder: 4
W1: 20
W2: 10
pValue: 30
Press =/- to change W1. W1 set to 20
Press =/- to change W2. W2 set to 10
Press =/- to change P. P set to 30
Pass: 2
Encoder: 4
W1: 20
W2: 10
pValue: 30
Press =/- to change W1. W1 set to 10
Press =/- to change W2. W2 set to 20
Press =/- to change P. P set to 50
Pass: 3
Encoder: 2
W1: 10
W2: 20
pValue: 50
Press =/- to change W1. W1 set to 10
Press =/- to change W2. W2 set to 20
Press =/- to change P. P set to 50
Pass: 4
Encoder: 2
W1: 10
W2: 20
pValue: 50
#include <stdio.h>
#include <conio.h> // for kbhit()
//#include <Arduino.h>
//#include <EEPROM.h>
//#include <NTC_Thermistor.h>
//#include <PinChangeInterrupt.h>
//#include <Servo.h>
//#include <Wire.h>
//#include <hd44780.h>
//#include <hd44780ioClass/hd44780_I2Cexp.h>
//hd44780_I2Cexp lcd;
//Servo myServo;
// Const Variables and pin assinged
const int ENC_SW = 3; // Encoder Switch
const int rdy = 4; // READY LED
const int servoPin = 6; // Servo
const int ENC_PinA = 7; // PIN A of Encoder
const int bzr = 5; // Buzzer
const int ENC_PinB = 8; // PIN B of Encoder
const int buttonPin = 9; // Weld button
const int cs = 10; // Contact Sense
const int TRIAC = 1; // Load
const int shortPress = 1;
const int longPress = 2;
const int longerPress = 3;
const int tgr_dly = 5084; // delay after zero cross at peak sine - average of 50/60Hz
const int shortTime = 200; // if equal longTime there is no unrecorded press
const int longTime = 500;
const int longerTime = 2000;
const int pause = 450; // pause value between dual pulses
const int delayTime = 50; // bounce delay for Encoder Switch
// Variables
#define Temp A6 // NTC PIN
#define RR 10000 // Reference Resistance
#define NR 10000 // Nominal Resistance
#define NT 25 // Nominal Temperature
#define B_Val 3435
// NTC_Thermistor *thermistor;
int zCd = 2; // Zero Crossing Detect
int weldCont = 0;
int menuSelect = 0;
int stayInMenu = 0;
int weldExitFlag = 0;
int previousButtonState = 1;
int buttonState = 1;
int prevENC_SWstate = 1;
int ENC_SWstate = 1;
int buttonFlag;
int resetFlag = 0;
int manualWeld = 0;
int testLoop = 1;
int cs_State;
int prevCS_State;
int currCS_State;
int congMenu1;
int confMenu;
int rdyState;
int WeldVal;
int ENC_PinAState = 0;
int ENC_PinALastState = 0;
int WeldTime; // read value from rotaryEncoder()
int prevRotaryVal;
int W2Addr = 110; // Address block to store W2 value
int PreWeldAddr = 120; // Address block to store PreWeld time
int startSrvAddr = 130; // Address block to store servo Start degree
int endSrvAddr = 140; // Address block to store servo end degree
int W2;
int pre;
int SrvPotMap1;
int SrvPotMap2;
int counter; // Auto weld counter
int WeldStepValue = 10; // Value of POT in milliseconds
int OldWeldStepValue; // Old value of POT
int PotValue; // Actual POT value
int ConfigureVal;
int w1Value;
int pValue;
int select;
int Counter1;
int Counter;
unsigned long TimeStamp;
unsigned long ENC_SWpressDuration;
unsigned long buttonPressStartTimeStamp;
unsigned long buttonPressDuration;
unsigned long timeCheck = 1000; // time to check zero cross error
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long prevT;
unsigned long currT;
unsigned long delayT = 500;
_Bool startTimeout = 0; // zc sense error timeout
volatile _Bool zeroCrossingFlag = 0;
//void setup()
//{
// lcd.clear();
// lcd.begin(16, 2);
// thermistor = new NTC_Thermistor(Temp, RR, NR, NT, B_Val);
// ServoReturn();
// pre = EEPROM.read(PreWeldAddr);
// SrvPotMap1 = EEPROM.read(endSrvAddr);
// SrvPotMap2 = EEPROM.read(startSrvAddr);
// W2 = EEPROM.read(W2Addr);
// pinMode(buttonPin, INPUT_PULLUP);
// pinMode(ENC_PinA, INPUT_PULLUP);
// pinMode(ENC_PinB, INPUT_PULLUP);
// pinMode(zCd, INPUT_PULLUP);
// pinMode(cs, INPUT_PULLUP);
// pinMode(ENC_SW, INPUT_PULLUP);
// pinMode(servoPin, OUTPUT);
// pinMode(TRIAC, OUTPUT);
// pinMode(rdy, OUTPUT);
// pinMode(bzr, OUTPUT);
// pinMode(A2, INPUT);
// pinMode(A4, INPUT_PULLUP);
// pinMode(A5, INPUT_PULLUP);
// attachInterrupt(0, setFlag, FALLING); // zero cross
// attachPCINT(digitalPinToPCINT(ENC_PinA), rotaryEncoder, CHANGE);
// attachPCINT(digitalPinToPCINT(ENC_SW), resetENC, RISING);
// attachPCINT(digitalPinToPCINT(buttonPin), WeldExit, CHANGE);
// delay(10);
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Max Weld");
// lcd.setCursor(12, 0);
// lcd.print("v3.0");
// lcd.setCursor(0, 1);
// lcd.print("GT Designs");
// delay(2000);
// lcd.clear();
// Display();
// startupTone();
// Serial.begin(115200);
//}
// ------------------------------------------------ FUNCTIONS ---------------------------------
//void startupTone()
//{
// tone(bzr, 2500);
// delay(120);
// noTone(bzr);
// delay(240);
// tone(bzr, 2500);
// delay(120);
// noTone(bzr);
//}
//
//void weldTone()
//{
// if (manualWeld == 0)
// {
// tone(bzr, 2322);
// delay(70);
// noTone(bzr);
// tone(bzr, 2222);
// delay(70);
// noTone(bzr);
// }
// else
// {
// tone(bzr, 3153);
// delay(50);
// noTone(bzr);
// }
//}
//
//void saveSuccess()
//{
// pinMode(bzr, OUTPUT);
// tone(bzr, 2025);
// delay(300);
// noTone(bzr);
//}
//
//void resetENC()
//{
// if (resetFlag == 0)
// {
// Counter1 = 0;
// }
//}
//
//void Display()
//{
// lcd.clear();
// lcd.setCursor(1, 0);
// lcd.print("W1:");
// lcd.print(pre);
// myServo.detach();
//}
//
//void rotaryEncoder()
//{
// ENC_PinAState = digitalRead(ENC_PinA);
// if (digitalRead(ENC_PinB) != ENC_PinAState)
// {
// Counter1--;
// }
// else
// {
// Counter1++;
// }
//}
_Bool checkButtons()
{
_Bool up = 0;
_Bool down = 0;
_Bool enter = 0;
if(kbhit())
{
char tempKey = getch();
//printf("%d\n",tempKey);
if (tempKey==61) // 43='+' and 61='='
{
up=1;
Counter1++;
}
if (tempKey==45)
{
down=1;
Counter1--;
}
if (tempKey==13) // 10 is enter key? or maybe 13?
{
enter=1;
}
}
return enter;
}
int readEncoder()
{
// noInterrupts();
int copyCounter = Counter1;
// interrupts();
return (copyCounter) >> 1;
}
//void Weld() // Actual weld
//{
// int wait;
// if (SrvPotMap1 && SrvPotMap2 == 0)
// // disables servo when both servo pot values are zero and set wait value
// {
// wait = 700;
// }
// else
// {
// wait = 0;
// }
// if (W2 <= 0)
// {
// WeldVal = WeldTime;
// }
// else
// {
// WeldVal = W2;
// }
// delay(wait);
// myServo.attach(servoPin);
// digitalWrite(rdy, 0);
// ServoStart();
// buttonState = digitalRead(buttonPin);
// zeroCrossingFlag = 0; // set flag 0 and wait for next zero crossing
// while (!zeroCrossingFlag)
// {
// };
// delayMicroseconds(tgr_dly);
// digitalWrite(TRIAC, 1);
// delay(pre); // preWeld time ms
// digitalWrite(TRIAC, 0);
// if (WeldVal > 0)
// {
// delay(pause);
// zeroCrossingFlag = 0; // set flag false and wait for next zero crossing
// while (!zeroCrossingFlag)
// {
// };
// delayMicroseconds(tgr_dly);
// digitalWrite(TRIAC, 1);
// delay(WeldVal);
// digitalWrite(TRIAC, 0);
// }
// ServoReturn();
// myServo.detach();
// weldTone();
//}
//
//void WeldExit()
//{
// if (weldCont == true)
// {
// weldExitFlag = true; // exit Auto Weld
// }
//}
//void contactSense()
//{
// lcd.clear();
// manualWeld = 1;
// weldCont = true;
// weldExitFlag = 0;
// lcd.setCursor(0, 0);
// lcd.print("T:");
// lcd.print(String(thermistor->readCelsius()));
// lcd.setCursor(0, 1);
// lcd.print("W2:");
// lcd.print(WeldTime);
// tone(bzr, 3256);
// delay(30);
// noTone(bzr);
// delay(100);
// tone(bzr, 4256);
// delay(30);
// noTone(bzr);
// delay(50);
// tone(bzr, 5256);
// delay(30);
// noTone(bzr);
// while (weldExitFlag == 0)
// {
// currT = millis();
//
// if (currT - prevT >= delayT)
// {
// prevT = currT;
// if (rdyState == 0)
// {
// rdyState = 1;
// }
// else
// {
// rdyState = 0;
// }
// digitalWrite(4, rdyState);
// }
// WeldTime += (readEncoder() % 101) * 10;
// if (WeldTime <= 0)
// {
// WeldTime = 0;
// }
// lcd.setCursor(0, 1);
// lcd.print("W2:");
// lcd.print(WeldTime);
// zeroCrossingFlag = 0;
// while (!zeroCrossingFlag)
// {
// };
// delayMicroseconds(8500);
// digitalWrite(TRIAC, 1);
// delayMicroseconds(200); // preWeld time ms
// digitalWrite(TRIAC, 0);
//
// cs_State = digitalRead(cs);
// if (cs_State == 1 && prevCS_State == 0)
// {
// delay(500);
// Weld();
// }
// if (prevCS_State == 1 && cs_State == 0)
// {
// prevCS_State = 0;
// }
// }
// buttonState = 0;
// previousButtonState = 0;
//}
void setFlag()
{
zeroCrossingFlag = 1; // interrupt sets flag true
}
//void ServoStart() // Increments to start position from Finish
//{
// for (int i = SrvPotMap1; i <= SrvPotMap2; i += 1)
// {
// myServo.attach(servoPin);
// myServo.write(i);
// delay(5);
// }
//}
//
//void ServoReturn() // Increments to Finish position from Start
//{
// for (int i = SrvPotMap2; i >= SrvPotMap1; i -= 1)
// {
// myServo.write(i);
// delay(5);
// }
//}
//
//void configMenu() // End configuration mode.
//{
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("CONFIG MODE");
// delay(800);
// lcd.clear();
// lcd.setCursor(1, 0);
// lcd.print("Start");
// lcd.setCursor(7, 0);
// lcd.print("End");
// lcd.setCursor(11, 0);
// lcd.print("Test");
// lcd.setCursor(1, 1);
// lcd.print("Exit");
// myServo.detach();
//}
//
//int clearSelectPointer(int checkPointer)
//{
// switch (checkPointer)
// {
// case 1: // for select 1
// lcd.setCursor(0, 1); // 2
// lcd.print(" ");
// lcd.setCursor(8, 1); // 3
// lcd.print(" ");
// break;
// case 2: // for select 2
// lcd.setCursor(0, 0); // 1
// lcd.print(" ");
// lcd.setCursor(8, 1); // 3
// lcd.print(" ");
// break;
// case 3: // for select 3
// lcd.setCursor(0, 0); // 1
// lcd.print(" ");
// lcd.setCursor(0, 1); // 2
// lcd.print(" ");
// break;
// case 4:
// lcd.setCursor(0, 0); // 1
// lcd.print(" ");
// lcd.setCursor(0, 1); // 2
// lcd.print(" ");
// lcd.setCursor(8, 1); //3
// lcd.print(" ");
// break;
// }
// return (checkPointer);
//}
// ------------------------------------------------ END OF FUNCTIONS --------------------------
int main() // previously void loop()
{
// lcd.setCursor(1, 1);
// lcd.print("W2:");
// lcd.print(WeldTime);
// lcd.setCursor(9, 0);
// lcd.print("T:");
// lcd.print(String(thermistor->readCelsius()));
// lcd.setCursor(9, 1);
// lcd.print("P:");
// digitalWrite(rdy, 1);
// ENC_SWstate = digitalRead(ENC_SW);
printf("Begin main loop\n");
while(1)
{
if (checkButtons())
{
// delay(50); //debounce period
// ENC_SWstate = digitalRead(ENC_SW);
if (1) //stable 0
{
Counter1 = w1Value / 10 << 1;
// Serial.print("Pass:");
// Serial.println(Counter);
// Serial.print("Counter:");
// Serial.println(Counter1);
// Serial.print("W1:");
// Serial.println(w1Value);
// Serial.print("W2:");
// Serial.println(WeldTime);
// Serial.print("pValue:");
// Serial.println(pValue);
// Serial.println(" ");
printf("\nPass: %d\n",Counter);
printf("Encoder: %d\n",Counter1);
printf("W1: %d\n",w1Value);
printf("W2: %d\n",WeldTime);
printf("pValue: %d\n\n",pValue);
printf("Press =/- to change W1. ");
confMenu = 1;
Counter++;
}
}
// prevENC_SWstate = ENC_SWstate;
while (confMenu == 1)
{
// ENC_SWstate = digitalRead(ENC_SW);
if (checkButtons())
{
if (1)
{
select += 1;
select = select % 4;
if (select == 0)
{
// noInterrupts();
Counter1 = w1Value / 10 << 1;
// interrupts();
}
if (select == 1)
{
// noInterrupts();
printf("W1 set to %d\n",w1Value);
printf("Press =/- to change W2. ");
Counter1 = WeldTime / 10 << 1;
// interrupts();
}
if (select == 2)
{
// noInterrupts();
Counter1 = pValue / 10 << 1;
printf("W2 set to %d\n",WeldTime);
printf("Press =/- to change P. ");
// interrupts();
}
}
// prevENC_SWstate = ENC_SWstate;
}
switch (select)
{
case 0: //W1
// clearSelectPointer(1);
// lcd.setCursor(0, 0);
// lcd.print(">");
w1Value = readEncoder() % 6 * 10;
if (w1Value <= 0)
{
w1Value = 0;
}
// lcd.setCursor(1, 0);
// lcd.print("W1:");
// lcd.print(w1Value);
// lcd.print(" ");
break;
case 1: //W2
// clearSelectPointer(2);
// lcd.setCursor(0, 1);
// lcd.print(">");
WeldTime = readEncoder() % 51 * 10;
if (WeldTime <= 0)
{
WeldTime = 0;
}
// lcd.setCursor(1, 1);
// lcd.print("W2:");
// lcd.print(WeldTime); //lcd.setCursor(5, 1);
// lcd.print(" ");
break;
case 2: //P
// clearSelectPointer(3);
// lcd.setCursor(8, 1);
// lcd.print(">");
pValue = readEncoder() % 11 * 10;
if (pValue <= 0)
{
pValue = 0;
}
// lcd.setCursor(9, 1);
// lcd.print("P:");
// lcd.print(pValue);
// lcd.print("%");
// lcd.print(" ");
break;
default:
// clearSelectPointer(4);
printf("P set to %d\n",pValue);
confMenu = 0;
select = 0;
break;
}
}
if (zeroCrossingFlag)
// exit while loop due to flag set
{
zeroCrossingFlag = 0;
}
//switch (checkButton())
//{
//case shortPress:
// lcd.clear();
// lcd.print("Welding....");
// Weld();
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("W1:");
// lcd.print(pre);
// lcd.setCursor(9, 0);
// lcd.print("T:");
// lcd.print(char(0xDF));
// lcd.print("C");
// break;
//case longPress:
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Auto Weld");
// delay(800);
// contactSense();
// break;
//case longerPress:
// lcd.clear();
// configMenu();
// tone(bzr, 2220);
// delay(30);
// noTone(bzr);
// delay(200);
// tone(bzr, 1200);
// delay(30);
// noTone(bzr);
// delay(200);
// tone(bzr, 800);
// delay(30);
// noTone(bzr);
// break;
//}
}
}
// end of Loop
//int checkButton()
//{
// int event = 0;
// buttonState = digitalRead(buttonPin);
// // button pressed
// if (buttonState == 0 && previousButtonState == 1)
// {
// delay(20); // blocking debounce routine
// buttonState = digitalRead(buttonPin); // read button again
// if (buttonState == 0 && previousButtonState == 1)
// {
// buttonPressStartTimeStamp = millis();
// startTimeout = true;
// }
// }
// // button released
// if (buttonState == 1 && previousButtonState == 0)
// {
// delay(20); // blocking debounce routine
// buttonState = digitalRead(buttonPin); // read button again
// if (buttonState == 1 && previousButtonState == 0)
// {
// buttonPressDuration = (millis() - buttonPressStartTimeStamp);
// startTimeout = 0; // duration determined no timeout required
// }
// }
//
// if (buttonPressDuration > 0 && buttonPressDuration <= shortTime)
// {
// event = shortPress;
// buttonPressDuration = 0;
// }
//
// if (buttonPressDuration > longTime && buttonPressDuration <= longerTime)
// {
// event = longPress;
// buttonPressDuration = 0;
// previousButtonState = buttonState;
// }
// // button not released and still timing
// if (buttonState == 0 && startTimeout == true && (millis() - buttonPressStartTimeStamp) > longerTime)
// {
// event = longerPress;
// digitalWrite(rdy, 0);
// startTimeout = 0;
// }
// buttonPressDuration = 0;
// previousButtonState = buttonState;
// prevENC_SWstate = ENC_SWstate;
// return event;
//}
Pass:1
Select:0
Counter:0
W1:0
W2:0
pValue:0
Pass:1
Select:1
Counter:0
W1:50
W2:0
pValue:0
Pass:1
Select:2
Counter:0
W1:50
W2:70
pValue:0
Pass:1
Select:3
Counter:18
W1:50
W2:70
pValue:90
Pass:2
Select:0
Counter:10
W1:50
W2:70
pValue:90
Pass:2
Select:2
Counter:18
W1:50
W2:80
pValue:90
Pass:2
Select:3
Counter:24
W1:50
W2:80
pValue:10
Pass:3
Select:0
Counter:10
W1:50
W2:80
pValue:10
Pass:3
Select:1
Counter:16
W1:40
W2:80
pValue:10
Pass:3
Select:1
Counter:16
W1:40
W2:80
pValue:10
Pass:3
Select:2
Counter:2
W1:40
W2:60
pValue:10
Pass:3
Select:2
Counter:2
W1:40
W2:60
pValue:10
Pass:3
Select:3
Counter:4
W1:40
W2:60
pValue:20
Pass:4
Select:0
Counter:8
W1:40
W2:60
pValue:20
Excellent! I'm glad to hear it's working now.Well well, that was it. All looks good now.
here is the output