Go Down

Topic: If-statement Is not tripping (Read 5868 times) previous topic - next topic

Syn42ME

Okay, I'm running into a very odd issue. I'm programming a custom stepper-control class to be able to drive a micro-step stepper controller and motor. I'm using an if statement at one point to determine if the microsecond clock has reached the trip point to pulse the motor.

Code: [Select]

bool StepperMicro::step()

  // Call the step function
  if (micros() > _longNextStepTime)
  {
    this->_longNextStepTime = micros() + this->_intStepInterval;
    this->stepMotor();
    this->_intStepsRemaining--;
  }

  // Return true if the remainint steps has reached 0.
  if( this->_intStepsRemaining < 1 ) return false;
  else return true;
}


I've used the Serial.println() command to check the values of both micros() and _longNextStepTime. micros() increments and reads as it should, and _intStepInterval, amusingly enough, is at roughly -24000 (not sure why, but hey, it's lower than micros().) Thus, the if-statement should trip. However, it isn't. I've placed serial prints inside the if-statement to confirm; no good.

Any ideas why this isn't tripping? This statement is used in a couple of other places, even for a blinking LED. Also drove the stepper before I started wrapping up the functionality in a class.

-Syn

PaulS

First suggestion: The this-> stuff looks amateurish. 99.99% of the time, it is not needed. Get rid of it, except in that 0.01% of the time that it is needed.

Second suggestion: Posting code snippets that omit key information, like how _longNextStepTime and _intStepInterval are defined is not the best way to get help. Including long and int in the names is no assurance that the variables are actually defined that way.

Third suggestion: Addition involving variables that can roll over, such as the return value from micros(), is a bad idea. Subtraction is guaranteed to work. Addition is not. Restructure the test to involve subtraction, not addition.

Fourth suggestion: This code:
Code: [Select]
  if( this->_intStepsRemaining < 1 ) return false;
  else return true;

would be much clearer as
Code: [Select]
   return (intStepsRemaining > 0);
The value in the parentheses will evaluate to true (if steps remaining is positive) or false (if no steps remaining).

Quote
_intStepInterval, amusingly enough, is at roughly -24000

This tells me that _intStepInterval is not properly defined as unsigned long.

Syn42ME

First Suggestion Reply: I didn't think it was needed, but I noticed that the Stepper.cpp example in the software made use of it in the Stepper class. I attempted to find what it actually meant online, but search engines don't really return anything helpful when using the word "this", and the symbols are ignored. If I recall, it's used for referencing the class member via pointer, but I don't really know. I was copying the example. I'll delete them.

Second Suggestion Reply: Point. I was refraining from posting the whole thing: I'll submit all definitions next time.

Third Suggestion Reply: Is this because the addition result might be larger than the variable size?

Fourth Suggestion Reply: Thanks for the suggestion! I'm getting back into coding and a bit rusty. Much more streamlined!

Fifth Suggestion Reply: Actually, I mistyped that. I meant to reference _longNextStepTime, and you're right. It is just long, not unsigned long. I will correct that.

Thanks a bunch for the feedback! Maybe these changes will fix the if-statement issue.

-Syn

Syn42ME

#3
Jan 26, 2011, 07:17 pm Last Edit: Jan 26, 2011, 07:19 pm by Syn42ME Reason: 1
Okay, I modified the code as suggested, and then some. It's working now, and the microsteping actually works. I figured I might as well post the current code. Thanks for your help!

The following is a class I wrote to be able to control a commercial microstep controller. As the controller I have (from work, awesome) is a variable-microstep device, I wrote the library to accommodate this. There are a few things to add, but it's comming.

I posted the code over here: Microstep Stepper Controller Interface Class

-Syn

Go Up