Notifications
Clear all

Adafriut, ina260, if & while statements and a drink from a fire hose!

18 Posts
6 Users
10 Likes
2,247 Views
(@davee)
Member
Joined: 3 years ago
Posts: 1683
 

Hi @quince,

  I notice you asked:

is it possible to hit a moving target like fluctuating current in C++ with anything but a "float"?

-----------------

Whilst the other replies obviously enabled you to proceed with your project, for which I suspect you are very grateful, I wondered if this question had been overlooked.  In case that has happened, I offer the following 'long read' (Sorry, I don't know how much you already know, so I have aimed at going to explain everything from the begining .. apologies if it gets tedious.):

------------------

The answer to your question might be "yes (there are alternates for C++ in general), but float is probably the only sensible answer for your specific situation."

In itself, this answer is almost useless ... so we have to dig a little deeper into the basics...

To put the "answer" into context, hardware like 'simple' analogue to digital converters usually present a 'binary' number to the microcontroller ... if you read the 'raw' value from a '10-bit A/D' into a microcontroller, you would (usually) get a number consisting of 10 bits, each bit being 1 or 0, whose decimal interpretation is (usually) either -512 to +511 or 0 to 1023.  (Note that these are 'integers' (whole numbers) ).

To turn that into a 'real world' voltage (current, etc.), you need to know the scaling factor. For example, for the case of 0 to 1023, where 0 corresponds to 0 V, and the full scale range of this imaginary A/D is 2.048 V, which corresponds to 1024, then when a value 'x' is read in, it is converted to a voltage by:

  Voltage = ( x  * 2.048) / 1024   

e.g. if x is 128, then Voltage = ( 128 * 2.048) / 1024  = 0.256 V

----------

Hence, if your programme code was directly reading values from a device like INA260, it is most likely the 'raw' values corresponding to 'x' in my simple example would be 'integers', and 'int' would often be the most appropriate data type to receive the 'raw' data values.

Having received the value, what happens next depends upon the programmer. In a simple situation, where you want to print 'more' or 'less' type message, than you can compare the raw value in 'x' ... e.g. extending my simple example

int x = raw_read_A_to_D();      // (fictional) function to read A/D value .. expected range of x = 0..1023

if (x < 128)  {
   Serial.println("less than 0.256 V");
}
else   {
    Serial.println("equal or more than 0.256 V");
}

Note that the 'if' comparison value was 128, as it is comparing the 'raw' A/D readings, whilst the print messages are 'helpfully' discussing the corresponding 'real world voltages' for the user's benefit.

In this case there was no need to convert A/D value into 'real world' values. However, if you also wanted to (say) plot the 'real world' voltages on a user graph, then a conversion would be appropriate.

e.g.

                                      //  (fictional) function to read A/D value .. expected range of x = 0..1023  [L1]

int x = raw_read_A_to_D();

                                      // convert the raw reading to a real world voltage                                   [L2]

float y = ( x * 2.048) / 1024.0; 

                                    // (fictional) function to add a real-world voltage reading to the user graph  [L3]

add_point_to_graph (y); 

--------------------

Your programme above uses the ina260 (class) library function:

float current = ina260.readCurrent();

 The 'ina260.readCurrent()' part of that program line is invoking the library code which fetches the raw data from the INA260 and converts it into a real world value, hence it is doing the equivalent of the first line ([L1] in my example above) and the conversion part of L2], leaving you to provide a suitable 'receptacle' for the value using 'float current = '.

-----------

That may leave you wondering, How do I know to provide a 'float'? Would something else like 'int' been suitable?

The simplest answer is 'You have to read the documentation or look up the library code'. In practice, a little detective work is often required at this point ...

Because the data has been converted from the raw INA260 chip data, it might have been changed into almost any form  ... albeit 'float' would probably be a 'chief suspect'.

--------------------

I have never used this device, so I typed 'INA260 adafruit' into Google and noted 'INA 260 adafruit github' was among the choices, so I clicked it ...

The Github Readme.md was (as usual) unhelpful, but in the Github list I noticed Adafruit_INA260.h

Clicking to open this file, I found the function prototypes including this fragment:

class Adafruit_INA260 {
public:
Adafruit_INA260();
bool begin(uint8_t i2c_addr = INA260_I2CADDR_DEFAULT,
TwoWire *theWire = &Wire);
void reset(void);
float readCurrent(void);
float readBusVoltage(void);
float readPower(void);

 Hence, you can see Adafruit260.readCurrent() is returning a value of type 'float', and hence 'float' is the appropriate type for your variable 'current' to receive the value.

--------------------

So whilst, in this case the 'chief suspect' of 'float' corresponded to the 'villain', in other circumstances, it might have been 'int' or 'double', or even some previously unknown 'user' defined type like 'wadgets'. And of course, all of these alternates could be compared in appropriately designed 'if(...)' constructs.

 

Like all the best detectives, you have to track down and follow the evidence.

--------------------

NB The INA260 is somewhat more complex & complicated than my simple '10-bit A/D' example .. it is more like a 16-bit A/D with some smarts including a digital multiplier, but the principles are the same.

 

--------------------

Sorry this is another lengthy sermon .. but I hope it is useful.

Best wishes, Dave.


   
Lee G and Quince reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 
Posted by: @davee

Sorry this is another lengthy sermon .. but I hope it is useful.

Just to say I always find your lengthy replies on whatever subject to be very interesting and I usually learn something from them.  Keep going with these 'sermons' as they provide illumination even if they don't save my soul 😋 


   
Inst-Tech and DaveE reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6971
 

@davee Useful info, but would even be better in a fies section.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 2 / 2