This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

If statements

Hi, Currently doing a project for college and have come to a road block. I have function which has a if statement. There are five different possible
outcomes for the if statement. The outcome depends on four different inputs, the fifth
one is null. The problem i'm having is I don't want to come out of the if statement if
there's no change to the input. I'm using a do while statement with the if statement. temp_read is the variable I store the value of which if statement is being used. Hope someone understands what i'm trying to say.

Thanks Thomas

void read_braking(void)
{ unsigned char temp_read; unsigned char total_secs; unsigned char new_min; unsigned char new_hrs; unsigned char new_read; total_secs = 0x00; temp_read = 0x00; new_read = 0x00;

do
{ if((thinking == 1) && (easy == 0) && (medium == 0) && (hard == 0)) { yellow_led = ON; green_led = OFF; amber_led = OFF; red_led = OFF; temp_read = 0x08; }

else if((thinking == 1) && (easy == 1) && (medium == 0) && (hard == 0)) { yellow_led = ON; green_led = ON; amber_led = OFF; red_led = OFF; temp_read = 0x0C; }

else if((thinking == 1) && (easy == 1) && (medium == 1) && (hard == 0)) { yellow_led = ON; green_led = ON; amber_led = ON; red_led = OFF; temp_read = 0x0E; }

else if((thinking == 1) && (easy == 1) && (medium == 1) && (hard == 1)) { yellow_led = ON; green_led = ON; amber_led = ON; red_led = ON; temp_read = 0x0F;

}

else { yellow_led = OFF; green_led = OFF; amber_led = OFF; red_led = OFF; temp_read = 0x00; }

}

while (temp_read == temp_read);

external_write(temp_read);

return;
}

Parents
  • Note that 5 bits can be converted into a number 0..31.

    So:

    unsigned char val;
    unsigned char prev_val;
    
    prev_val = 255;
    for (;;) {
        for (;;) {
            val = 0;
            if (thinking) val |= 1;
            if (easy) val |= 2;
            if (medium) val |= 4;
            if (hard) val |= 8;
            if (val != prevval) {
                switch (val) {
                    case 1:
                    case 3:
                    case 7:
                    case 15:
                        yellow_led = (val & 1) ? ON : OFF;
                        green_led = (val & 2) ? ON : OFF;
                        amber_led = (val & 4) ? ON : OFF;
                        red_led = (val & 8) ? ON : OFF;
                        break;
                    default:
                        // invalid combination
                        yellow_led = green_led = amber_led = red_led = OFF;
                        // do a continue here if invalid combinations should break loop.
                }
                break; // end this loop, since you had a change
            }
        } // loop until a change found (or maybe a change to another valid value)
        prev_val = val;
        // do something fancy now when you know you have a change (or the first time)
        ...
    }
    

Reply
  • Note that 5 bits can be converted into a number 0..31.

    So:

    unsigned char val;
    unsigned char prev_val;
    
    prev_val = 255;
    for (;;) {
        for (;;) {
            val = 0;
            if (thinking) val |= 1;
            if (easy) val |= 2;
            if (medium) val |= 4;
            if (hard) val |= 8;
            if (val != prevval) {
                switch (val) {
                    case 1:
                    case 3:
                    case 7:
                    case 15:
                        yellow_led = (val & 1) ? ON : OFF;
                        green_led = (val & 2) ? ON : OFF;
                        amber_led = (val & 4) ? ON : OFF;
                        red_led = (val & 8) ? ON : OFF;
                        break;
                    default:
                        // invalid combination
                        yellow_led = green_led = amber_led = red_led = OFF;
                        // do a continue here if invalid combinations should break loop.
                }
                break; // end this loop, since you had a change
            }
        } // loop until a change found (or maybe a change to another valid value)
        prev_val = val;
        // do something fancy now when you know you have a change (or the first time)
        ...
    }
    

Children