avoiding False triggering a function [SOLVED]

Hi All,

how can i prevent aWeld() functioning from false triggering.

void aWeld() // Auto weld
{
  if (currCtc == 0 && prevCtc == 1)
  {
    //Serial.println(currCtc);
    //Serial.println(prevCtc);
    //Serial.println(" ");
    prevCtc = currCtc;
    //Serial.println(currCtc);
    //Serial.println(prevCtc);
    //Serial.println(" ");
    myServo.attach(servoPin);
    digitalWrite(rdy, LOW);
    ServoStart();
    zeroCrossingFlag = false; // set flag false and wait for next zero crossing
    while (!zeroCrossingFlag)
    {
    };
    delayMicroseconds(tgr_dly);
    digitalWrite(TRIAC, HIGH);
    delay(w1Value); // preWeld time ms
    digitalWrite(TRIAC, LOW);
    delay(pause);
    zeroCrossingFlag = false; // set flag false and wait for next zero crossing
    while (!zeroCrossingFlag)
    {
    };
    delayMicroseconds(tgr_dly);
    digitalWrite(TRIAC, HIGH);
    delay(WeldTime);
    digitalWrite(TRIAC, LOW);
  }
  ServoReturn();
  myServo.detach();
  weldTone();
}

called from here

while (weldExitFlag == false) // begin Auto Weld
  {
    zeroCrossingFlag = false;
    while (!zeroCrossingFlag)
    {
    };
    delayMicroseconds(8600); // Sense voltage (after zero cross). higher number, lower voltage.
    digitalWrite(TRIAC, HIGH);
    delayMicroseconds(200);
    digitalWrite(TRIAC, LOW);
    cs_State = digitalRead(cs);
    currCS_State = cs_State;
    if (currCS_State == prevCS_State)
    {
      ctc = 0;
    }
    else
    {
      ctc = 1;
    }
    prevCS_State = currCS_State;
    currCtc = ctc;
    if (currCtc != prevCtc)
    {
      if (currCtc == LOW && prevCtc == HIGH)
      {
        delay(300);
        aWeld();
      }
      prevCtc = currCtc;
    }
    buttonState = digitalRead(buttonState);
    previousButtonState = buttonState;
    currCtc = prevCtc;
  } // End Auto weld

The above while loop begins when a button is pressed but at times when the while loop is triggered it also calls the aWeld().

How can i prevent it ?

Post your code.

code attached

code.txt (13.9 KB)

anishkgt:
The above while loop begins when a button is pressed but at times when the while loop is triggered it also calls the aWeld().

How can i prevent it ?

Should it NEVER call aWeld()? If so, the way to prevent that is to remove the call to aWeld().

Are you saying that aWeld() is sometimes called the first time through the while() loop and it shouldn't be? In that case you have probably not initialized all of the state variables properly before entering the while().

I meant it should be called only when needed not at beginning of the while loop but when the condition within the loop is true.

So that condition randomly becomes true which is a false trigger.

The condition being

 if (currCtc != prevCtc)
    {
      if (currCtc == LOW && prevCtc == HIGH)
      {
        delay(300);
        aWeld();
      }

anishkgt:
So that condition randomly becomes true

I doubt that...

But if is so, then it's the wrong criterion to use for deciding to call the function or not. You surely need some criterion / criteria you can rely on.

anishkgt:
I meant it should be called only when needed

What are the circumstances in the real world when it needs to be called?

...R

In real world the CS pin is connected to an ac source via an optocoupler with the other side connected to pin 10 on the arduino and the other pin to gnd.

The serial output form CS is
1
0
1
0
1
0
.
.
.
.
.
The aWeld() is called when ac source at the opto are shorted and that will set CS to
0
0
0
0
0
0
The condition here

if (currCS_State == prevCS_State)
    {
      ctc = 0;
    }
    else
    {
      ctc = 1;
    }

Sets 0 when the ac source on the opto are shorted and 1 when idle. Which works as expected.

So I need to call aWeld() when the CTC == 0 and only once and wait for next time when I short it.

anishkgt:
So I need to call aWeld() when the CTC == 0 and only once and wait for next time when I short it.

That sounds like you need a variable the keep track of the fact that aWeld() has been called and the variable gets cleared when CTC returns to 1.

Something like

if (ctc == 1) {
  aWeldMayBeCalled = true;
}

and withing aWeld()

void aWeld() {
   if (aWeldMayBeCalled == false) {
     return;
   }
   else {
     aWeldMayBeCalled = false;
  }
  // other stuff
}

There are other ways to organize the same concept. but this seemed the easiest way to illustrate it.

...R

anishkgt:
The condition being

 if (currCtc != prevCtc)

{
     if (currCtc == LOW && prevCtc == HIGH)
     {
       delay(300);
       aWeld();
     }
    prevCtc = currCtc;
  }

My guess is that 'prevCtc' was accidentally set or left HIGH before entering the loop. Since I can't see the declaration of 'prevCtc' I can't be sure why that is going wrong. Could be it's an uninitialized local variable. Could be it's a 'static' or global variable left in an undesired state.
Perhaps setting 'prevCtc' to LOW before entering the loop would help.

johnwasser:
My guess is that 'prevCtc' was accidentally set or left HIGH before entering the loop. Since I can't see the declaration of 'prevCtc' I can't be sure why that is going wrong. Could be it's an uninitialized local variable. Could be it's a 'static' or global variable left in an undesired state.
Perhaps setting 'prevCtc' to LOW before entering the loop would help.

Yes it was. It was set to HIGH when leaving the while loop, so i set it to '0' when entering Main Loop and that solved it.