Prompt to Turn on LEDs

06 Nov 2012

Embedded Programming Suggestion Needed

I am new to embedded programming. I am intended to prompt for input to control two LEDs. I there a better way of doing this? This is what I got; this code is tested working but I know there got to be a normal way that embedded programmer accustom to be using for example using mask etc? Would anyone care to give me some better suggestion?

int main() {
    
    
    int i;
    
    while (1) {
    pc.printf ("\n\r Please Enter hex number: ");
    pc.scanf ("%x", &i );
    
    if ( i == 0x03) {
        pc.printf ("\n Three ");
        bit0 = 1;
        bit1 = 1;
    }
    else if (i == 0x02){
        pc.printf ("\n Two ");
        bit0 = 0;
        bit1 = 1;
    }
    else if (i == 0x01) {
        pc.printf ("\n One ");
        bit0 = 1;
        bit1 = 0;
    }
    else { 
        pc.printf ("\n Zero ");
        bit0 = 0;
        bit1 = 0;
    }
            
    wait(1);
    } 
}

06 Nov 2012

You can try to directly use a mask from what returns from the serial input, but then you got to start using directly the mbeds registers. But really for almost all situations that is a waste of time. It only makes sense to do such small optimizations when they get called really often. An if-then-else statement of this size also has only little overhead, for sure less than the printf and scanf, not to mention the serial speed.

If you want to do it to get cleaner code, consider either a switch statement, or what can work nice in this case, the BusOut class.