9 years, 9 months ago.

DigitalOut(PG_13).write(1) to modify pin value

Hello,

I tried changing a led pin value with the constructor. It works, can toggle the pin on and off.

DigitalOn(PG_13).write(1); wait(2); DigitalWrite(PG_13).write(0);

Just not sure if I am creating a new object each time? Can you comment on this?

Thanks

1 Answer

9 years, 9 months ago.

You are indeed creating a new object everytime. Just use this example for DigitalOut: http://developer.mbed.org/handbook/DigitalOut (the = 1 is here the same as .write(1)).

Accepted Answer

Erik,

Thanks for the answer. I understand that it creates a new object and I should be using DigitalOn as you pointed out. However, I am interested in what happens in this instance. So, basically it creates a new object, does the write and calls the destructor(because it is not assigned to a value)? From your answer, it implies the way I did is a bad way of coding, but I am interested in knowing why. Can you point me to any references or explain why?

posted by renasis@... 24 Feb 2015

The problem is that it is inefficient: Every time it changes state instead of just toggling one bit, instead it first has to create a new object, it puts alot of settings correct (even though they are already correct), re-calculates values (such as which addresses it needs to use for reading/writing), then it toggles that single bit, and finall it destroys everything again. (Which in this instance is probably just erasing it from its RAM memory).

posted by Erik - 24 Feb 2015

Thanks for the help! I understand now.

posted by renasis@... 25 Feb 2015