5 years, 8 months ago.

Mbed DigitalOut problem?

Is there something wrong in my "code"? (the wait time should get smaller step by step until it reaches 0.1). I dont have device, so I used mbed online simulator, but pins start to act crazy(maybe it is bcs of mbed online simulator?)

  1. include <mbed.h> DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3);

int main() { float a=1; while(1) { led1=1; led2=0; led3=0; wait(a); led1=0; led2=1; led3=0; wait(a); led1=0; led2=0; led3=1; wait(a); if(a!=0.1) a=a-0.1; } }

1 Answer

5 years, 8 months ago.

float can not represent 0.1 exactly. In your code, "if (a != 0.1)" is always true and it keeps calling "a = a - 0.1;".

floating point precision basic: https://floating-point-gui.de/basic/

You can change the if statement to "if (a > 0.1)" or something like that.

posted by Kentaro Okuda 21 Mar 2019