Code acting weird

  if (ContainerLSWState == HIGH){
    CCPM = millis();
  }

   if (millis() - CCPM == CCI){
    PickedupSeq();
   }

   if (ContainerLSWState == LOW) {
    COPM = millis();
   }

   if (millis() - COPM == COI){
    ReturnSeq();
   }

I have the above code that is acting weird. When my ContainerLSWState is HIGH, the code runs ReturnSeq(); function, and vice versa.
My agenda is to see the switch status, if its high or low, wait for 5 secs and then run the following funtion once only.

your've posted a fragment of code within some function. the code does not stop running during each of the if statements in this code.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Thats the funny part, the code only runs once. So whats happening with this code is:
when the status turns HIGH, after 5 seconds, the ReturnsSeq(); functions runs once. and then thats it. now the code waits for the next change of state.
here is a code:

void loop() {
 currentMillis = millis();

  if((currentMillis - AliveOldMillis) >= AliveInterval){
    AliveOldMillis = currentMillis;
    KeepAliveMessage();
  }
ContainerLSWState = digitalRead(ContainerLSW);
  if (ContainerLSWState == HIGH){
    CCPM = millis();
  }

   if (millis() - CCPM == CCI){
    PickedupSeq();
   }

   if (ContainerLSWState == LOW) {
    COPM = millis();
   }

   if (millis() - COPM == COI){
    ReturnSeq();
   }
}

Hi,
These ifs will only be true for 1 millisecond,
and if it's not being tested right now it won't be true anymore.
Try using:

 if (millis() - CCPM >= CCI){

 if (millis() - COPM >= COI){

RV mineirin

They may not become true at all, because millis skips each 42nd value.

each time this code runs and ContrainerLSW is HIGH is sets CCPM.

since CCPM was just set to millis(), i doubt millis() - CCPM == CCI can be true

are you getting this?

I see what you are saying. but then why is the ReturnSeq() code end up running?

I can't say because you haven't posted your complete code.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Ok i will check this out. THank you.

I dont see why posting the whole code will help. there is some proprietary information that cannot be share. The main loop is already posted above, there is nothing more to it.

Then go to your proprietors and explain to them that you need professional help. And they’ll probably have to pay for it.

This is a forum for beginners, hobbyists, dilettantes and the like.

If you can’t share information critical to solving you problem, we cannot help. If you cannot see why we ask for the things we do you are in over your head.

The problem is undoubtedly most likely to be found in the code you refuse to expose.

a7

Well if you had the decency to pay attention to the code instead of complaining about sharing the whole code, you would have found the problem in there. I already got a private message from another member explaining what is going on with the code and why i am seeing what i am!
As i mentioned in my above post, there is nothing more in the loop.. that was it.. everything else was just communication modules for server access.

For all those who paid attention to the code and want to find the problem:
The problem was simple, not to my eyes unfortunately. But when the status of the switch changed, for example from Low to High, the if statement checking for the low state had the millis value retained, and that ran the 5sec timer and ran the returnseq(); function.

Thank you all for participating.

the irony of the code is that in one state, either CPPM or COPM is being reset to millis() preventing the the time to expire.

is this the intended design?

It wasnt, but i guess it works as expected.. we just have to swap the functions being called and it will work exactly like what it was supposed to do.

guess??

pay attention

The standard approach to this situation is to write an MRE, Minimal Reproducible Example. That gives people a fair chance to fully understand and sometimes also test the code.

The problem with not posting a complete program, is that there are usually dependencies, such as data type declarations, that shouldn't be assumed. Also the side effects of the hidden code that may influence the revealed code are essentially limitless.

Maybe. Anyway, sorry, truly. I will add indecent and inattentive to the list of my character flaws.

My own ability to spot errors just by reading is limited, especially when there is no context. As @anon57585045 has said, I am among those who will put more attention onto something complete, that I can run and tinker with…

And it is too bad the solution was PM delivered, thanks whoever that was, but s/he would have been more in the spirit of these fora to place those observations in line, publicly, for the benefit of all.

a7

Hi,

Good idea.. :+1: :+1: :+1:
Some Serial.print might help too..

Tom... :smiley: :+1: :coffee: :australia:

Understood. and I appreciate you putting it together the way you did.
I will surely keep that in mind posting any queries in the future in this forum or any other.