Argument pass to function incorrectly

In this project I've started using the "elapsMillis" library to hand a variety of timings. This is my first time with this library so want to verify it is doing what I expect.

I have a function that uses a switch to select which delay is to be tested. Currently I have 5 different delays.

All definitions are above setup() so should be totally global.

I have a test function that I run in startup to see if a selected delay will be as long as expected. I send the desired delay as a value from 0 to 5 - but the timeIsUp() function, the passed variable shows up always as 2! It will respond to 8 or 10 or 50 correctly, but smaller numbers always revert to 2!

:o WTF???

Below is the essential code snips involved
First I'm showing the call in startup, then the test function and the switch function/selector:

Global defs

#define DBGDLYTIME 3000  //  3000 ms for debuging
#define PINGTIME 1000  //  ms to rcv ping response
#define RCVDATATIME 1000  //  ms for gettng data
#define CONECTTIME 1000  //  ms for connection test
#define PAUSEDLYTIME 1000 // ms for stepper pause
#define STEPTIME 1500  // ms for steppers to move

// indexes for elapsed milli case test
#define elapsMilDbg 0
#define elapsMilPing 1
#define elapsMilRcvDat	2
#define elapsMilConn 3
#define elapsMilPause 4
#define elapsMilStep 5

// updated millis variable
elapsedMillis DBGDLY;
elapsedMillis PINGDLY;
elapsedMillis RCVDATADLY;
elapsedMillis CONECTDLY;
elapsedMillis PAUSEDLY;
elapsedMillis STEPDLY;

----------------------------------
void delayTest(int intval) {
	int wi = 4;

	Serial.print("Start Test...");
	Serial.println(millis());
	bool flag = true;
	while (flag) {
		if (timeIsUp(wi)) {
			Serial.println("Test Dun! -- ");
			Serial.println(millis());
			flag = false;
		}
		else Serial.println(millis());
	}
}



boolean timeIsUp(unsigned int whichDly) {

	boolean rtnval = false;
	Serial.print("in TIU ");
	Serial.println(whichDly);
	
	switch (whichDly) {
	case elapsMilDbg:
		Serial.println(i++);
		if (DBGDLY >= DBGDLYTIME) {
			DBGDLY = 0; ///DBGDLY - DBGDLYTIME;  // reset timer
			rtnval = true;
		}
		break;
	case elapsMilPing:
		if (PINGDLY > PINGTIME) {
			PINGDLY = 0;
			rtnval = true;
		}
		break;
	case elapsMilRcvDat:
		if(RCVDATADLY> RCVDATATIME) {
			RCVDATADLY = 0;
			rtnval = true;
		}
		break;
	case elapsMilConn:
		if (CONECTDLY > CONECTTIME) {
			CONECTDLY = 0;
			rtnval = true;
		}
		break;
	case elapsMilPause:
		if (PAUSEDLY> PAUSEDLYTIME) {
			PAUSEDLY = 0;
			rtnval = true;
		}
		break;
	case elapsMilStep:
		if (STEPDLY > STEPTIME) {
			STEPDLY = 0;
			rtnval = true;
		}
		break;
	}

	return rtnval;
}
	int wi = 4;

		if (timeIsUp(wi)) {

boolean timeIsUp(unsigned int whichDly) {

It is generally a good idea to use the proper argument type for any function (you don't really expect values as high as 65535, do you?). It is generally a good idea to pass a function a value of the type it expects.

		Serial.println(i++);

27 shows up in the Serial Monitor application. What the heck does that mean? Anonymous printing sucks.

		if (DBGDLY >= DBGDLYTIME) {

How can a class instance be greater than or equal 3000?

Post your complete sketch (program code)! If you don't you waste time while people ask you to do that.

PaulS:

 int wi = 4;

if (timeIsUp(wi)) {

boolean timeIsUp(unsigned int whichDly) {



It is generally a good idea to use the proper argument type for any function (you don't really expect values as high as 65535, do you?). It is generally a good idea to pass a function a value of the type it expects.



Serial.println(i++);



27 shows up in the Serial Monitor application. What the heck does that mean? Anonymous printing sucks.



if (DBGDLY >= DBGDLYTIME) {



How can a class instance be greater than or equal 3000?

Sheese! I always get a spanking when I ask a relatively simple question! :frowning: This is DEBUGGING and study code - not part of the complete app.

I HAVE included all the pertinent code to the issue - look at a few hundred lines of code that has nothing to do with this issue would NOT help. The main body of code is several years old and I've just added this new elapsedMillis library.

Obviously you don't know about the "elapasedMillis" library. Also - that is NOT really part of the problem. I have probably written thousand of functions over the past 30 years, and generally an argument passed to it shows up as sent. This is why I go so flummoxed at seeing the 2 show up!

For some bizarre reason whatever value I send to the switch function - it displays the value of 2. In the sample code I unfortunately left some of my test code in trying to find out what I could pass and get the correct value. Sorry if this has confused you.

The entire point of this question is how/why sending differing forms of INT's (should not matter) produce a variety of values. So if have any ideas why an argument sent to a function shows up as a different value I'd love to hear/see it. I don't need critique's on the rest of the code.

Have a nice day! >:(

Obviously you don't know about the "elapasedMillis" library.

Obviously, you should have provided a link.

This is why I go so flummoxed at seeing the 2 show up!

It would be far better to be able to say "Why do the Serial.print()s on line n through m of this snippet print "xxx = 2", when I expect them to print "xxx = The moon is made of green cheese"".

But, you're the one with the problem, seeking help, not me.

PaulS:
Obviously, you should have provided a link.
It would be far better to be able to say "Why do the Serial.print()s on line n through m of this snippet print "xxx = 2", when I expect them to print "xxx = The moon is made of green cheese"".

But, you're the one with the problem, seeking help, not me.

Sorry - but that was NOT the question! I have no problem with Serial.print().

Hopefully you could see that I was passing an integer argument of a function and the same value was not getting to the function. THAT is the heart of the question... not my (maybe piss poor) debugging practices. Also in the opening of the question I did say I was working with the "elapsedMillis" library - that probably did throw a FUD factor into things... really nothing to do with the prob.

Function did not like #define values as an argument. When I defined them as an INT, it works great.

Thanks! :slight_smile:

PaulS:

 if (DBGDLY >= DBGDLYTIME) {

How can a class instance be greater than or equal 3000?

Because the elapsedMillis class overrides "operator unsigned long"

operator unsigned long () const { return millis() - ms; }

So, it's perfectly proper and a nice use of OOP techniques. My only objection is the use of all CAPITALS for a variable name. Per convention, it should be: "dbgdly".

Because the elapsedMillis class overrides "operator unsigned long"

Would have been nice to see a link to the library to learn this.

In most cases, I'm in favor of overloading operators, where it makes sense. This overload does not.

PaulS:
This overload does not.

That's an opinion. I find it a useful example of OOP Abstraction. You may not agree, but you should at least understand the concept / purpose of the library to comment: Delay and Timing Functions.

I think it would have been more obvious what was happening to have added a field, time, to the class, and used that field in comparisons.

   elapsedMillis waiting;
   while(waiting.time < 1000)
   {
      // we're waiting...
   }

Everybody has different opinions, which is what makes programming so "fun".