digital write a double variable in a 8 bit digital output

Hello everybody!
I am new to the Forum!

I am writing a PID controller using the PID library.
In the end I obtain a value that is a double and I want to feed this value to a 8 bit DAC that will drive a piezo actuator.

Is there any command that gives me an easy way to write the double value in the 8 bit output pins?

Thank you everybody for the help.

Hello everybody!
I am new to the Forum!

I am writing a PID controller using the PID library.
In the end I obtain a value that is a double and I want to feed this value to a 8 bit DAC that will drive a piezo actuator.

Is there any command that gives me an easy way to write the double value in the 8 bit output pins?

Thank you everybody for the help.

Is there any command that gives me an easy way to write the double value in the 8 bit output pins?

You can write an 8bit value in the 8bit output pins.
http://www.arduino.cc/en/Reference/PortManipulation
So you have limit your value to 8bits - for example by shifting it right, or with map() function, etc..

I want to feed this value to a 8 bit DAC

Which one?

An 8 bit input is a byte, not a double.

Hello!
Thank you for your answer.

Sorry I am a newbie.
I would like to know if there is a command to write the double variable directly in the output.
Or a command to convert the double in 8 binaries bit that I can digitalwrite in the PD0...PD7 output.
Because at the moment I don't know how to deal with it.

I would like to know if there is a command to write the double variable directly in the output.

No. You can't use a 32 bit value where a 8 bit value is needed.

But so there is no way to get it in a DAC?
Can i convert it to 8 bit decreasing the resolution?

Can i convert it to 8 bit decreasing the resolution?

Of course you can. How depends on the range of values in the double.

Thanks!
It will be the full range of possible values.
DO you have any advice or can you suggest a possible approach?

It will be the full range of possible values.
DO you have any advice or can you suggest a possible approach?

I find that a bit hard to believe. Where is this double coming from? What is it supposed to represent?

I am trying to realize a self stabilized interferometer.
It is a system where the intensity of light is fluctuating, and in order to keep this intensity fixed to a desired values is possible to control it and compensate the fluctuations using a piezo actuator.

To generate the control signal I am going to use an arduino 2009 board that has in Input an analog signal coming from a detector and in output is connected to a DAC and an amplifier that drive the piezo actuator.

So the Arduino reads the intensity, generate a correction, and apply it through the DAC and actuator.

The double that I want to put in the output is the value of the correction, obtained using a PID algorithm (Proportional Integrative Derivative)PID controller - Wikipedia.

The PID is obtained using the library:

http://playground.arduino.cc/Code/PIDLibrary

Why do you think is unlikely that the corrections goes through the full range?
Cheers

Duplicate posts merged.

Why do you think is unlikely that the corrections goes through the full range?

Because a correction of 3.4028235E+38 seems highly unlikely. I think you really should print out the values that you are actually seeing. Perhaps just truncating the double as a byte would be sufficient. If the range of ACTUAL values is 0 to 255, truncation is sufficient. If the value is greater than 255, division is necessary before truncation.

On the other hand, the correction factor may actually need to be scaled up to fit the allowable range. But, with no idea of what the actual range is, we can't help you.

Ok,
thank you very much.
I'll check in the lab and I will let you know.
But I will try with the truncation for a first test.

Can you give me some information on how apply the truncation on 8 bit for a double?
Or just how to extract the single bits from the 32 bit word of the double?

I really appreciate your help. :slight_smile:

Or just how to extract the single bits from the 32 bit word of the double?

Be careful with that "definition".
A "double" may be 64 bits, and usually is,
Also, an IEEE 754 format floating point number wouldn't normally be subject to individual bit manipulation.

Can you give me some information on how apply the truncation on 8 bit for a double?
Or just how to extract the single bits from the 32 bit word of the double?

On the Arduino, floats and doubles are exactly the same. So, get over the hangup with doubles.

Try looking at a basic C book for the rules regarding what happens when you assign a larger type to a smaller type, and what happens when you assign a float to an int.

float f = 14.2;
int i = f; // i will be 14
byte b = f; // b will also be 14

float ff = 123456789.32;
int ii = ff; // overflow; ii will be garbage
byte bb = ff; // overflow; bb will be garbage