Quick simple If/Else Question

Is there anyway to write this code in a much simpler way? All I want to do is compare two values, if one is higher I want it to spin a motor one way, if its lower i want it to spin it the other way.

if (wristValue > wristPot)
{
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}
else
{
if (wristValue < wristPot)
{
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
else
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}

Thanks

From the Arduino reference http://arduino.cc/en/Reference/Else

Try something like “else if”

if (pinFiveInput < 500)
{
  // do Thing A
}
else if (pinFiveInput >= 1000)
{
  // do Thing B
}
else
{
  // do Thing C
}

That's exactly what I did.

If you’re just trying to set it to go in one direction or another depending on the value of the ‘wristValue’, this would work:

if (wristValue > wristPot) {
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
} else { // wristValue <= wristPot
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
}

If you wanted to reduce the repetitive code, this would be shorter, at the cost of complexity:

bool fwd = wristValue > wristPot;
digitalWrite(2, fwd);
digitalWrite(3, !fwd);

magruder13:
That’s exactly what I did.

You have another case in your code which somewhat confuses us, as there are three ‘states’: (high, low), (low, high), and (low, low), in which case else-if trees are probably the best way to go.

Your code effectively tests three conditions: 1) one value is larger. 2) same value is smaller. 3) they are the same. if-else is probably the "simplest" method.

As a side note, the code posted by theepdinker is not "exactly" the same as your original code. Your 2nd if-statement is nested inside of the else-clause of the 1st if-statement. theepdinker's code makes uses of the "else-if" clauses. If you needed to go another level of "if-else" the code posted by theepdinker would be easier to follow than your original code.

Aeturnalus,

The program I’m running is reading in 5 variables from an xbee and 5 from analog in then comparing them and adjusting the digital pins, it’s not a large program but it seems to me to take a lot of steps and its all in one big loop and I want to make sure that it reacts fast. Would both of those setups run at around the same speed? Does less code mean it runs faster?

Everyone else, I’m very new to programming on an arduino, my whole program is in one void loop, is there any way to make each part run at the same time? I don’t know if you can multitask on an arduino.

I want to make sure that it reacts fast.

Define: "fast." How slow is too slow? (In terms of numbers, not vague terms like "as fast as possible.")

Does less code mean it runs faster?

No. Efficient code runs faster. "Less code" is ambiguous.

my whole program is in one void loop

That's how everyone's Arduino sketches/programs run.

is there any way to make each part run at the same time? I don't know if you can multitask on an arduino.

Technically you can't "multitask." Like with any microcontroller-based platform the best approach is to write efficient code to effectively perform multiple tasks at once.

Fast, as in, I can adjust 5 pots on one arduino, have it run all of the data through an xbee and into this arduino and then compared to another pot that is read in and adjust the motor within 500ms.

The bit rate (and amount of data) you send between the two Arduinos over Xbee probably has a bigger impact on total execution time than a handful of if-statements.

James,

What do you mean bit rate? Is this the baud? What is baud anyway? All i know is they need to be the same on the sending and receiving end.

Baud is a unit equivalent to a bit per second. The baud rate defines the rate of transfer of data through a serial connection - they need to be the same on both sides of the connection because it determines the interval between bits.

A simple analysis of baud rate on the Arduino:
If you transfer data at 115200 baud (just about the fastest you can do with the USART at low error rates), you have 16,000,000/115,200 = 139 processor cycles per bit transferred. Since you transfer data in bytes (packets of 8 bits + 1 stop bit), this equates to about 1250 cycles per byte transferred. In perspective: a single if/else comparison takes one processor cycle, provided no calculations are done inside the comparison. That means you can fit well over a few hundred if/else statements before you’ve even transferred one byte of data - and your packets are almost certainly larger than that. As such, the bottleneck in your system is NOT the data processing, but the speed at which you can transmit data.

Assuming you use the ‘optimum-speed’ transfer algorithm:

Time to read 5 pots: 5 pots * ADC read time = 0.521 milliseconds
Transfer time: 5 pots * (8 bits per pot) @ 115200 baud = 3.47 milliseconds.
Time to read 1 pot: 0.104 milliseconds
A simple if/else comparison: 1 comparison * one processor cycle: 0.000062 milliseconds
Time to adjust the motor: ??? but probably <= 50 milliseconds.

0.521 + 3.47 + 0.104 + 0.000062 + 50 = just under 55 milliseconds. For your speed requirements, I wouldn’t worry about optimization - make it easy to understand and reliable first. You’ve got quite a bit of time to spare, after all.

Thanks Aeturnalus, I was really worried about getting this whole project done and worrying about if being laggy. The relays I'm using will take longer to switch on and off than it will to process it.