Change "if, else" statements to multiple "if" statements? robot car project


Need some help turning motors.

I have followed this tutorial successfully

… and have been able to control two DC motors with an IR remote / receiver via this Arduino Uno
and a H bridge motor controller
(Arduino Compatible Stepper Motor Controller Module | Jaycar Electronics).

I have attempted to modify the code so that I can program the arduino to move the motors in such a way as the car turns left, and right respectively when specific buttons are pressed.

In addition to creating new variables to determine motor output, I have changed up an if, else statement to a series of if statements so as to include these additional variables (in the attached, I have kept the previous, working code in the comments).

However, what I have done is not working. The code compiles successfully, but the motors do not run when the ir remote is pressed.

I have been reading posts about if statements, enums, and switch case statements, and I still am not sure what I am doing. If anyone could help / point me in the right direction, I would be very grateful.

Randosketch.ino (4.52 KB)

The IR library uses a timer. PWM uses timers - one for two pins. If the motors move, and change speed, when connected to those pins, without the IR code present, but not when it is present, the timer that the IR library uses is the one that enables PWM on those pins. You'll have to use different PWM capable pins.

Changing the if/else if structure to if/if format was pointless. The value from the IR can't match more than one if statement. If the value is IR_ADVANCE, the old code did one comparison. Now, it does 4.

Hello, thanks for responding!

I am both helped and confused.

I will have a look at changing the pins so that I can refer to the speed in the switch / case statements in the "void do_Drive_Tick()" part of the code. Thanks for pointing that out.

The part that I don't understand about your answer relates to the section "void do_IR_Tick()"

The original if / else statement seems to relate to ?two possible values from the ir remote. The first "if" relates to "IR_ADVANCE" and the second "else" relates to "IR_BACK" - both associated with different hex values on the ir remote.

But I think you are saying that this original comparison relates to only one statement?

The multiple 'ifs' are my attempt to do similar comparisons with two additional statements which have been associated with separate IR buttons in the code above - IR_LEFT, IR_RIGHT:/

I am not sure what the best way is to go about checking multiple statements for my remote.

USing multiple if statements is less efficient than using if/else because all of the if statements will be executed every time. Using if/else causes subsequent tests to be skipped once a match is found

Even better would be to use IRresults.value as the switch value in do_Drive_Tick() rather than converting it to Drive_Num first. Then there would be no need for the ifs in the first place

The original if / else statement seems to relate to ?two possible values from the ir remote. The first "if" relates to "IR_ADVANCE" and the second "else" relates to "IR_BACK" - both associated with different hex values on the ir remote.

The original code did, indeed, only deal with 2 possible values. If the IR value was the one for advance, then there was no reason to evaluate the else if statement. If the IR value was not the one for advance, then the one else if statement does get evaluated. You could easily add more else if statements. When the entire statement is encountered, evaluation stops when a true condition happens.

With a series of if statements, each statement must be evaluated independently.

In your case, the conditional parts of your statements are just comparing two numbers, so they are fast.

If your conditional expressions involved function calls that could take some time, like checking to see if you have a valid WiFi connection, or pinging another server, then you do NOT want to be making those calls any more often that you need to.

Take note of the final paragraph in reply #3 and eliminate the ifs altogether

Thank you! I will play with this tomorrow and see how it all goes