2

When I'm running some test on my encoder motors using the following provided code by Makeblock, it appears that the if..else.. statement is not mutually exclusive ie. I can control Encoder_1 and Encoder_2 at the same time. How come?

case ENCODER_BOARD:
      if(port == 0)
      {
        uint8_t slot = readBuffer(7);
        int16_t speed_value = readShort(8);
        speed_value = -speed_value;`

        if(slot == SLOT_1)
        {
          Encoder_1.setTarPWM(speed_value);
        }
        else if(slot == SLOT_2)
        {
          Encoder_2.setTarPWM(speed_value);
        }
        else if(slot == SLOT_3)
        {
          Encoder_3.setTarPWM(speed_value);
        }
        else if(slot == SLOT_4)
        {
          Encoder_4.setTarPWM(speed_value);
        }
      }

int16_t readShort(int16_t idx)
{
  valShort.byteVal[0] = readBuffer(idx);
  valShort.byteVal[1] = readBuffer(idx+1);
  return valShort.shortVal; 
}

uint8_t readBuffer(int16_t index)
{
  if(BluetoothSource == DATA_SERIAL)
  {
    return buffer[index];
  }
  else if(BluetoothSource == DATA_SERIAL2)
  {
    return bufferBt1[index];
  }
  else if(BluetoothSource == DATA_SERIAL3)
  {
    return bufferBt2[index];
  }
}

All the code are taken from here and I am not the owner. I myself still studying how these whole code works. I am hoping someone out there who have experience with these kind of implementations be able to help me out.

7

if and else if clauses are mutually exclusive. If the if is true then the else if cannot run.

Therefore it must be the case that the code you posted is being executed twice (at least) - once for SLOT_1 and once for SLOT_2. Just so fast that you think it's happening at the same time.

| improve this answer | |
2

Exactly one of the blocks in if{..} else {...} will be executed. Your misunderstanding is elsewhere.

| improve this answer | |
1

Yes they are mutually exclusive.

Note that the following construct is more appropriate:

switch (slot)
{
case SLOT_1:
     Encoder_1.setTarPWM(speed_value);
     break;

case SLOT_2:
     Encoder_2.setTarPWM(speed_value);
     break;

case SLOT_3:
     Encoder_3.setTarPWM(speed_value);
     break;

case SLOT_4:
     Encoder_4.setTarPWM(speed_value);
     break;

default: // other cases/error
     // Handle error situation
     break;
}

You can do the same with BluetoothSource.

Also you can do multiple commands within one case statement, like:

case SLOT_4:
     Encoder_4.setTarPWM(speed_value);
     Encoder_Extra.setTarPWM(speed_value); 
     break;

Note that the switch/case only works where you compare one variable/expression.

| improve this answer | |
0

am hoping someone out there who have experience with these kind of implementations be able to help me out.

Match out the brackets (to your logic) and the issue will be clear.

Your inadvertently created a duffs machine

| improve this answer | |
  • 2
    I see no Duff’s device in his snippets. – Edgar Bonet May 24 '17 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.