Bool not working in if(status==true)

Hi Guys
I am working on a step motor driver class (Attached).
This is the main loop code using the “Step_driver_class_int.h” which works fine

#include “Step_driver_class_int.h”
long No1=1600;//16304
long No2=6400;//16304
int done1=0;
int done2=0;

StepDriver Step1(6,7,1600,LOW,300);
StepDriver Step2(A1,A2,3200,LOW,300);

void setup() {
Serial.begin(9600);
}

void loop() {

done1=Step1.Update(); //Big step motor
done2=Step2.Update();

if(done1==1){
No1=No1*-1;
Step1.change(No1,300);
delayMicroseconds(10);
}
if(done2==1){
No2=No2*-1;
Step2.change(No2,500);
delayMicroseconds(10);
}
}

Originally I wrote the class with a update function returning an int.
But I would like to return bool indicating if the stepmotor is done stepping or not i.e. true/false
Like this

#include “Step_driver_class_bool.h”
long No1=1600;//16304
long No2=6400;//16304
bool done1=0;
bool done2=0;

StepDriver Step1(6,7,1600,LOW,300);
StepDriver Step2(A1,A2,3200,LOW,300);

void setup() {
Serial.begin(9600);
}

void loop() {

done1=Step1.Update(); //Big step motor
done2=Step2.Update();

if(done1==true){
No1=No1*-1;
Step1.change(No1,300);
delayMicroseconds(10);
}
if(done2==true){
No2=No2*-1;
Step2.change(No2,500);
delayMicroseconds(10);
}
}

The int class works fine but I cannot get the bool class to work, the motors runs in a irrugular fashion.
I can simply not see why it shoulden work.
Do you guys have any idears why not?
The reason I want to use bool instead of int is that the int sometimes in special case returns more than 1 so my if statment if(done1==1) will not activate even if done1>1.
One solution might be if(done1>=1) but I do not think this the way to go.
Hope you can help.

Step_driver_class_bool.h (1.52 KB)

Step_driver_class_int.h (1.51 KB)

What boolean does your update() method returns when (i < No) and (currentMillis-previousMillis < stepInterval) ??

the int sometimes in special case returns more than 1

that’s for the same reason…

Your variables have weird names

unsigned long currentMillis=micros();

Should return false for i<No with the else return false but their might be a case with the time?

You need to ensure your function returns something all the time

Verify your logic

Hi
I have added the following check to the code

if(currentMillis-previousMillis<stepInterval){
return false;
}

And it seemed to fix the problem with using bool which I can now use.
Thanks for pointing me in the right direction J-M-L

Hello DjangoArduino,

Welcome to the forum.

You have obviously read 'how to use this forum - please read', please can you tell me where it says to post code in different colours rather than in code tags? I can't remember seeing that, maybe I forgot or something.

Thank you.

Please don't

if(x == true) {
}

When x is already a boolean. I mean, where does it all end?

if(((x == true) ==true ) == true) {
}

Use

if(x) {
}

and be sure to give your variable a useful name.

PaulMurrayCbr:
and be sure to give your variable a useful name.

Along that line, check out [convention #26](C++ Programming Style Guidelines Conventions). Makes it easy to see the type at a glance *and *it goes well with if().

DjangoArduino:
Hi
I have added the following check to the code

if(currentMillis-previousMillis<stepInterval){
return false;
}

And it seemed to fix the problem with using bool which I can now use.
Thanks for pointing me in the right direction J-M-L

ideally you should have a return just before the closing brace as a "catch all’ case. this way you will be sure that your function does return something matching its signature and not need for the extra if supposedly catching the remaining cases.